mongodb更新数组中的所有匹配项

假如集合中有如下数据

{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
  "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }

将comments值中所有votes的值改为1

 t.update( obj, {$set:{'comments.$.votes':1}}, false, true )

结果为:

{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
  "comments" : [ { "by" : "joe", "votes" : 1 }, { "by" : "jane", "votes" : 1 } ] }

 

posted @ 2017-04-22 21:39  向着太阳生  阅读(3514)  评论(0编辑  收藏  举报