0

How do you query for “is null” in Mongo? i.e. opposite of {$ne: null}

I would like to execute a following query:

find a person with yearOfBirth between a given range, or yearOfBirth equals null (i.e. not set)

e.g.:

                        {$or: [
                            {yearOfBirth: {$e: null}},
                            {$and: [{yearOfBirth: {$gt: ageMaxYear}},
                                    {yearOfBirth: {$lt: ageMinYear}}]
                            }
                            ]
                        }

What should be the correct syntax?

Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
  • 1
    `"yearOfBirth": null` or `"yearOfBirth": { "$eq": null }`. But the `$eq` is basically *"implied"* in the standard syntax. Same for the `$and`, but you actually mean `$or` on both of those instead: `{ "$or": [{ "yearOfBirth": null }, { "yearOfBirth": { "$gt": ageMaxYear, "$lt": ageMinYear } }] }` – Neil Lunn Apr 14 '19 at 05:20

1 Answers1

0

I found this works:

                        {$or: [
                            {yearOfBirth: null},
                            {$and: [{yearOfBirth: {$gt: ageMaxYear}},
                                    {yearOfBirth: {$lt: ageMinYear}}]
                            }
                            ]
                        }
Elia Weiss
  • 8,324
  • 13
  • 70
  • 110