1
我嘗試通過內部對象中的字段名稱來查找文檔。 我有內部的集合名稱「爲TestCollection」MongoDB C驅動程序mongoc_collection_find()找不到文檔按字段
{
"_id" : ObjectId("56c2f0f3892b312e740041a1"),
"Obj" : {
"Type" : 0,
"Num" : NumberLong(1111111111111)
}
}
基於此教程這個1號文件:http://api.mongodb.org/c/current/tutorial.html#find
而關於「尋找」這個API文檔我學習,我需要使用點表示法Obj.Num搜索 我試圖找到這個文件是這樣的:
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);
測試我在MongoDB的運行查詢外殼 ,我並取回文件的結果如你所見:
> use CollectionDB
switched to db CollectionDB
> db
Collection
> db.TestCollection.find(
... {
... "Obj.Num":1111111111111
... }
...)
{ "_id" : ObjectId("56c2f0f3892b312e740041a1"), "Obj" : { "Type" : 0, "Num" : NumberLong("1111111111111") } }
>
c代碼有什麼問題,它爲什麼不給我返回結果? 謝謝