I created two collections using Robomongo: collection_Project, which contains such documents
{ "_id" : ObjectId("5537ba643a45781cc8912d8f"), "_Name" : "ProjectName", "_Guid" : LUUID("16cf098a-fead-9d44-9dc9-f0bf7fb5b60f"), "_Obj" : [ ] }
which I create using a 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, which contains such documents
{ "_id" : ObjectId("5537ba6c3a45781cc8912d90"), "AssociatedProject" : "ProjectName", "_Guid" : LUUID("d0a5565d-a0aa-7a4a-9683-b86f1c1de188"), "First" : 42, "Second" : 1000 }
What I create with the 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 am updating collection_Project documents with 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 that the documents in collection_Project look like this:
{ "_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 I update a document only in the collection_Object file and see the change in the Project_ collection?
I tried to do it
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 I collection_Project does not change.
Do you have any hints?