问题描述
我试图找到这里面的对象字段名的文件。
我有集合名称这里面的1号文件为TestCollection
{
_id:的ObjectId(56c2f0f3892b312e740041a1),
的Obj:{
类型:0,
民:NumberLong(1111111111111)
}
}
根据这个教程:结果
而关于这个API文档中找到我知道我需要使用点符号由Obj.Num搜索
我试图找到这个文件是这样的:
mongoc_collection_t *收集= NULL;
mongoc_cursor_t *光标= NULL;
常量bson_t *文档= NULL;
字符*海峡= NULL;
bson_t *查询= bson_new();
性病::字符串str;
布尔B = BSON_APPEND_INT64(查询,TestCollection.Num1111111111111);
// b为真
集合= mongoc_client_get_collection(m_mogoClientCollectionDB,Obj.Num);
光标= mongoc_collection_find(收集,MONGOC_QUERY_NONE,0,0,0,查询,NULL,NULL);
而(mongoc_cursor_next(光标,&安培; DOC)){
//这是从来没有在这里得到打印的文档
海峡= bson_as_json(DOC,NULL);
的printf(%S \\ n,STR);
bson_free(STR);
}bson_destroy(查询);
mongoc_cursor_destroy(光标);
mongoc_collection_destroy(集合);
测试查询我在MongoDB的shell中运行它
而且我也当你看到找回文档的结果:
>使用CollectionDB
切换到DB CollectionDB
>分贝
采集
> db.TestCollection.find(
... {
......Obj.Num:1111111111111
...}
...)
{_id:的ObjectId(56c2f0f3892b312e740041a1),的Obj:{类型0,货号:NumberLong(1111111111111)}}>
什么是错在c code,为什么不给我回的结果?
谢谢
布尔B = BSON_APPEND_INT64(查询,Obj.Num1111111111111);
集合= mongoc_client_get_collection(m_mogoClientCollectionDB,为TestCollection);
光标= mongoc_collection_find(收集,MONGOC_QUERY_NONE,0,0,0,查询,NULL,NULL);
希望你现在想通了。
在BSON B中的点号(一号线)需要的字段命名
集合名(线路2及在你的shell code引用)是集合名称命名空间的混搭。它应该包含数据库的名称,只有集合名称。
i try to find document by field name which is inside object .i have this 1 document inside collection names "TestCollection"
{
"_id" : ObjectId("56c2f0f3892b312e740041a1"),
"Obj" : {
"Type" : 0,
"Num" : NumberLong(1111111111111)
}
}
Based on this tutorial : http://api.mongodb.org/c/current/tutorial.html#find
And this API doc about "find" i learn that i need to use dot notation to search by Obj.NumI try to find this document like this :
mongoc_collection_t *collection = NULL;
mongoc_cursor_t *cursor = NULL;
const bson_t *doc = NULL;
char *str = NULL;
bson_t * query = bson_new ();
std::string str;
bool b = BSON_APPEND_INT64(query,"TestCollection.Num",1111111111111);
// b is true
collection = mongoc_client_get_collection(m_mogoClient,"CollectionDB","Obj.Num");
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
//it is never gets here to print the document
str = bson_as_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (query);
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (collection);
to test the query i run it in the mongoDB shell and i did get back the document result as you see :
> use CollectionDB
switched to db CollectionDB
> db
Collection
> db.TestCollection.find(
... {
... "Obj.Num":1111111111111
... }
... )
{ "_id" : ObjectId("56c2f0f3892b312e740041a1"), "Obj" : { "Type" : 0, "Num" : NumberLong("1111111111111") } }
>
what is wrong in the c code , why it is not giving me back result ?thanks
bool b = BSON_APPEND_INT64(query,"Obj.Num",1111111111111);
collection = mongoc_client_get_collection(m_mogoClient,"CollectionDB","TestCollection");
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
Hopefully you figured it out by now.
the dot notation in bson b (line1) needs to be the namespace of the field
the Collection name (line 2 & referenced in your shell code) was mashup of the collection name and the namespace. It should be contain the database name and the collection name only.
这篇关于MongoDB的C驱动mongoc_collection_find()无法通过现场找到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!