Andreas Trantidis
Andreas Trantidis

Reputation: 130

Find where array of objects does not contain objects with param with specific value

I'm using Mongoose and have a schema like this:

var User = new mongoose.Schema({
    registrations:[{
        fieldA: String,
        fieldB: String,
    }]
});

var UserModel = mongoose.model('User', User);

I want to find all users that their registrations array does not contain objects with fieldA == 'specific value'.

Upvotes: 1

Views: 3577

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312129

Use the $ne operator and dot notation to do this:

UserModel.find({'registrations.fieldA': {$ne: 'specific value'}}, cb);

When used with an array field like this, $ne will only match docs where no array element contains the specific value.

Upvotes: 6

Related Questions