2012-09-07 51 views
3

我試圖使用sort($ orderby)關鍵字對mongo集合進行查詢。

這裏是沒有$排序依據BSON查詢對象:

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

它的偉大工程和returnes 5000個結果。

當我(在這裏C-驅動例如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); 
... 

我結果。

我做錯了什麼?

回答

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

謝謝。它很棒! – eugenemail