Search code examples
mongodbmongo-c-driver

mongoc: Do not get $unwind to work with mongoDB c driver


I have documents like this using an array of subdocuments "c".

   {
      "_id" : 1,
      "c" : [
        {
          "p" : {
            "name" : "SimpleGroup"
          }
        },
        {
          "p" : {
            "name" : "SimpleGroup2"
          }
        }
      ]
    }

Running

db.collection.aggregate([ { "$unwind" : "$c" }, { "$project" : { "name" : "$c.p.name" } } ])

works fine in command line. If I build the same pipeline using mongoc driver it looks like $unwind entry is ignored.

#include <bson.h>
#include <mongoc.h>

bson_t pipeline; bson_init(&pipeline);
bson_t pipeArr, pipeElem1, pipeElem2;
bson_append_array_begin(&pipeline, "pipeline", -1, &pipeArr);

bson_append_document_begin(&pipeArr, "0", -1, &pipeElem1);
bson_append_utf8(&pipeElem1, "$unwind", -1, "$c", -1);
bson_append_document_end(&pipeArr, &pipeElem1);

bson_append_document_begin(&pipeArr, "0", -1, &pipeElem1);
bson_append_document_begin(&pipeElem1, "$project", -1, &pipeElem2);
bson_append_utf8(&pipeElem2, "name", -1, "$c.p.name", -1);
bson_append_document_end(&pipeElem1, &pipeElem2);
bson_append_document_end(&pipeArr, &pipeElem1);

bson_append_array_end(&pipeline, &pipeArr);

cursor = mongoc_collection_aggregate (coll, MONGOC_QUERY_NONE, &pipeline, nullptr, nullptr);

What is done wrong?

Expected result:

{ "_id" : 1, "name" : "SimpleGroup" }
{ "_id" : 1, "name" : "SimpleGroup2" }

Retrieved result:

{ "_id" : 1, "name" : [ "SimpleGroup", "SimpleGroup2" ] }

Additional

I tried with BCON_NEW and it worked. How is $unwind correctly inserted using bson_append_??? .

bson_t *pipeline;

pipeline = BCON_NEW ("pipeline", "[",
  "{", "$unwind", "$c", "}",
  "{", "$project", "{", "name", "$c.p.name", "}", "}",
  "]");

Solution

  • The problem is the creation of the pipeline array. Each array element has a key but I always inserted "0" which causes overwrite.

    bson_t pipeline; bson_init(&pipeline);
    bson_t pipeArr, pipeElem1, pipeElem2;
    bson_append_array_begin(&pipeline, "pipeline", -1, &pipeArr);
    
    bson_append_document_begin(&pipeArr, "0", -1, &pipeElem1);
    bson_append_utf8(&pipeElem1, "$unwind", -1, "$c", -1);
    bson_append_document_end(&pipeArr, &pipeElem1);
    
    bson_append_document_begin(&pipeArr, "1", -1, &pipeElem1);
    bson_append_document_begin(&pipeElem1, "$project", -1, &pipeElem2);
    bson_append_utf8(&pipeElem2, "name", -1, "$c.p.name", -1);
    bson_append_document_end(&pipeElem1, &pipeElem2);
    bson_append_document_end(&pipeArr, &pipeElem1);
    
    bson_append_array_end(&pipeline, &pipeArr);
    

    I missed this aspect of bson_append_document_begin after bson_append_array_begin.