I'm trying to make a query to mongo collection using sort ($orderby) keyword.
Here is bson-query object without $orderby:
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 );
It works great and returnes 5000 results.
When I add $orderby object (like in c-driver example here 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 );
...
I have 0 results.
What I do wrong?
Try:
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 );