mongodb c driver的使用总结(1)
2016-09-07 11:15
393 查看
mongodb c driver的使用总结(1)
非线程安全,只需调用一次
mongoc_init();
const char* log_domain, const char* message, void* user_data)
{
cout << "[mongo][" << log_domain << "]" << message;
}
mongoc_log_set_handler (log_handler, NULL);
mongoc_client_t* m_pClient = mongoc_client_new(uristr);
mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");
char* str = bson_as_json(doc, NULL);
fprintf(stdout, "%s\n", str);
6、插入记录
bson_error_t error;
bson_t *doc = bson_new();
BSON_APPEND_INT64(doc, "id", 1);
BSON_APPEND_INT64(doc, "field1", 0);
string msg = "test message";
BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, (const uint8_t*)(msg.c_str()), (uint32_t)(msg.size()));
bool r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error);
if (!r)
{
cout << "Insert Failure:" << error.message;
}
bson_destroy(doc);
bson_t *doc = bson_new();
bson_t child;
bson_append_document_begin(doc, "$set", -1, &child);
BSON_APPEND_INT64(&child, "field1", 22);
bson_append_document_end(doc, &child);
bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query, "id", 1);
bool r = mongoc_collection_update(m_pCollection,
MONGOC_UPDATE_NONE,
&query,
doc,
NULL,
&error);
if (!r)
{
cout << "Update Failure: " << error.message;
}
bson_destroy(&query);
bson_destroy(doc);
bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query, "id", 1);
bool r = mongoc_collection_delete(m_pCollection,
MONGOC_DELETE_NONE,
&query,
NULL,
&error);
if (!r)
{
cout << "Delete Failure: " << error.message;
ret = ERR_MONGODB_FAILED;
}
bson_destroy(&query);
1、初始化mongoc
非线程安全,只需调用一次mongoc_init();
2、设置日志回调
static void log_handler (mongoc_log_level_t log_level,const char* log_domain, const char* message, void* user_data)
{
cout << "[mongo][" << log_domain << "]" << message;
}
mongoc_log_set_handler (log_handler, NULL);
3、连接mongodb
const char *uristr = "mongodb://127.0.0.1/";mongoc_client_t* m_pClient = mongoc_client_new(uristr);
4、获取collection
mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");5、打印bson调试
MongoDB使用了BSON这种结构来存储数据和网络数据交换。 mongoc提供了方法将bson格式转化为json, 可以用于打印调试 。char* str = bson_as_json(doc, NULL);
fprintf(stdout, "%s\n", str);
6、插入记录
bson_error_t error;bson_t *doc = bson_new();
BSON_APPEND_INT64(doc, "id", 1);
BSON_APPEND_INT64(doc, "field1", 0);
string msg = "test message";
BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, (const uint8_t*)(msg.c_str()), (uint32_t)(msg.size()));
bool r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error);
if (!r)
{
cout << "Insert Failure:" << error.message;
}
bson_destroy(doc);
7、更新记录
bson_error_t error;bson_t *doc = bson_new();
bson_t child;
bson_append_document_begin(doc, "$set", -1, &child);
BSON_APPEND_INT64(&child, "field1", 22);
bson_append_document_end(doc, &child);
bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query, "id", 1);
bool r = mongoc_collection_update(m_pCollection,
MONGOC_UPDATE_NONE,
&query,
doc,
NULL,
&error);
if (!r)
{
cout << "Update Failure: " << error.message;
}
bson_destroy(&query);
bson_destroy(doc);
8、删除记录
bson_error_t error;bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query, "id", 1);
bool r = mongoc_collection_delete(m_pCollection,
MONGOC_DELETE_NONE,
&query,
NULL,
&error);
if (!r)
{
cout << "Delete Failure: " << error.message;
ret = ERR_MONGODB_FAILED;
}
bson_destroy(&query);
相关文章推荐
- mongo replSet副本集方式配置、.net core使用MongoDB.Driver创建数据库连接池总结
- mongodb使用总结1
- Mongodb matlab Driver 使用简介
- mongoDB 使用总结
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用
- mongodb使用总结1
- 使用mongo-java-driver3.0.2.jar和mongodb3.0在java代码中的用户验证4
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用
- 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用
- mongodb-driver-async 如何使用GridFS
- 使用webdriver的几点粗浅总结
- mongodb安装和mongo-c-driver的安装使用
- MongoDB使用总结系列2
- mongodb c++ driver 2.0编译使用
- Mongodb c Driver 使用实例
- MongoDB使用总结系列1
- MongoDB Replica Set使用几点总结
- Mongodb学习一,开始使用C# Driver操作Mongodb
- Mongodb-使用javaDriver 实现增删改查