Mongodb 更新mongo文档中数组内的对象

Mongodb 更新mongo文档中数组内的对象,mongodb,Mongodb,对于如下所示的mongo集合: { { site: 'one', twoWheelers: [ { engine: 'honda', qty:5 } { engine: 'yamaha', qty:6 } ], fourWheelers: [ ..], eightWheelers: [ .. ] }, { site : 'two', twoWheelers: [

对于如下所示的mongo集合:

{
  {
    site: 'one',
    twoWheelers: [
         { engine: 'honda',  qty:5 }
         { engine: 'yamaha', qty:6 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
  { 
    site : 'two',
    twoWheelers: [
         { engine: 'honda',  qty:13 }
         { engine: 'yamaha', qty:16 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
  {
    site: 'three',
    twoWheelers: [
         { engine: 'honda',  qty:2 }
         { engine: 'yamaha', qty:10 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
}
db.stock.update(
{ 'twoWheelers.engine' : 'honda'},
{ /*update statement here */ },
{ multi: true}
)
我正在写一个查询,查找所有拥有“本田”发动机的两轮车,并将其数量更新一次

我的更新查询如下所示:

{
  {
    site: 'one',
    twoWheelers: [
         { engine: 'honda',  qty:5 }
         { engine: 'yamaha', qty:6 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
  { 
    site : 'two',
    twoWheelers: [
         { engine: 'honda',  qty:13 }
         { engine: 'yamaha', qty:16 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
  {
    site: 'three',
    twoWheelers: [
         { engine: 'honda',  qty:2 }
         { engine: 'yamaha', qty:10 }     
        ],
    fourWheelers: [ ..],
    eightWheelers: [ .. ]
  },
}
db.stock.update(
{ 'twoWheelers.engine' : 'honda'},
{ /*update statement here */ },
{ multi: true}
)
让我困惑的是,我该如何在twoWheeler数组中针对该确切索引进行更新

请注意:我的搜索工作正常,并返回应更新的文档。

位置更新运算符表示每个文档中第一个匹配数组元素的索引,因此您可以在
更新
调用中使用它,如下所示:

db.stock.update(
{'twoWheelers.engine':'honda'},
{$inc:{'twoWheelers.$.qty':1},
{multi:true}
)