简体   繁体   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
}

In database schema of mongoDB. 在mongoDB的数据库模式中。 Here I am want to remove perticular document of a perticular collection. 在这里,我想删除一个特定集合的特定文档。 like I want to remove first document of details collection. 我希望删除第一个详细信息收集文档。 I have _id ( 57284f42e4560b66238a637c ) of that perticular document. 我有那个特定文件的_id(57284f42e4560b66238a637c)。

Tried 试着

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

You could use the $pull operator to remove one or more entries from an array of documents: 您可以使用$ 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