MongoDB find()在多维数组和$ unset中

时间:2018-06-09 21:29:48

标签: php mongodb

这是我在mongoDB上的对象,我需要通过ID在数组中找到 relations 对象并删除它。

 {
        "_id" : ObjectId("5b1a95c84700cb01758b4568"),
        "name" : "development",
        "type" : "",    
        "relations" : [ 
            {
                "_id" : ObjectId("5b1c290f20244eec02000029"),
                "name" : "mobile",
                "count" : 0
            }, 
            {
                "_id" : ObjectId("5b1c290f20244eec02000028"),
                "name" : "iOS",
                "count" : 0
            }
        ]
    }

通过关系对象“_id”查找然后取消设置此对象的最佳方法是什么?

我在这里得到身份证明

 $cursor = $collection->find([], ['relations' => []]);
 $find_id = "5b1c290f20244eec02000028";

        foreach (iterator_to_array($cursor) as $key) {
            foreach ($key['relations'] as $key2) {
                if ((string)$key2['_id'] == $find_id) {
                    var_dump($key2['_id']);
                }
            }
        }

0 个答案:

没有答案