mongoc: Do not get $unwind to work with mongoDB c driver -
i have documents using 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 build same pipeline using mongoc driver looks $unwind entry 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 done wrong?
expected result:
{ "_id" : 1, "name" : "simplegroup" } { "_id" : 1, "name" : "simplegroup2" }
retrieved result:
{ "_id" : 1, "name" : [ "simplegroup", "simplegroup2" ] }
additional
i tried bcon_new , worked. how $unwind correctly inserted using bson_append_??? .
bson_t *pipeline; pipeline = bcon_new ("pipeline", "[", "{", "$unwind", "$c", "}", "{", "$project", "{", "name", "$c.p.name", "}", "}", "]");
the problem creation of pipeline array. each array element has key inserted "0" 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 aspect of bson_append_document_begin after bson_append_array_begin.
Comments
Post a Comment