2017-03-11 96 views
0

我想刪除數組中的對象。但是該數組嵌套在兩個對象中。我該怎麼做呢?mongodb:刪除數組中的對象,嵌套在兩個對象中

例如:比方說,我想和 _id刪除交易:58c3154a19f82c0ddc53f0de

我將如何做到這一點?

{ 
    "_id": { 
     "$oid": "58bad6cf93ab9703da331e25" 
    }, 
    "username": "[email protected]", 
    "password": "sha1$fc05ad7d$1$1cc86a287642516f947fda520ae8ddd42e983e23", 
    "firstName": "David", 
    "lastName": "David", 
    "transactions": { 
     "2017": { 
      "3": [ 
       { 
        "where": "Duane Reade", 
        "what": "asdf", 
        "category": 6, 
        "amount": "34", 
        "_id": { 
         "$oid": "58c300ef1602f90c7166cbfb" 
        }, 
        "date": { 
         "day": 10, 
         "month": 3, 
         "year": 2017 
        } 
       }, 
       { 
        "where": "Amazon", 
        "what": "asdf", 
        "category": 2, 
        "amount": "100", 
        "_id": { 
         "$oid": "58c3154a19f82c0ddc53f0de" 
        }, 
        "date": { 
         "day": 10, 
         "month": 3, 
         "year": 2017 
        } 
       } 
      ] 
     } 
    } 
} 

回答

0

爲此,您需要使用$pull運算符。用點符號表示數組元素。以下查詢完成作業:

db.user1.update({}, 
    {$pull: {'transactions.2017.3': {_id: ObjectId("58c3154a19f82c0ddc53f0de")}} 
});