我正在尝试连接到mongodb并插入GET参数,使用G-WAN和mongodb的C驱动程序,我成功地连接到了mongodb,但我还没有成功插入任何数据。我正在使用代码
mongo_write_concern_init(write_concern);
write_concern->w = 0;
mongo_write_concern_finish(write_concern);
bson b[1];
bson_init( b );
bson_append_new_oid( b, "_id" );
bson_append_string( b, "param1", param1);
bson_append_string( b, "param2", param2);
status = mongo_insert( conn, "mydb.mycol", b , write_concern);
bson_finish( b );
bson_destroy( b );
mongo_write_concern_destroy(write_concern);
连接成功,我可以通过mongod.log文件查看;
[conn36] run command admin.$cmd { ismaster: 1 }
[conn36] command admin.$cmd command: { ismaster: 1 } ntoreturn:1 reslen:71 0ms
[conn36] end connection 127.0.0.1:50086
但除此之外,当我调用最后一个错误时,我无法在mongodb shell上获得任何错误消息或错误日志
> db.getLastError()
null
返回null欢迎您知道为什么会发生这种情况或提出任何解决方案,谢谢
此调用必须在mongo_insert():之前
bson_finish( b );
否则,这里有一个未完成的BSON对象:
status = mongo_insert( conn, "mydb.mycol", b , write_concern);
所以代码应该是
bson b[1];
/// Init
bson_init( b );
bson_append_new_oid( b, "_id" );
bson_append_string( b, "param1", param1);
bson_append_string( b, "param2", param2);
// Make this complete
bson_finish( b );
/// Insert
status = mongo_insert( conn, "mydb.mycol", b , write_concern);
/// Destroy the BSON obj
bson_destroy( b );