mongodb C++调用查询DEMO


mongodb C++调用简单DEMO

简单粗暴,直接扔代码!!

main.cpp

/*
* file : main.cpp
* directory : example for mongodb using in c++
* author : HotVC
* Official statement:
* http://docs.mongodb.org/ecosystem/drivers/cpp-to-sql-to-mongo-shell/
*/

#include<iostream> 
#include"mongo/client/dbclient.h"  
#include <stdlib.h>
#include <stdio.h>
#include <boost/foreach.hpp>


using namespace std;
using namespace mongo;

#define CONNECT_STRING "mongodb://localhost:27017"
#define DATA_COLLECTION "testdb"
#define DATA_TABLE "student"

ConnectionString g_csConn;
string g_strErr;
//boost::scoped_ptr<DBClientBase> g_spConnBase;
DBClientBase *g_spConnBase;

//

int main() {

	//call the client initialize function to use the BSON library.
	mongo::client::GlobalInstance instance;
	if (!instance.initialized()) {
		std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
		return EXIT_FAILURE;
	}


	g_csConn = ConnectionString::parse(CONNECT_STRING, g_strErr);

	if (!g_csConn.isValid())
	{
		cout << "Error parsing connection string " << CONNECT_STRING << ": " << g_strErr << endl;
		return EXIT_FAILURE;
	}
	cout << "g_csConn is right" <<endl;

	g_spConnBase = g_csConn.connect(g_strErr);
	//  foreach dataBase
	list<string> lDataBase = g_spConnBase->getDatabaseNames();
	BOOST_FOREACH(string strIterator, lDataBase)
	{
		cout << "DataBase Name is : "<< strIterator <<endl;
	}

	//  foreach collections
	list<string> lCollections = g_spConnBase->getCollectionNames(DATA_COLLECTION);
	BOOST_FOREACH(string strIterator, lCollections)
	{
		cout << "collections Name is : "<< strIterator <<endl;
	}

	cout << "================================================"<< endl;
	cout << "* insert into testdb.student"<< endl;
	cout << "* mongodb expression : db.student.insert({\"name\":\"jk1\", \"age\":20, \"sex\":\"gril\"} )" << endl;

	try
	{
		g_spConnBase->insert("testdb.student", BSON("name" << "jk1" << "age" << 19 << "sex" << "gril"));
		g_spConnBase->insert("testdb.student", BSON("name" << "jk2" << "age" << 20 << "sex" << "boy"));
		g_spConnBase->insert("testdb.student", BSON("name" << "jk3" << "age" << 21 << "sex" << "gril"));
		g_spConnBase->insert("testdb.student", BSON("name" << "jk4" << "age" << 22 << "sex" << "boy"));
		g_spConnBase->insert("testdb.student", BSON("name" << "jk5" << "age" << 23 << "sex" << "boy"));
		g_spConnBase->insert("testdb.student", BSON("name" << "jk6" << "age" << 24 << "sex" << "gril"));
	}
	catch( ... )
	{
		cout <<"no insert "<<endl;
	}


	cout << "================================================"<< endl;
	cout << "* update testdb.student"<< endl;
	cout << "* mongodb expression : db.student.update({\"name\":\"jk1\"}, {\"$set\":{\"sex\":\"boy\"}})" << endl;
	try
	{
		g_spConnBase->update("testdb.student", BSON("name" << "jk1"), BSON("$set"<< BSON("sex" << "boy")));
	}
	catch( ... )
	{
		cout <<"no update "<<endl;
	}

	//count of testdb.student object
	cout << "count:" << g_spConnBase->count("testdb.student") << endl;    //先输出对象数量

	//foreach data in the collections testdb.student collections
	cout << "================================================"<< endl;
	cout << "* Find All"<< endl;
	cout << "* mongodb expression : db.student.find()"<< endl;

	auto_ptr<DBClientCursor> cursorAll = g_spConnBase->query("testdb.student", BSONObj());
	while (cursorAll->more())
		cout << cursorAll->next().toString() << endl;

	//query the data where age = 20
	cout << "================================================"<< endl;
	cout << "* Find age = 20"<< endl;	
	cout << "* mongodb expression : db.student.find({\"age\":20})"<< endl;
	auto_ptr<DBClientCursor> cursorAge = g_spConnBase->query("testdb.student", BSON("age" << 20));
	while (cursorAge->more())
		cout << cursorAge->next().toString() << endl;

	cout << "================================================"<< endl;
	// cout << "* Find name  = json "<< endl;	
	// cout << "* mongodb expression : db.student.find({"age":20})"<< endl;
	// auto_ptr<DBClientCursor> cursorName = g_spConnBase->query("testdb.student", BSON("name" << "json*"));
	// while (cursorName->more())
	// 	cout << cursorName->next().toString() << endl;


	cout << "================================================"<< endl;
	cout << "* Find 20 <= age  <= 22 "<< endl;	
	cout << "* mongodb expression : db.student.find({\"age\":{\"$gte\":20, \"$lte\":22}})"<< endl;
	auto_ptr<DBClientCursor> cursorAgeRange = g_spConnBase->query("testdb.student", BSON("age" << BSON("$gte" << 20 << "$lte" << 22)));
	while (cursorAgeRange->more())
		cout << cursorAgeRange->next().toString() << endl;


	cout << "================================================"<< endl;
	cout << "* Find 20 <= age  <= 22 "<< endl;	
	cout << "* mongodb expression : db.student.find({\"age\":{\"$gte\":20, \"$lte\":22}}, {\"name\":1, \"age\":1})"<< endl;
	BSONObj bsoTmp = BSON("name" << 1 << "age" << 1);
	auto_ptr<DBClientCursor> cursorAgeRangeLs = g_spConnBase->query("testdb.student", BSON("age" << BSON("$gte" << 20 << "$lte" << 22)) , 0, 0, &bsoTmp);
	while (cursorAgeRangeLs->more()) 
		cout << cursorAgeRangeLs->next().toString() << endl;

	cout << "================================================"<< endl;
	cout << "* DBClientCursor to jk1 string"<< endl;	
	auto_ptr<DBClientCursor> cursorToJson = g_spConnBase->query("testdb.student", BSON("name" << "jk1"));
	while (cursorToJson->more())
	{
		cout << cursorToJson->next().jsonString() << endl;
	}
	// cout << "================================================"<< endl;
	// cout << "* Find 20 <= age  <= 22 "<< endl;	
	// auto_ptr<DBClientCursor> cursorAge_ = g_spConnBase->query("testdb.student", BSON("age" << BSON("$gte" << 20 << "$lte" << 22)), BSON("name"<<1<<"age"<<1));
	// while (cursorAgeRange->more())
	// cout << cursorAgeRange->next().toString() << endl;


	cout << "================================================"<< endl;
	cout << "* End "<< endl;	
	system ("read");
	return 0;
}

makefile:

mongoDemo : main.o 
	g++ -o mongoDemo main.o -L/usr/local/lib -lpthread -lpcap -lboost_thread -lboost_filesystem -lboost_program_options \
	 -lboost_regex -lmongoclient 

main : main.cpp 
	g++ -c main.cpp

clean:       
	rm -rf *.o common/*.o mongoDemo



你可能感兴趣的:(mongodb,查询,mongodb使用)