Reputation: 938
Good Morning,
I am pretty new to MongoDB and using the c-driver so this question may seem trivial to some, but I am at a complete loss.
I have a document that has an array called 'folders' and inside each element of folders is a secondary array titled 'files'. I wish to add an entry into 'files' for a specific folder's array.
I have tried to create a BCON using $push in c, but I know I have completely screwed it all up. I get no compilation errors and I can run the application, but nothing happens to the document. I think my application aborts right at the time of this code because there is a printf just after it that fails to print, but if I remove the 'update = ' and 'mongoc_collection_update' lines from the code everything executes correctly. So I think my problem is the actual bcon structure is wrong and causing a fault on exec.
Sample Document
{
"_id" : ObjectId("5627e20d4bacefccf4864e4e"),
"allow_save" : "true",
"allow_add" : "true",
"auto_approve_invites" : "true",
"folders" : [
{
"folder_id" : "root",
"display_name" : "My Folder",
"files" : [
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
},
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
},
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
}
]
},
{
"folder_id" : "1",
"display_name" : "My Other Folder",
"preview_thumbnail" : "58656607-801b-40e4-aa34-e01ef1def85b",
"files" : [
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
},
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
},
{
"modified" : "",
"file" : "58656607-801b-40e4-aa34-e01ef1def85b"
}
]
}
]
}
Code Sample
collection = mongoc_client_get_collection (client, "***", "***");
update = bson_new ();
bson_oid_t oid;
bson_oid_init_from_string (&oid, tribe_id);
update = BCON_NEW ("_id", BCON_OID(&oid));
// Find the document
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
// add new file
update = BCON_NEW ("$push",
"{","folders.folder_id", BCON_UTF8 ("root"), "}",
"{","new_file", "new value", "}");
mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);
Upvotes: 0
Views: 841
Reputation: 938
update = BCON_NEW ("$push", "{", "folders.0.files", "{", "file", BCON_UTF8 ("XXXXXXXXX"), "modified", BCON_UTF8 (""), "}", "}");
Solution found.
Upvotes: 1