0

I am trying to delete the object with the id: "2019-08-22T04:53:11.357Z" from this users portfolio array.

I have searched for the correct query filter to help me delete the object but nothing seems to be working!

Here is the data we are working with. There is currently only one user (InStock) in the system but more will be added in the future.

[
  {
    "id": "MTg4MDU3ODM2MDk2NDU0NjU3",
    "name": "InStock",
    "balance": 7760,
    "portfolio": [
      {
        "id": "2019-08-22T04:15:22.998Z",
        "name": "Jordan Shoe",
        "size": "10.5",
        "price": 150
      },
      {
        "id": "2019-08-22T04:36:37.836Z",
        "name": "Nike Tee",
        "size": "M",
        "price": 35
      },
      {
        "id": "2019-08-22T04:53:11.357Z",
        "name": "Adidas Shoe",
        "size": "8.5",
        "price": 100
      }
    ],
    "history": [

    ]
  }
]

and here is what I was trying using what I've seen in other solutions.

db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"}, {$pull : {"portfolio": {"id": "2019-08-22T04:36:37.836Z"}}})

I am looking for the line to remove the Adidas Shoe object from InStock's portfolio array.

3
  • user portfolio array is a collection in mongo? Commented Aug 22, 2019 at 6:15
  • @AzzamAsghar No, the collection includes the entire JSON I included with my question. Hopefully that helps. Commented Aug 22, 2019 at 6:19
  • Your query was correct though, the only difference is that you have wrong id there, it ends with 836, not with 357 as you state at the top of your question, can that be the issue? So you are removing the wrong one, and thinking that $pull does not work? Commented Aug 22, 2019 at 6:59

2 Answers 2

1

Try this query:

db.test1.updateOne({"_id" : ObjectId("5d5e7a291b761bfc0420e580")}, 
{$pull: {"portfolio": {"name": "Adidas Shoe"}}})

After execution:

{
    "_id" : ObjectId("5d5e7a291b761bfc0420e580"),
    "portfolio" : [ 
        {
            "id" : "2019-08-22T04:15:22.998Z",
            "name" : "Jordan Shoe",
            "size" : "10.5",
            "price" : 150.0
        }, 
        {
            "id" : "2019-08-22T04:36:37.836Z",
            "name" : "Nike Tee",
            "size" : "M",
            "price" : 35.0
        }
    ],
    "name" : "InStock",
    "balance" : 7760.0
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's Working Fine for you.

db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"},
{ $pull: { portfolio: { $elemMatch: { id:"2019-08-22T04:36:37.836Z"} }},

6 Comments

Results in error MongoError: unknown top level operator: $eq
@AbramKremer updated my answer please check hope it's working
Thank you for the answer. Sadly, it is still not removing the object as hoped for. I'm beginning to wonder if the problem lies somewhere else in my project, but I'm doubtful since all other MongoDB commands are performing properly.
@AbramKremer okay got it .which version of mongodb are you using?
Verision 3.3.0.
|

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.