简体   繁体   English

重命名MongoDB中数组中的嵌入式文档中的字段不起作用

[英]Renaming a field in an embedded Document in an Array in MongoDB not working

Step One 第一步

> db.myCollection.find();
{ "_id" : ObjectId("2358523892345"), "field1" : "value 1", "field2" : [ { "subfield1" : "value 2" }, { "Subfield2" : "value 3" } ], "field3" : "value 4" }

I am wanting to rename the field Subfield2 to subfield2 . 我想重命名场Subfield2subfield2 I tried: 我试过了:

Step Two 第二步

> db.myCollection.update ( { "field3": "value 4" }, {$rename: {"Subfield2": "subfield2" } } )

And then ran find() again and get the same results as in 'Step One' ie the field is not renamed. 然后再次运行find()并获得与“第一步”相同的结果,即该字段未重命名。

Using MongoDB terminology, I think what I am trying to do is 'rename a field in an embedded document in an array'. 使用MongoDB术语,我想我要做的是“重命名数组中嵌入式文档中的字段”。

References 参考文献

http://docs.mongodb.org/manual/reference/operator/rename/ http://docs.mongodb.org/manual/reference/operator/rename/

It seems to not be possible to rename a field within an array from the command line as answered in this question: 似乎无法从命令行重命名数组中的字段,如以下问题所示:

MongoDB rename database field within array MongoDB重命名数组中的数据库字段

As mentioned in the documentation there is no way to rename fields within arrays. 如文档中所述,无法重命名数组中的字段。 Your only option is to iterate over your collection documents, read them and update each with $unset old/$set new operations. 唯一的选择是遍历收集文档,阅读它们,并使用$ unset old / $ set new操作更新每个文档。

It is possible to change these values via RockMongo however as suggested by user Liad Livnat. 可以根据用户Liad Livnat的建议通过RockMongo更改这些值。

For my particular instance, whist there I also removed the array and changed the structure to: 对于我的特定实例,在那里,我还删除了数组并将结构更改为:

{
  "field1": "value 1",
  "field2": {"subfield1": "value 2", "subfield2": "value 3"},
  "field3": "value 4"
}

Querying this object was then possible with: 然后可以使用以下方法查询此对象:

db.myCollection.find( {"field2.subfield2":"value 3"} );

if you want to update it manually i suggest you will install rockmongo, rockmongo is a great tool working with mongo databases, just extract it on your server and connect to your database. 如果您想手动更新它,我建议您安装rockmongo,rockmongo是使用mongo数据库的好工具,只需将其提取到服务器上并连接到数据库即可。 there you will find very easy update to mongo database, tables and records. 在那里,您会发现对mongo数据库,表和记录的更新非常容易。

rock mongo 摇滚蒙哥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM