使用$pull更新的yii2 mongodb语法
集合中记录的结构:使用$pull更新的yii2 mongodb语法,mongodb,yii2,Mongodb,Yii2,集合中记录的结构: { "_id": ObjectId("577e3c801da29a8f2f8b4567"), "contacts": [ { "id": NumberInt(7) }, { "id": NumberInt(8) } ], "user": NumberInt(10) } 必须从contacts数组的所有记录中删除具有特定id的对象 在Mongo中,该问题通过以下请求解决(对于id=7): 如何在Yi2 mon
{
"_id": ObjectId("577e3c801da29a8f2f8b4567"),
"contacts": [
{ "id": NumberInt(7) },
{ "id": NumberInt(8) }
],
"user": NumberInt(10)
}
必须从contacts数组的所有记录中删除具有特定id的对象
在Mongo中,该问题通过以下请求解决(对于id=7):
如何在Yi2 mongodb中实现扩展器
return $this->updateAll(
[ '$pull' => ['contacts' => ['id' => ['$in' => $contactIds] ] ] ],
[ 'user' => $this->user ]
);
其中$contactIds—用于从联系人中删除对象(使用这些id)的id-s数组
$data = Category::findOne(['_id' => $id]);
$key = array_search($parent_id, array_column($data->parent, 'id'));
Yii::$app->mongodb->getCollection('category')->update(['_id' => $id], ['$pull' => [
'parent' => $data->parent[$key]
]]);
$data = Category::findOne(['_id' => $id]);
$key = array_search($parent_id, array_column($data->parent, 'id'));
Yii::$app->mongodb->getCollection('category')->update(['_id' => $id], ['$pull' => [
'parent' => $data->parent[$key]
]]);