MongoDB 当另一个集合发生变化时动态更新集合

MongoDB dynamic update of collection when changes occurs in another collection

我使用 Robomongo 创建了两个集合: collection_Project 包含这样的文档

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

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

我用函数

创建的
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);
    }

和 collection_Object 包含这样的文档

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

我用函数

创建的
 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);

我用函数

更新了collection_Project的文档
 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);
    }

以便 collection_Project 中的文档看起来像这样

{
"_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
    }
  ]
}

我可以仅在 collection_Object 中更新文档并在 collection_Project 中也看到更改吗?

我试过了

 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);
    }

但我 collection_Project 没有改变。

你有什么线索吗?

您似乎将 'Object' 文档嵌入到 'Project' 文档中,这可能没问题,但这种方法消除了对单独的 collection_Object 集合的需要。也就是说,collection_Object 是多余的,因为每个对象(不仅仅是引用)实际上都存储在您实现它的项目文档中。

有关使用 embedded documents 的信息,请参阅文档。

或者,您可以使用 document references

最佳使用方法取决于您的具体用例。