2014-09-03 106 views
0

内阵列内的对象对于蒙戈集合,看起来象下面这样:更新一个蒙戈文件

{ 
    { 
    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: [ .. ] 
    }, 
} 

我写一个查询发现,有一个“本田”发动机所有twoWheelers和更新由一个人的数量

我的更新查询看起来像下面:

db.stock.update(
{ 'twoWheelers.engine' : 'honda'}, 
{ /*update statement here */ }, 
{ multi: true} 
) 

什么混淆我是如何定位的twoWheeler阵列的更新中,准确的指标?

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

回答

1

$的位置更新操作者表示每个文档中的第一匹配的数组元素的索引,因此可以使用的是,在呼叫update这样:

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