在Mongodb中更新嵌入式文档属性
问题描述:
我有一个看起来像这样的文档:
I have a document that looks like this:
{
"_id": 3,
"Slug": "slug",
"Title": "title",
"Authors": [
{
"Slug": "slug",
"Name": "name"
}
]
}
我想基于Authors.Slug更新所有Authors.Name. 我试过了,但是没用:
I want to update all Authors.Name based on Authors.Slug. I tried this but it didn't work:
.update({"Authors.Slug":"slug"}, {$set: {"Authors.Name":"zzz"}});
我在做什么错了?
答
.update(Authors:{$elemMatch:{Slug:"slug"}}, {$set: {'Authors.$.Name':"zzz"}});