Mongo-c-driver Update array

139 Views Asked by At

I want to have a set of unique elements in array. If there is no document, matching my query, it should be created. For example:

char items_1[][64] = {"cable", "wires", "optic"};
char tags_1[][64] =  {"electronics", "supplies", "camera", "accessories"};
bson_t *update = BCON_NEW("$addToSet", "{", "items","{", "$each", "[", items_1, "]","}", "}", "$addToSet", "{", "tags","{", "$each", "[", tags_1, "]","}", "}");
mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, update, &error);

After executing this, i should have the doc:

{
  _id: 1,
  items: [ "cable", "wires", "optic" ],
  tags: [ "electronics", "supplies", "camera", "accessories" ]
}

And I want to update it with the two arrays, by saving only unique items:

**items:["wires", "cords"], tags: ["supplies", "tv-sets"]**

char items_2[][64] = {"wires", "cords"};
char tags_2[][64] =  {"supplies", "tv-sets"};
bson_t *update = BCON_NEW("$addToSet", "{", "items","{", "$each", "[", items_2, "]","}", "}", "$addToSet", "{", "tags","{", "$each", "[", tags_2, "]","}", "}");
mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, update, &error);

The result should be:

{
  _id: 1,
  items: ["cable", "wires", "optic", "cords"],
  tags: [ "electronics", "supplies", "camera", "accessories", "tv-sets" ]
}

But after the first iteration not all the elements in arrays were added. The problem is with the "$addToSet". Please, explain me how to update and create arrays. Thank you.

0

There are 0 best solutions below