MongoDB with PHP: nest されたキーバリューの削除

updated: 2018/10/23

 環境: Laravel 5.7.6 PHP7

 

f:id:monteecristoo:20181022200412p:plain

特定のコレクション内のcomments array の特定の _id をもつバリューを削除したい。

$db = (new MongoDB\Client)->YOUR_DB;

$collection = $db->YOUR_COLLECTION;

f:id:monteecristoo:20181023203117p:plain

$pid は削除したい pixieImage collection の id

$cid は削除したいcomment の id  下記例ではcommentsの一番目の_id

f:id:monteecristoo:20181023194406p:plain

MongoDB\BSON\ObjectId を自前で作るなら

use MongoDB;

f:id:monteecristoo:20181023202911p:plain

参考:

stackoverflow.com

MongoDB\Collection::updateOne() — PHP Library Manual 1.4

 

https://docs.mongodb.com/manual/reference/operator/update/pull/index.html

 

PHP: MongoDB\BSON\ObjectId - Manual

https://docs.mongodb.com/php-library/current/reference/method/MongoDBCollection-findOne/