aggregation on mongodb C Driver

34 views
Skip to first unread message

xiaok...@gmail.com

unread,
Aug 11, 2018, 1:03:51 PM8/11/18
to mongodb-user
Hello
some one can help to write following mongo shell request in using mongodb c driver ?

db.Daily.aggregate([
   {
      $project: {
         RECORDS: {
            $filter: {
               input: '$RECORDS',
               as: 'item',
               cond: {  $and: [
                 { $gte: [ '$$item.TM', 1215388800 ] },
                 { $lte: [  '$$item.TM', 1215734400 ] }
          ]}
            }
         }
      }
   }
])


your help is very appreciated
thanks
 xiaokuai

Wan Bachtiar

unread,
Aug 13, 2018, 9:52:47 PM8/13/18
to mongodb-user

some one can help to write following mongo shell request in using mongodb c driver ?

Hi Xiaokuai,

You can find a number of practical examples on libmongoc guides: Aggregation Framework to perform MongoDB aggregation pipeline using MongoDB C driver.

For example using the current version of the driver v1.12.0, your aggregation pipeline would look :

bson_t *pipeline;
pipeline = BCON_NEW("pipeline", "[", 
                        "{", "$project", "{", "RECORDS", 
                                "{", "$filter", 
                                    "{", "input", "$RECORDS", 
                                         "as", "item", 
                                         "cond", "{", "$and", "[", 
                                                 "{", "$gte", "[", "$$item.TM", BCON_INT32(1215388800), "]", "}",
                                                 "{", "$lte", "[", "$$item.TM", BCON_INT32(1215734400), "]", "}", 
                                         "]", "}", 
                    "}", "}", "}", "}", "]");
cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)){
    str = bson_as_canonical_extended_json(doc, NULL);
    printf("%s\n", str);
    bson_free(str);
}

If you have any further question, please provide:

  • MongoDB C driver version
  • A snippet code of what you’ve tried
  • Any error that you’re encountering

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages