参考
https://blog.csdn.net/x_xx_xxx_xxxx/article/details/79414186
这里我们使用mongo-c-driver
1、下载mongo-c-driver源码文件:
https://github.com/mongodb/mongo-c-driver.git
2、下载`libbson的源码
https://github.com/mongodb/libbson.git
3、编译libbson(此步骤可以省略)
mkdir msvc15
cd msvc15
cmake -DCMAKE_INSTALL_PREFIX=D:\lib\mongo-c-driver\usr -G "Visual Studio 15 2017" ..
打开.sln文件
编译,结果如下:
4、编译mongo-c-driver
mkdir msvc15
cd msvc15
cmake -DBUILD_VERSION="1.15.1" -DCMAKE_INSTALL_PREFIX=D:\lib\mongo-c-driver\usr -DBSON_ROOT_DIR=D:\lib\mongo-c-driver\usr -G "Visual Studio 15 2017" ..
看到
编译完在mongo-c-driver-1.15.1\msvc15\src\libmongoc\Debug目录下看到lib和dll库
全部使用mongo-c-driver-1.15.1\msvc15\src\libbson\Debug即可。
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
mongoc_database_t *database;
mongoc_collection_t *collection;
bson_t *command,
reply,
*insert;
bson_error_t error;
char *str;
bool retval;
/*
* Required to initialize libmongoc's internals
*/
mongoc_init ();
/*
* Create a new client instance
*/
client = mongoc_client_new ("mongodb://localhost:27017");
/*
* Register the application name so we can track it in the profile logs
* on the server. This can also be done from the URI (see other examples).
*/
mongoc_client_set_appname (client, "connect-example");
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
database = mongoc_client_get_database (client, "db_name");
collection = mongoc_client_get_collection (client, "db_name", "coll_name");
/*
* Do work. This example pings the database, prints the result as JSON and
* performs an insert
*/
command = BCON_NEW ("ping", BCON_INT32 (1));
retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error);
if (!retval) {
fprintf (stderr, "%s\n", error.message);
return EXIT_FAILURE;
}
str = bson_as_json (&reply, NULL);
printf ("%s\n", str);
insert = BCON_NEW ("hello", BCON_UTF8 ("world"));
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (insert);
bson_destroy (&reply);
bson_destroy (command);
bson_free (str);
/*
* Release our handles and clean up libmongoc
*/
mongoc_collection_destroy (collection);
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
参考
https://blog.csdn.net/doitsjz/article/details/73920255
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
int
main (int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
const bson_t *doc;
bson_t *query;
char *str;
mongoc_init ();
client = mongoc_client_new (
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection (client, "mydb", "mycoll");
query = bson_new ();
BSON_APPEND_UTF8 (query, "hello", "world");
cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
str = bson_as_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (query);
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
使用mongoc driver读写mongoDB
https://my.oschina.net/lCQ3FC3/blog/1555465