1

I am trying to delete "virtualNumber" : "12345" in the following document:

{
"_id" : ObjectId("50a9db5bdc7a04df06000005"),
"billingInfo" : null,
"date" : "dsfdsfsdfsd",
"description" : "sdfsdff",
"pbx" : {
  "_id" : ObjectId("50a9db5bdc7a04df06000006"),
  "did" : {
    "1234567890" : {
      "inventoryId" : "509df7547e84b25e18000001",
      "didcountry" : "india",
      "didState" : "bangalore",
      "routeType" : "CallForward",
      "didNumber" : "1234567890",
      "didVirtualNumbers" : [
        {
          "virtualNumber" : "12345"
        },
        {
          "virtualNumber" : "56789"
        }
      ],
      "id" : ObjectId("50a9db9acdfb4f9217000002")
    }
  },
},

I am using node.js, so I constructed a query in JavaScript:

var query = {_id: ObjectId("50a9db5bdc7a04df06000005")};

var obj = {};
obj["pbx.did.1234567890.didVirtualNumbers.virtualNumber"]=12345;
//problem
collection.update(query,{$pull:obj});

2 Answers 2

2

You need to match the array element like:

{"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}

So you should change your code to:

obj["pbx.did.7259591220.didVirtualNumbers"]={"virtualNumber": "12345"};
Sign up to request clarification or add additional context in comments.

5 Comments

thanks but its not working, query is { '$pull': { 'pbx.did.7259591220.didVirtualNumbers': { virtualNumber: '12345' } } }
When I save the document in your question into "objects" collection in my db, I can remove the virtualNumber "12345" from the document using query db.objects.update({"_id" : ObjectId("50a9db5bdc7a04df06000005")}, {"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}). After this query, the document contains only the other virtualNumber "56789".
Are you trying to run this in the Mongo console or as part of you application? Do you get any error messages?
You have your code inside a db.collection call, right? Have you double checked that you have collection names etc. right?
oh my god, spelling mistake in collection name..very sorry for troubling, thanks a lot it working fine...
0

Please refer to http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull

It mentions the pull field should be an array.

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.