我正在尝试使用 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个结果。
我做错了什么?