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 ?
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.