Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB dynamic update of collection when changes occurs in another collection

I created two collections using Robomongo : collection_Project that contains documents like this

{
"_id" : ObjectId("5537ba643a45781cc8912d8f"),

"_Name" : "ProjectName",
"_Guid" : LUUID("16cf098a-fead-9d44-9dc9-f0bf7fb5b60f"),
"_Obj" : [ 
 ]
}

that I create with the 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 that contains documents like this

{
  "_id" : ObjectId("5537ba6c3a45781cc8912d90"),
  "AssociatedProject" : "ProjectName",
  "_Guid" : LUUID("d0a5565d-a0aa-7a4a-9683-b86f1c1de188"),
  "First" : 42,
  "Second" : 1000
}

That 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 update the documents of collection_Project with the 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 the document only in the collection_Object and see the change in the collection_Project as well ?

I tried to do that

 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 the collection_Project doesn't change.

Do you have any clue ?

like image 340
NicolasR Avatar asked Apr 22 '15 15:04

NicolasR


People also ask

How do I update all documents in MongoDB collection?

The updateMany () method updates all the documents in MongoDB collections that match the given query. When you update your document, the value of the _id field remains unchanged. This method can also add new fields in the document. Specify an empty document ({}) in the selection criteria to update all collection documents.

What is the use of updatemany in MongoDB?

The updateMany () method updates all the documents in MongoDB collections that match the given query. When you update your document, the value of the _id field remains unchanged. This method can also add new fields in the document. Specify an empty document ( {}) in the selection criteria to update all collection documents.

What if collation is not specified in MongoDB?

If the collation is unspecified but the collection has a default collation (see db.createCollection () ), the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

What are the two ID fields in MongoDB collection?

The two id fields in the both collection are indices. And the numbers of documents in these two collections are same. Show activity on this post. Mongodb is not relational database and doesn't support join's. So, you should think a little, do you really need mongodb for your purposes?


1 Answers

It looks like you are embedding the 'Object' document inside the 'Project' document, which might be fine, but that approach eliminates the need for your separate collection_Object collection. That is to say, collection_Object is redundant because each object (not just a reference) is actually stored inside the Project document as you have implemented it.

See the documentation for information on using embedded documents.

Alternatively, you could use document references.

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

like image 163
Sam Hiatt Avatar answered Oct 15 '22 11:10

Sam Hiatt