2016-07-07 41 views
-1

结构:

{ 
    "_id": ObjectId("577e3c801da29a8f2f8b4567"), 
    "contacts": [ 
     { "id": NumberInt(7) }, 
     { "id": NumberInt(8) } 
    ], 
    "user": NumberInt(10) 
} 

,有必要从联系人数组,对象中的所有记录删除与某个ID。

db.perimeter.update({ }, {$pull: {contacts: {id:7}}}, {multi: true}) 

如何实现yii2-MongoDB的扩展:

在蒙戈的问题是(对于ID = 7)这样的要求解决了吗?

回答

0
return $this->updateAll(
    [ '$pull' => ['contacts' => ['id' => ['$in' => $contactIds] ] ] ], 
    [ 'user' => $this->user ] 
); 

其中$ contactIds - 用于从联系人中删除对象(带有这些ID)的id-s数组。