I am attempting to convert the following MongoDB JSON command into valid BSON using the Mongo C driver:
db.test.update({
"_id" : ObjectId("5624200d4bacd3940b8b2d62"),
"folders.folder_id": "3_root",
"folders.files": { $elemMatch: { "file": "5BD252AD-10C9-4DCE-A59C-5E3223BDDC60"}} },
{$inc : { "folders.0.files.$.favorites.0.like": 1} }
);
I have attempted to create it using:
query = BCON_NEW ("_id", BCON_OID(&oid));
BSON_APPEND_UTF8 (query, "folders.folder_id", folderPositionRaw);
BSON_APPEND_UTF8 (query, "folders.files", "{" , "$elemMatch","{","file","5BD252AD-10C9-4DCE-A59C-5E3223BDDC60","}","}");
update = BCON_NEW ("$inc",
"{",
"folders.0.files.$.favorites.0.like", 1,
"}");
mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);
But it is way wrong.
When I compile I get:
/current/set_fav.c: In function ‘main’:
/current/set_fav.c:102: warning: initialization discards qualifiers from pointer target type
/current/set_fav.c:168:122: error: macro "BSON_APPEND_UTF8" passed 9 arguments, but takes just 3
/current/set_fav.c:168: error: ‘BSON_APPEND_UTF8’ undeclared (first use in this function)
/current/set_fav.c:168: error: (Each undeclared identifier is reported only once /current/set_fav.c:168: error: for each function it appears in.)
FYI: folderPositionRaw
has the value of 3_root
set higher up in the codes.
To add a little more information: Originally I had the wrong Mongo syntax, but C was happy. I was using the following:
query = bson_new ();
bson_oid_init_from_string (&oid, tribe_id);
query = BCON_NEW ("_id", BCON_OID(&oid));
BSON_APPEND_UTF8 (query, "folders.folder_id", folderPositionRaw);
BSON_APPEND_UTF8 (query, "folders.folder_id.$.files.file", file);
I received help from my DB team giving me the correct Mongo syntax (top of post) and in my attempt to structure that into the C BSON I did something wrong.
Some progress:
I have updated my codes and they NO longer crash which is a good sign, but the output JSON that is sent to Mongo is not fully structured correctly. I think the issue is the value 1 that is sent in the mongo $inc command is being sent as a scalar 1 not an int 1. I have tried both with and without quotes around the 1. With quotes the code runs, but does not preform the update (no errors returned). If I remove the quotes the app crashes.
query = bson_new ();
bson_oid_init_from_string (&oid, tribe_id);
query = BCON_NEW ("_id", BCON_OID(&oid), "folders.folder_id", folderPositionRaw,"folders.files",
"{",
"$elemMatch",
"{",
"file", file,
"}",
"}");
// Find the document
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
// update document
update = BCON_NEW ("$inc",
"{",
"folders.0.files.$.favorites.0.like","1",
"}");
str = bson_as_json (update, NULL);
printf ("***-> %s <-***\n\n",str);
mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);
Found the issue!
You cannot use a standard int
it must be a BCON_INT
update = BCON_NEW ("$inc",
"{",
"folders.0.files.$.favorites.0.like",BCON_INT32 (1),
"}");
:)