我尝试使用sort($ orderby)关键字对mongo集合进行查询。
这是没有$ orderby的bson-query对象:
bson cmd;
bson_init( &cmd );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );
bson_finish( &cmd );
mongo_find( conn, collection, cmd, bson_empty( &empty ), 0, 0, MONGO_SLAVE_OK );
效果很好,返回5000个结果。
当我添加$ orderby对象时(比如c-driver示例http://api.mongodb.org/c/current/tutorial.html#complex-queries):
bson cmd;
bson_init( &cmd );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );
bson_append_start_object( &cmd, "$orderby" );
bson_append_int( &cmd, "timestamp", 1 );
bson_append_finish_object( &cmd );
bson_finish( &cmd );
...
我有 0 结果。
我做错了什么?
答案 0 :(得分:1)
尝试:
bson cmd;
bson_init( &cmd );
bson_append_start_object( &cmd, "$query" );
bson_append_string( &cmd, "ip", ip.c_str() );
bson_append_start_object( &cmd, "timestamp" );
bson_append_long( &cmd, "$gt", 100 );
bson_append_finish_object( &cmd );
bson_append_finish_object( &cmd );
bson_append_start_object( &cmd, "$orderby" );
bson_append_int( &cmd, "timestamp", 1 );
bson_append_finish_object( &cmd );
bson_finish( &cmd );