How to Query for Is Not Null Value in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. the $ne Operator in MongoDB
  2. the $eq Operator in MongoDB
  3. Query for the is Not Null Value in MongoDB
  4. $ne Operator to Query for the is Not Null Value on a String Field in MongoDB
  5. $ne Operator to Query for the is Not Null on a Numeric Value Field in MongoDB
  6. $ne Operator to Query Non-Empty Values in MongoDB
  7. $eq Operator to Specify a Non-Null Value in MongoDB
How to Query for Is Not Null Value in MongoDB

This MongoDB article will explain how you can query non-null values in MongoDB. To query for the is not null value, you can use the $ne operator and the $eq operator, then specify the desired value that you want to query for.

This article shows the readers how to query for is not null values in MongoDB using various methods and examples. So, let’s get this guide started.

the $ne Operator in MongoDB

The syntax for this operator is:

{ field: { $ne: value } }

The $ne operator selects all the documents where the field’s value differs from the given value. It includes documents that don’t contain the field.

the $eq Operator in MongoDB

This operator specifies equality conditions. For example, the $eq operator matches documents where the value of a field equals the specified value.

Syntax for this operator is:

{ <field>: { $eq: <value> } }

Specifying the $eq operator is equivalent to using the form { field: <value> } except when the <value> is a regular expression.

Query for the is Not Null Value in MongoDB

Now let’s get you started by looking at examples of how you can achieve this.

Below is the collection that you will use to illustrate some operations.

db={
  "teams": [
    {
      team: "Manchester City",
      position: "1st",
      points: 70
    },
    {
      team: "Liverpool",
      position: null,
      points: 69
    },
    {
      team: "Chelsea",
      position: "3rd",
      points: 59
    },
    {
      team: "Arsenal",
      position: "4th",
      points: null
    },
    {
      team: "Tottenham",
      position: "5th",
      points: 51
    },
    {
      team: "Manchester United",
      position: "6th",
      points: 50
    },
  ]
}

$ne Operator to Query for the is Not Null Value on a String Field in MongoDB

You’ll use the $ne operator to pass an expression to the $ne operator that negates a null value for a specific field to query for a string field that isn’t null in this example. You can use the query below for this example.

db.teams.find({
  points: {
    $ne: null
  }
})

The result returned when running the query above can be seen in the screenshot below.

ne operator 1

You found all such documents with no points field value specified null.

$ne Operator to Query for the is Not Null on a Numeric Value Field in MongoDB

In this instance, you will be picking those documents that do not have a numeric-value field value that is null. You can use the query below for this example.

db.teams.find({
  position: {
    $ne: null
  }
})

The result returned when running the query above can be seen in the screenshot below.

ne operator

You can again find the documents that did not have null as the value on the position field.

$ne Operator to Query Non-Empty Values in MongoDB

In this example, you’ll use the $ne operator with a blank string to query MongoDB for is not null values.

As the collection you are using above does not have any such document with a blank string value, you can add a few to the collection. The following documents are added to the collection as shown below.

db={
  "teams": [
    {
      team: "Manchester City",
      position: "1st",
      points: 70
    },
    {
      team: "Liverpool",
      position: null,
      points: 69
    },
    {
      team: "Chelsea",
      position: "3rd",
      points: 59
    },
    {
      team: "Arsenal",
      position: "4th",
      points: null
    },
    {
      team: "Tottenham",
      position: "5th",
      points: 51
    },
    {
      team: "Manchester United",
      position: "6th",
      points: 50
    }, {
      team: "West Ham",
      position: "",
      points: 48
    },
    {
      team: "Wolves",
      position: "",
      points: 46
    }

  ]
}

Okay, now that you have two more documents added to your collection, you are good to go. Now you can use the $ne operator to query for the is not null value by specifying a blank string.

You can use the query below for this example.

db.teams.find({
  position: {
    $ne: ""
  }
})

The result returned when running the code above can be seen in the screenshot below.

ne operator 2

This time you were unable to locate the newly added documents.

$eq Operator to Specify a Non-Null Value in MongoDB

In this example, you will use the $eq operator to query for a non-null value because you will be passing a value you want to look up to query for that is not a null value in layman’s terms.

Let’s look for the team which has the 3rd position. You can use the query below for this example.

db.teams.find({
  position: {
    $eq: "3rd"
  }
})

The result returned when you run the code above can be seen in the screenshot below.

eq operator

You found the team in the collection that currently holds the 3rd position.

So through the help of this MongoDB article, you learned how to query for the is not null value with the use of $ne and $eq operators.

Related Article - MongoDB Query