0

在我的 MongoDB 中,我有 Document,如下所示。如何更新内部文档结构中的文档如下所示:[{1},[{2}],[{3}],[{4}]].

例如:子文档中的子文档有objectId:1andobjectId:2objectId:3and objectId:4。喜欢:

[{ _id : ObjectId("1")}],
[{ _id : ObjectId("2")}], 
[{ _id : ObjectId("3")}],
[{ _id : ObjectId("4")}]


"Child": [{
    "_id": ObjectId("1"),
     Name: "Raghu",
     Age : 21,

    "subchild": [{
            "_id": ObjectId("2"),
            "Name": "Yuva",
             Age : 23,
        },
        [{
                "_id": ObjectId("3"),
                 Name: "Ravi",
                 Age : 25
            }
        ],
        [{
                 "_id": ObjectId("4"),
                "Name": "Ram",
                  Age : 27
            }
        ]
    ],
}

]

如何更新以下子文档特定_Id:ObjectId(“3”)和_Id:ObjectId(“4”)例如:[{“_id”:ObjectId(“4”),“Name”:“Ram”,}]

我在 C# 中尝试过这样的:

          I updated ObjectId : 1 like  Example   child.$.Subchild.0.Name="Raghu" 
        But not Updated ObjectId : 4 like  Example   child.$.Subchild.3.Name="Ram"           

    MongoServerSettings settings = new MongoServerSettings();
        settings.Server = new MongoServerAddress("127.1.1.1", 27017);
        MongoServer servers = new MongoServer(settings);
        var db = servers.GetDatabase("StudentDB");
        MongoCollection<Student> pax;
        pax = db.GetCollection<Student>("StudentDetails");
        IMongoQuery queryEdit = Query.EQ("Child.subchild._id", new ObjectId("4"));
        var updateValues = new List<UpdateBuilder>();
        { updateValues.Add(Update.Set("child.$.Subchild.3.Name", "Ramu")); }
        updateValues.Add(Update.Set("child.$.subchild.3.Age", 27));
        IMongoUpdate update = Update.Combine(updateValues);  
        SafeModeResult sm = pax.Update(queryEdit, update, UpdateFlags.None, SafeMode.True);
4

1 回答 1

0

child.$.Subchild.0.Name="Raghu"不需要对第一个数组游标的引用,需要child.Subchild.$.Name="Raghu"对 MongodDB 的第二个游标的引用不要更新两个引用“$”

db.foo.update({_id:ObjectId(...),"child.Subchild.Name":"Ram"},{$set:{child.Subchild.$.Name="Raghu"}});
于 2013-05-27T12:55:34.510 回答