MongoDB C驱动程序 - 构建$ lte查询BSON?

时间:2014-07-03 10:37:34

标签: mongo-c-driver

如下所示,代码不起作用。我是关于MongoDB C Driver的新手。任何人都可以帮我纠正我的代码吗?非常感谢。

我想实施命令" {" _id":{$ lt:11832668}})。sort({" _id": - 1}&#34 ;

            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 );

2 个答案:

答案 0 :(得分: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);

希望有所帮助!

答案 1 :(得分: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