Frage

I can get my query results but I can not manage to retrieve them in ascending (or descending) order according to the value of my field "X" (int32). Could you help me?

Note that I use the latest version of MongoDB-C and in the old version I could very well use "$orderby" for my queries but in new version this function "bson_append_start_object()" dont exist.

Here's the structure part of my collection :

   by: [
      {
         id: ObjectId("XX"),
         type: NumberInt(1)
      } 
   ],
   timestamp: NumberInt()

and part of my code :

   bson_init(&array);
   bson_append_oid(&array, "id", 2, &oid);
   bson_append_int32(&array, "type", 4, 1);
   bson_init(&query);
   bson_append_document(&query, "by", 2, &array);

Thank you in advance.

Best regards.

War es hilfreich?

Lösung

You can still use $orderby in the new C MongooDB driver.

Instead of bson_append_start_object(), the new API uses bson_append_document_begin().

Sample usage from the GridFS example in the github repo:

  bson_init (&query);
  bson_append_document_begin (&query, "$orderby", -1, &child);
  bson_append_int32 (&child, "filename", -1, 1);
  bson_append_document_end (&query, &child);
  bson_append_document_begin (&query, "$query", -1, &child);
  bson_append_document_end (&query, &child);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top