2016-05-03 181 views
1
{ 
    "_id" : ObjectId("5728443e04b2e5b42073a361"), 
    "user_id" : ObjectId("5728443e04b2e5b42073a35f"), 
    "main" : { 
     "data_to_save" : [ 
      { 
       "user_profile_id" : ObjectId("572844bc04b2e5b42073a362") 
       "_id" : ObjectId("57284ab183e62da222e1378b"), 
       "details" : [ 
        { 
         "amount" : 10000, 
         "_id" : ObjectId("57284f42e4560b66238a637c"), 
         "modified" : ISODate("2016-05-03T12:42:02.927Z"), 
         "created" : ISODate("2016-05-03T12:42:02.927Z") 
        }, 
        { 
         "amount" : 4000 
         "_id" : ObjectId("57284f42e4560b66238a637b"), 
         "modified" : ISODate("2016-05-03T12:42:02.927Z"), 
         "created" : ISODate("2016-05-03T12:42:02.927Z") 
        } 
       ] 
      } 
     ] 
    } 
    "__v" : 0 
} 

在mongoDB的數據庫模式中。在這裏,我想刪除一個特定集合的相關文檔。 就像我想刪除細節收集的第一個文件。我有該文件的_id(57284f42e4560b66238a637c)。如何使用mongodb查詢從深集合中刪除文檔?

試圖

connection.collection.remove({ 'main.data_to_save.0.details.$._id' : id }, function (err, removed) {}); 

回答

0

您可以使用$pull運營商從文檔的數組中刪除一個或多個條目:

// mainDocId = ObjectId("5728443e04b2e5b42073a361") 
// id = ObjectId("57284f42e4560b66238a637c") 
connection.collection.update(
    { _id: mainDocId}, 
    { $pull: {'main.data_to_save.0.details' : id } }, function (err, result) { 

}); 
相關問題