1

Here is the schema of my collection "profile"

{ 
        _id : ObjectId("123"), 
        name : "Tommy", 
        defaultrates : 
        [ 
                {_id : ObjectId("111"), rate : 35.0, raisedOn : "5/2/2009"}, 
                {_id : ObjectId("222"), rate : 55.0, raisedOn : "5/3/2010"}, 
                {_id : ObjectId("333"), rate : 65.0, raisedOn : "5/5/2010"} 
        ] 
} 

I want to remove the first index of default rates ({_id : 111, rate : 35.0, raisedOn : "5/2/2009"}).

The result should be:

{ 
        _id : ObjectId("123"), 
        name : "Tommy", 
        defaultrates : 
        [ 
                {_id : ObjectId("222"), rate : 55.0, raisedOn : "5/3/2010"}, 
                {_id : ObjectId("333"), rate : 65.0, raisedOn : "5/5/2010"} 
        ] 
}

I did this in the console:

db.profile.update({'_id: ObjectId("123")},{ $pull: { 'defaultrates':{'_id': "111"}}},{ multi: false })

But sadly, it didn't gave me the desired output.

Please help. What's the proper way of doing this?

2
  • ...update({'id: .... you forgot to close the quote? Commented Aug 20, 2018 at 5:19
  • Thanks for the correction. But still, it didn't gave the desired output Commented Aug 20, 2018 at 5:36

1 Answer 1

1

I think you are mixing and matching quotes and double quotes indiscriminately. Try this:

db.profile.update({
"_id": ObjectId("123")
},
{
    "$pull": {
        "defaultrates": {
            "_id": ObjectId("111")
        }
    }
},
false,
true
);

that false is multi. And true is upsert (if not available, insert one).

Sign up to request clarification or add additional context in comments.

6 Comments

Just didt it. The result was: "nMatched" : 1, "nUpserted" : 0, "nModified" : 0
It did not modify the collection. I wonder what I am missing?
ok try this one. I edited my answer. I forgot to add object ID inside default rates
Still didn't gave the desired output. Do you have any other ways in order to get the desired output?
what is the output you are seeing?
|

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.