我正在尝试更新mongodb文档中数组中包含的单个子元素.我想使用其数组索引引用该字段(数组中的元素没有任何我可以保证将是唯一标识符的字段).看起来这应该很容易,但我无法弄清楚语法.
这是我想在伪json中做的事情.
之前:
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... old content B ... },
{ ... old content C ... }
]
}
Run Code Online (Sandbox Code Playgroud)
后:
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
Run Code Online (Sandbox Code Playgroud)
好像查询应该是这样的:
//pseudocode
db.my_collection.update(
{_id: ObjectId(document_id), my_array.1 : 1 },
{my_array.$.content: …Run Code Online (Sandbox Code Playgroud)