ho creato due collezioni che utilizzano Robomongo: collection_Project che contiene documenti come questoMongoDB aggiornamento dinamico di raccolta quando i cambiamenti si verifica in un'altra raccolta
{
"_id" : ObjectId("5537ba643a45781cc8912d8f"),
"_Name" : "ProjectName",
"_Guid" : LUUID("16cf098a-fead-9d44-9dc9-f0bf7fb5b60f"),
"_Obj" : [
]
}
che creo con la funzione
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);
}
e collection_Object che contiene documenti come questo
{
"_id" : ObjectId("5537ba6c3a45781cc8912d90"),
"AssociatedProject" : "ProjectName",
"_Guid" : LUUID("d0a5565d-a0aa-7a4a-9683-b86f1c1de188"),
"First" : 42,
"Second" : 1000
}
.210
che creo con la funzione
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);
aggiorno i documenti del collection_Project con la funzione
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);
}
in modo che i documenti in collection_Project simile a questa
{
"_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
}
]
}
Posso aggiornare il documento solo nel collection_Object e vedi anche la modifica nel collection_Project?
ho cercato di farlo
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);
}
ma il collection_Project non cambia.
Avete qualche idea?