Rupesh
Rupesh

Reputation: 890

$all not working in mongodb

I have two document in my collections

{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fd15d8bfca2cc0aebb38' ],
    _id: 5ab9a5a0cb274a2064b65d1b,
    __v: 0 
},
{ participants: [ '5ab8fcf6d8bfca2cc0aebb37', '5ab8fcf6d8bfca2cc0aebb37' ],
    _id: 5ab9a5a7cb274a2064b65d1c,
    __v: 0
}

and i have an array of persons like

persons = [ 5ab8fcf6d8bfca2cc0aebb37, '5ab8fcf6d8bfca2cc0aebb37' ]

Now I am trying to find a document which contains which contain participants fields similar to array persons using this query.

Participant.find({participants: {$all: persons}}).exec()
        .then(connected => {
            console.log(connected);
            // perform some stuff
});

it throws me both document as an output. I don't know what is the problem.

Thanx in advance.

Upvotes: 4

Views: 913

Answers (2)

Ashh
Ashh

Reputation: 46481

you can use $eq operator as well

    Participant.find( { participants: { $eq: persons } })
      .then(connected => {
                console.log(connected);
                // perform some stuff
    });

for more https://docs.mongodb.com/manual/reference/operator/query/eq/

Upvotes: 2

Sede
Sede

Reputation: 61263

I think what you want is the $setEquals operator.

db.collection.find({ $expr: { $setEquals: [ persons, "$participants" ] } } )

You can use the $setEquals operator with the $redact operator if the $expr operator is not available in the mongod version you're running.

Upvotes: 2

Related Questions