2

I have this data

{
   "_id": ObjectId("575e66a4c2b503944800002b"),
   "phone": "201-780-8497",
   "orders": [
     "1961682636",
     "43243242" 
  ] 
}

I want to remove order '43243242' where phone is "201-780-8497"

Here is my PHP code

$collection->update(
        array('phone'=>$doc['phone']),
        array('$pull' => array('orders' => array('1961682636'), array('safe' => TRUE))
));

This does nothing, no error, no exception.

Where I am getting wrong?

1 Answer 1

2

You update operation should be :

db.collection.update(
    { "phone" : "201-780-8497" },
    { "$pull": { "orders": "43243242" } }
)

and your PHP code:

$collection->update(
    array('phone' => '201-780-8497'),
    array('$pull' => array('orders' => '43243242'))
);

You are getting it wrong on the $pull syntax; you don't need to push the element you want to remove to an array as the $pull document, just reference the value. For a more detailed explanation, consult the docs.

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.