Sorting by subfloors in Mongo C-Driver

We are trying to create a query to programmatically get the ordered collection cursor. There is one single example given on the mongodb website and it doesn't even work.

We are trying to sort our collection with two fields, which we named timestamp.seconds and timestamp.nanoseconds . Our collection is indexed by these fields and we can sort the data using the following code in the mongo shell:

db.Data.find().sort({"timestamp.seconds": 1, "timestamp.nanoseconds": 1})

      

How can we create the same request using the C driver? We tried the code below and it doesn't work as we expected.

mongoc_cursor_t *cursor;
bson_t *query;

query = BCON_NEW("$query", "{", "}", "$orderby", "{",
                 "timestamp.seconds: 1, timestamp.nanoseconds: 1", "}");
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0,
                                 query, NULL, NULL);

      

+3


source to share


1 answer


You are not using the correct syntax when creating your request with BCON_NEW

:



query = BCON_NEW("$query", "{", "}",
                 "$orderby", "{",
                                  "timestamp.seconds",     BCON_INT32(1),
                                  "timestamp.nanoseconds", BCON_INT32(1),
                        //        ^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^
                        //                key                  value
                             "}");

      

+2


source







All Articles