mongodb C++调用查询DEMO

这篇博客展示了如何在C++中进行MongoDB查询,提供了包含main.cpp源代码的简单DEMO,以及makefile构建说明。
摘要由CSDN通过智能技术生成


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 << "========================&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值