简体   繁体   中英

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. 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.

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:

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM