mongoDB Rename embedded field

感情迁移 提交于 2019-12-10 17:26:48

问题


how do we rename embedded fields using C# with mongoDB ? An example of document Person would be:

{
Id: 1,
LastName: "Smith",
FirstName: "John",
Orders: {
         Id: 1,
         Name: "Trousers" // I want to rename **Name** into **Something**
    }
}

With mongoDB syntax, it would be something like

db.Users.update({}, {$rename:{"Orders.Name":"Orders.Something"}},true, true)

Thanks.


回答1:


Look at

 MongoDB.Driver.Builders.Update.Rename(string oldElementName, 
                                       string newElementName)

It returns an IUpdateQuery, which you can pass to collection.Update() and rename your field. The C# Update builder has every special command you can use in mongo as a callable function to build your query.

The Builders namespace is a great namespace in the MongoDB C# driver. It includes Query and Update builders. You can chain commands and do things like this:

 Update.Set("indexsize", indexSize).Set("extractsize", extractedFileSize);

or

 Query.GT("filesize", 200000).In(bsonArray);


来源:https://stackoverflow.com/questions/6901701/mongodb-rename-embedded-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!