MongoDB dynamic update of collection when changes occurs in another collection -
i created 2 collections using robomongo : collection_project contains documents this
{ "_id" : objectid("5537ba643a45781cc8912d8f"), "_name" : "projectname", "_guid" : luuid("16cf098a-fead-9d44-9dc9-f0bf7fb5b60f"), "_obj" : [ ] }
that create function
public static void createproject(string projectname) { mongoclient client = new mongoclient("mongodb://localhost/testcreationmongo"); var db = client.getserver().getdatabase("testmongo"); var collection = db.getcollection("collection_project"); var project = new project { _name = projectname, _guid = guid.newguid(), _obj = new list<c_object>() }; collection.insert(project); }
and collection_object contains documents this
{ "_id" : objectid("5537ba6c3a45781cc8912d90"), "associatedproject" : "projectname", "_guid" : luuid("d0a5565d-a0aa-7a4a-9683-b86f1c1de188"), "first" : 42, "second" : 1000 }
that create function
public static void createobject(c_object toadd) { mongoclient client = new mongoclient("mongodb://localhost/testcreationmongo"); var db = client.getserver().getdatabase("testmongo"); var collection = db.getcollection("collection_object"); collection.insert(toadd);
i update documents of collection_project function
public static void addobjtoproject(c_object objtoadd, string associatedproject) { mongoclient client = new mongoclient("mongodb://localhost/testcreationmongo"); var db = client.getserver().getdatabase("testmongo"); var collection = db.getcollection<project>("collection_project"); var query = query.eq("_name", associatedproject); var update = update.addtosetwrapped<c_object>("_obj", objtoadd); collection.update(query, update); }
so documents in collection_project
{ "_id" : objectid("5537ba643a45781cc8912d8f"), "_name" : "projectname", "_guid" : luuid("16cf098a-fead-9d44-9dc9-f0bf7fb5b60f"), "_obj" : [ { "_id" : objectid("5537ba6c3a45781cc8912d90"), "associatedproject" : "projectname", "_guid" : luuid("d0a5565d-a0aa-7a4a-9683-b86f1c1de188"), "first" : 42, "second" : 1000 } ] }
can update document in collection_object , see change in collection_project ?
i tried
public static void updateobject(c_object toupdate) { mongoclient client = new mongoclient("mongodb://localhost/testcreationmongo"); var db = client.getserver().getdatabase("testmongo"); var collection = db.getcollection("collection_object"); var query = query.eq("_guid", toupdate._guid); var update = update.replace<c_object>(toupdate); collection.update(query, update); }
but collection_project doesn't change.
do have clue ?
it looks embedding 'object' document inside 'project' document, might fine, approach eliminates need separate collection_object collection. say, collection_object redundant because each object (not reference) stored inside project document have implemented it.
see documentation information on using embedded documents.
alternatively, use document references.
the best approach use depends on specific use case.
Comments
Post a Comment