根据ID

时间:2018-08-20 08:14:08

标签: php database mongodb web

我在mongodb中有一个数组,在该数组中,我想根据我拥有的ID删除嵌套的数组

下面是我的数组结构

{
        "_id" : ObjectId("5b17b991c440782b5a218cd1"),
       
        "product" : [
                {
                        "id" : ObjectId("5b71310eeff5c42f00f8a4e8"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                                        "status" : 1,
                                        "date_added" : "2018-08-10-09-55-54"
                                }
                        ],
                        "shipping_cost" : NumberLong(100),
                        "date_added" : "2018-08-10-09-55-54",
                        "date_status_change" : "2018-08-10-09-55-54",
                        "status" : 1
                },
                {
                        "id" : ObjectId("5b76ae3a40c1c449d6409fa3"),
                        "vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
                        "product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
                        "condition_id" : [
                                {
                                        "_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
                                        "status" : 0,
                                        "date_added" : "2018-08-17-11-15-06"
                                },
                                {
                                        "_id" : ObjectId("5ae978187ff1706f3b7dc47c"),
                                        "status" : 0,
                                        "date_added" : "2018-08-17-11-15-06"
                                }
                        ],
                        "shipping_cost" : 100,
                        "date_added" : "2018-08-17-11-15-06",
                        "date_status_change" : "2018-08-17-11-15-06",
                        "status" : 0
                },
       
        ]

例如,我想基于该数组块的condition_id_id删除_id内的特定数组。例如,如果我要删除第一个condition_id,则该块的值将为ObjectId('5ae977da7ff1706f3b7dc47a'),这是condition_id,而值为ObjectId('5b71310eeff5c42f00f8a4e8'),是该块的id

到目前为止,我已经尝试过了

db.vendor.update({_id:ObjectId('5b17b991c440782b5a218cd1')},     {$pull: {product: {id: ObjectId('5b71310eeff5c42f00f8a4e8')},condition_id:{_id:ObjectId('5ae977da7ff1706f3b7dc47a')}}});

编辑1:

我尝试过这个..甚至不匹配数组

db.vendor.update(  { "_id": ObjectId("5b17b991c440782b5a218cd1")},{ "$pull": { "product.$.id": ObjectId('5b71310eeff5c42f00f8a4e8') } } );

0 个答案:

没有答案