簡體   English   中英

如何使用mongodb查詢從深層集合中刪除文檔?

[英]How to remove a document from a deep collection using mongodb query?

{
    "_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)。

試着

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

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

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

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM