2

如下,代码不起作用。我是 MongoDB C Driver 的新手。谁能帮我更正我的代码?非常感谢。

我想实现命令 "{"_id":{$lt:11832668}}).sort({"_id":-1}"。

            bson        laoquery[1];
            memset( laoquery, 0, sizeof( laoquery ) );


            bson_init( laoquery );
                    bson_append_start_object( laoquery, "&lte" );
                            bson_append_long( laoquery, "_id", 11832668 );
                    bson_append_finish_object( laoquery );

                    bson_append_start_object( laoquery, "$orderby" );
                        bson_append_int( laoquery, "_id", -1);
                    bson_append_finish_object( laoquery );

            bson_finish( laoquery );
4

2 回答 2

1

首先,我建议您使用最新版本的 MongoDB C 驱动程序。这似乎正在使用旧版驱动程序。

使用新驱动程序(在许多新功能和性能改进中),您可以使用 BCON 来构造查询。

bson_t *query;
mongoc_cursor_t *cursor;
const bson_t *doc;

query = BCON_NEW (
   "$query", "{", "_id", "{", "$lt", BCON_INT32 (11832688), "}", "}",
   "$orderby", "{", "_id", BCON_INT32 (-1), "}"
);

cursor = mongoc_collection_find (collection, 0, 0, 0, 0, query, NULL, NULL);

while (mongoc_cursor_next (cursor, &doc)) {
   /* do something with doc */
}

mongoc_cursor_destroy (cursor);
bson_destroy (query);

希望有帮助!

于 2014-07-10T04:11:36.400 回答
0

使用 mongoc_collection_find_with_opts 你可以:

 filter = BCON_NEW ("_id", "{", "$lt", BCON_INT32 (11832668), "}");
 opts = BCON_NEW ("sort", "{", "_id", BCON_INT32 (-1), "}");
 mongoc_cursor_t *cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);

源代码和示例在这里http://mongoc.org/libmongoc/current/mongoc_collection_find_with_opts.html

于 2017-03-20T10:14:58.497 回答