简体   繁体   中英

How to remove an Item from an Mongodb array documents

I have the following mongodb collection named Posts with document like following:

{
    "_id" : "111",
    "comments" : [
        {
            "replyPost" : "aaaa",
            "username" : "John Doe"
        },
        {
            "replyPost" : "bbbb",
            "username" : "Jane Smith"
        },
        {
            "replyPost" : "cccc",
            "username" : "Jane Smith"
        },
        {
            "replyPost" : "dddd",
            "username" : "Jane Smith"
        }
    ]
}

I am trying to remove an array item with the replyPost: "cccc" so the result would be:

{
    "_id" : "111",
    "comments" : [
        {
            "replyPost" : "aaaa",
            "username" : "John Doe"
        },
        {
            "replyPost" : "bbbb",
            "username" : "Jane Smith"
        },
        {
            "replyPost" : "dddd",
            "username" : "Jane Smith"
        }
    ]
}

I have tried .update method with $pull refering to mongodb document https://docs.mongodb.com/manual/reference/operator/update/pull/

Posts.update(
  {_id: this._id},
  { $pull: { comments: { replyPost:"cccc"} } }
);

which don't seem to be working. can anyone see the problem?

See if you're getting the right _id. It's in string format.

I tried the same in mongo shell. It worked for me.

Here's the log:

> db.posts.insert({
...     "_id" : "111",
...     "comments" : [
...         {
...             "replyPost" : "aaaa",
...             "username" : "John Doe"
...         },
...         {
...             "replyPost" : "bbbb",
...             "username" : "Jane Smith"
...         },
...         {
...             "replyPost" : "cccc",
...             "username" : "Jane Smith"
...         },
...         {
...             "replyPost" : "dddd",
...             "username" : "Jane Smith"
...         }
...     ]
... })
WriteResult({ "nInserted" : 1 })
> db.posts.update({_id:'111'},{$pull:{comments:{replyPost:'cccc'}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.findOne()
{
        "_id" : "111",
        "comments" : [
                {
                        "replyPost" : "aaaa",
                        "username" : "John Doe"
                },
                {
                        "replyPost" : "bbbb",
                        "username" : "Jane Smith"
                },
                {
                        "replyPost" : "dddd",
                        "username" : "Jane Smith"
                }
        ]
}

If you are using mongoose, you can do:

db.posts.remove({replyPost: 'cccc'}, function(err) {
})

The first parameter can be any mongoose query expression. All matches will be removed from db.

See mongoose remove

Tested Works Fine:

Posts.update((
  {"_id" : "111"},
  { $pull: {comments: {"replyPost" : "cccc"}} },
  { multi: true }
)

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