0

This is the object that i have:

{
  "_id": ObjectId("590985b1859aefea4a39982f"),
  "comment": [{
    "created_at": 1493880257678.0,
    "comment": "12345",
    "user_id": ObjectId("58edd98d40627c492c8689c5"),
    "_id": ObjectId("590acdc1141b16c6521b9140"),
    "modified": 1493880257678.0,
    "assigned_to": null
  }, {
    "created_at": 1493880257678.0,
    "comment": "12345",
    "user_id": ObjectId("5906c50c1be98711121edf2b"),
    "_id": ObjectId("590acdc1141b16c6521b9140"),
    "modified": 1493880257678.0,
    "assigned_to": null
  }, {
    "created_at": 1493880257678.0,
    "comment": "12345",
    "user_id": ObjectId("58edd98d40627c492c8689c5"),
    "_id": ObjectId("590acdc1141b16c6521b9140"),
    "modified": 1493880257678.0,
    "assigned_to": null
  }]
}

I want delete subdocument of comment where user_id(58edd98d40627c492c8689c5)

1
  • May you edit the question to be a little neater? Commented May 17, 2017 at 12:56

1 Answer 1

1

you can use findOneAndUpdate with $pull.

you will construct your query like this.

findOneAndUpdate(
{_id: <id of the document>}, 
{$pull: {comment: {user_id: <user_id>}}}, 
{multi:true}
)

Not sure what client or driver you are using, but here is an example test that I did on robomongo, collection name is "documents"

db.getCollection('documents').findOneAndUpdate(
{_id: new ObjectId("590985b1859aefea4a39982f")}, 
{$pull: 
   {comment:{user_id : new ObjectId("58edd98d40627c492c8689c5")}}
}, 
multi:true})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.