Find documents with array that doesn't contains a specific value

I have the following model:

var PersonSchema = new Schema({
    name: String,
    groups: [
        {type: Schema.Types.ObjectId, ref: 'Group'}
    ],
});

I am looking for a query that retrieves all the Persons that are not part of a certain Group (i.e the persons’ group array doesn’t contain the id of the specified group).

I was thinking about something like this, but I’m not sure it is correct:

Person.find({groups: {$nin: [group._id]})

17 thoughts on “Find documents with array that doesn't contains a specific value”

Leave a Comment