MongoDB dynamically updates a collection when changes occur in another collection

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?

+6
source share
1 answer

It looks like you are inserting an Object document inside a Project document, which may be fine, but this approach eliminates the need for your separate collection_Object collection. That is, collection_Object is redundant because each object (and not just the link) is actually stored inside the Project document as it is implemented.

For information on using embedded documents, see the documentation.

Alternatively, you can use links to documents .

The best approach to use depends on your specific use case.

+2
source

Source: https://habr.com/ru/post/985694/


All Articles