0

这个问题很简单。我在 mongoDb 中有以下文档:

_id : ObjectId("519e3c14ade67153e9a003a0")
status:1, 
name:"name",
details:
{ 
  crm:115, 
  webs: 
  [
    { tag:"blog" , url:"http://..."} 
  ], 
  contacts:
  [
    {
      _id:ObjectId("009e37e1ade67153e9a0039e"),
      name:"me", 
      phones:
      [
        { tag:"home", number:"123..." },
        {tag:"mobile", number:"123456789"}
      ]
    }
  ]
}

我正在尝试更新子文档 details.contacts 中的电话之一:

db.new.update(
{
  _id : ObjectId("519e3c14ade67153e9a003a0"), 
  "details.contacts._id": ObjectId("009e37e1ade67153e9a0039e"),
  "details.contacts.phones.tag":"home"
},
{
  $set: 
  {
    "details.contacts.$.phones.$": 
    {
      tag:"home", 
      number:"965923777"
    }
  }
})

最后我得到以下错误:如果没有包含数组的相应查询字段,则无法应用位置运算符。

无论如何要以这种方式更新文档,还是我应该发送所有电话来更新此字段?

4

0 回答 0