重命名MongoDB中数组中的嵌入式文档中的字段不起作用 [英] Renaming a field in an embedded Document in an Array in MongoDB not working

查看:64
本文介绍了重命名MongoDB中数组中的嵌入式文档中的字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一步

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

我想将字段Subfield2重命名为subfield2.我尝试过:

I am wanting to rename the field Subfield2 to subfield2. I tried:

第二步

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

然后再次运行find()并获得与第一步"相同的结果,即该字段未重命名.

And then ran find() again and get the same results as in 'Step One' ie the field is not renamed.

使用MongoDB术语,我想我要做的是重命名数组中嵌入式文档中的字段".

Using MongoDB terminology, I think what I am trying to do is 'rename a field in an embedded document in an array'.

参考

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重命名数组中的数据库字段

如文档中所述,无法重命名字段 在数组内.您唯一的选择是遍历您的收藏 文档,阅读它们并用$ unset old/$ set new更新每个文档 操作.

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.

但是可以按照用户Liad Livnat的建议通过RockMongo更改这些值.

It is possible to change these values via RockMongo however as suggested by user Liad Livnat.

对于我的特定实例,在那里,我还删除了数组并将结构更改为:

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

这篇关于重命名MongoDB中数组中的嵌入式文档中的字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆