MongoDB笔记一,Win10上使用C Driver访问MongoDB Server

Author: kagula
Date: 2018-08-30
Environment:
[1]Visual Studio 2013 Update 5
[2]mongodb-win32-x86_64-2008plus-ssl-3.4.7-signed.msi
[3]MongoDB C Driver 1.7.0


Introduction
  MongoDB以Json对象为单位存取数据。
  
    因为我们是VS2013,不满足文档上写的Mongocxx 3.1.x最低VS2015的build需求,决定 
使用MongoDB C Driver.
  
  使用MongoDB的时候我把 collection看作是关系数据库的表, document看作关系数据库的记录。

第一部分:安装MongoDB
第二部分:MongoDB基本操作
第二部分:新建个C++项目访问MongoDB Server


Content
第一部分:安装MongoDB
Step1:

从官网下载
mongodb-win32-x86_64-2008plus-ssl-3.4.7-signed.msi
安装后,默认路径
C:\Program Files\MongoDB\Server\3.4

Step2:新建“d:\mongodb\data”和““d:\mongodb\log”路径”

Step3:启动MongoDB,并指定DB数据和Log存放位置,
"C:\Program Files\MongoDB\Server\3.4\bin\ mongod.exe" --dbpath d:\mongodb\data -v --logpath d:/mongodb/log/server1.log

Final Step:
使用下面的命令,测试MongoDB服务器是否能够连接上
"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe"
出现提示符后输入exit命令推出MongoDB客户端

第二部分:MongoDB基本操作
//下面的命令链接到本地数据库
"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe"

//使用数据库kagula,若kagula数据库不存在,创建它。
use kagula

//查看当前用了哪个数据库
db

//列出现在哪些数据库
show dbs
创建的数据库kagula不在列表中。要显示数据库,需要至少插入一个文档,空的数据库是不显示出来的。

//当前数据库中插入记录
db.items.insert({"id":1,"name":"lijun"})

//新建集合myCollection(如果集合不存在的话),往集合里插入json对象。
db.myCollection.insert({"id":1,"name":"record1","value":[{"sub-name":"a"},{"sub-name":"b"}]})
db.myCollection.insert({"id":2,"name":"record2","value":[{"sub-name":"2a"},{"sub-name":"2b"}]})
db.myCollection.insert({"id":3,"name":"record3","value2":[12.34,34,9.0]})

//删除数据
db.myCollection.remove({'id':3})

//更新对象某个属性
db.myCollection.update({'id':1},{$set:{'name':'name1 after update'}})

//查询数据-等于比较
db.myCollection.find({"id":2}).pretty()
db.myCollection.find({"id":2,"name":"record1"}).pretty()
db.myCollection.find({"id":2,"name":"record2"}).pretty()

//查询数据-小于,大于,大于等于比较
db.myCollection.find({"id":{$lt:1}}).pretty()
db.myCollection.find({"id":{$gt:1}}).pretty()
db.myCollection.find({"id":{$gte:1}}).pretty()

第三部分:使用C++连接MongoDB
Step1:
准备 mongo c driver.
下载mongo-c-driver-1.7.0.tar.gz,并解压缩到D:\SDK\mongo-c-driver-1.7.0.


cmake
D:\SDK\mongo-c-driver-1.7.0\src\libbson
注意:generate之前CMAKE_INSTALL_PREFIX设为“D:\SDK\mongo-c-driver-1.7.0”
注意打开solution后install project必须最后build,它会把编译好的project,install到
刚才我们用 CMAKE_INSTALL_PREFIX指定的路径中.
如果只是调用的话,只要build release版本的库就够了。

cmake
D:\SDK\mongo-c-driver-1.7.0
注意:generate之前 CMAKE_INSTALL_PREFIX设为“D:\SDK\mongo-c-driver-1.7.0”
打开solution后,build all成功。

Step2:
vs2013里新建windows console程序
源代码如下:
#include 

int main(int argc, char *argv[])
{
	const char *uri_str = "mongodb://localhost:27017";
	mongoc_client_t *client;
	mongoc_database_t *database;
	mongoc_collection_t *collection;
	bson_t *insert;
	bson_error_t error;

	mongoc_init();
	client = mongoc_client_new(uri_str);

	mongoc_client_set_appname(client, "connect-example");

	/*
	* Get a handle on the database "kagula" and collection "myCollection"
	*/
	database = mongoc_client_get_database(client, "kagula");
	collection = mongoc_client_get_collection(client, "kagula", "myCollection");

	//json对象  {"name","kagula"}
	insert = BCON_NEW("name", BCON_UTF8("kagula"));

	//
	if (!mongoc_collection_insert(
		collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {
		fprintf(stderr, "%s\n", error.message);
	}

	bson_destroy(insert);

	/*
	* Release our handles and clean up libmongoc
	*/
	mongoc_collection_destroy(collection);
	mongoc_database_destroy(database);
	mongoc_client_destroy(client);
	mongoc_cleanup();

	return 0;
}

在project, All Configuration下做如下设置
Include Path:
D:\SDK\mongo-c-driver-1.7.0\include\libbson-1.0;D:\SDK\mongo-c-driver-1.7.0\include\libmongoc-1.0;

Library Path:
D:\SDK\mongo-c-driver-1.7.0\lib;

Additional Dependencies:
bson-1.0.lib;mongoc-1.0.lib;


Final Step:
把上次编译好的两个release版本 libbson-1.0.dlllibmongoc-1.0.dll放到到你project的Debug路径下
编译通过后,运行测试MongoDB的工程成功。
使用MongoDB客户端使用下面的命令,验证数据是否已经插入到数据库中
use kagula
db.myCollection.find()

note:
[1]你也可以从下面位置下载libbson
https://github.com/mongodb/libbson/releases
使用cmake分别编译出Debug、Release版本的库。
[2]mongo-c-driver带的示例代码在D:\SDK\mongo-c-driver-1.7.0\examples路径下

Reference
[1]https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
[2]https://docs.mongodb.com/manual/crud/
[3]https://docs.mongodb.com/manual/applications/drivers/
[4]http://mongoc.org/libmongoc/current/tutorial.html

你可能感兴趣的:(C++,数据库)