1

i have inside my mongoDB collection this document

{ 
"_id" : ObjectId("5b633025579fac22e74bf3be"), 
"FLAGS" : [
    {
        "toSent" : [
            {
                "_id" : ObjectId("5b633025579fac22e74bf3c2"), 
                "phone" : "+84404040404"
            }, 
            {
                "_id" : ObjectId("5b633025579fac22e74bf3c1"), 
                "phone" : "+212652253403"
            }, 
            {
                "_id" : ObjectId("5b633025579fac22e74bf3c0"), 
                "phone" : "+212123456788"
            }
        ], 
        "_id" : ObjectId("5b633025579fac22e74bf3bf"), 
        "action" : "group_p_a"
    }, 
    {
        "toSent" : [
            {
                "_id" : ObjectId("5b633031579fac22e74bf3c9"), 
                "phone" : "+212651077199"
            }, 
            {
                "_id" : ObjectId("5b633031579fac22e74bf3c8"), 
                "phone" : "+84404040404"
            }, 
            {
                "_id" : ObjectId("5b633031579fac22e74bf3c7"), 
                "phone" : "+212652253403"
            }, 
            {
                "_id" : ObjectId("5b633031579fac22e74bf3c6"), 
                "phone" : "+212123456788"
            }
        ], 
        "_id" : ObjectId("5b633031579fac22e74bf3c5"), 
        "action" : "group_p_a"
    }
], 
"time" : ISODate("2018-08-02T16:24:05.747+0000"), 
"action_user_phone" : "+212123456788", 
"idGroup" : "e534379a-1580-4568-b5ec-6eaf981538d2", 
"nomGroup" : "MOH FOR EVER", 
"__v" : NumberInt(0)

} TODO I need to remove for example this element { "_id" : ObjectId("5b633025579fac22e74bf3c2"), "phone" : "+84404040404"}

WHAT I DID

GroupEvents.update({}, {$pull:{FLAGS:{$elemMatch:{toSent:{phone: "+84404040404"}  }}}},function(err,ret){
    if(err)
        console.log("error"+err);
    if(ret)
        console.log(ret);
});

It remove all what's inside toSent event if it doesn't match.

Any help please

1 Answer 1

4

You need to use $ positional operator instead of $elemMatch here

GroupEvents.update(
  { "Flags.toSent.phone": "+84404040404" },
  { "$pull": { "FLAGS.$.toSent": { "phone": "+84404040404" }}},
)

If you want remove from every element of FLAGS array this you need to use $[] the all positional operator

GroupEvents.update(
  { "Flags.toSent.phone": "+84404040404" },
  { "$pull": { "FLAGS.$[].toSent": { "phone": "+84404040404" }}},
)
Sign up to request clarification or add additional context in comments.

4 Comments

it pull out only the first element for each array not all matched element, even if i use {multi:true}
what is your mongodb version... Updated the answer as well
my mongo versions is 3.4.2, i tried this, they said { [MongoError: cannot use the part (FLAGS of FLAGS.$[].toSent) to traverse the element ({FLAGS: [ { _id: ObjectId('5b633025579fac22e74bf3bf'),
You need to upgrade your mongodb version to 3.6 or 4.0 to use this

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.