Querying for non-null values in MongoDB
This MongoDB article will explain how to query for non-null values in MongoDB. To query for non-null values, you can use $ne
the operator and $eq
the operator and then specify the desired value to query.
This article shows the readers how to query for non-null values in MongoDB using various methods and examples. So, let’s get started with this guide.
$ne
Operators
in MongoDB
The syntax of this operator is:
{ field: { $ne: value } }
$ne
The operator selects all documents field
whose value of is different from the given . It includes documents that do not contain .value
field
$eq
Operators
in MongoDB
This operator specifies an equality condition. For example, $eq
the operator matches documents where field
the value of is equal to the specified value
.
The syntax for this operator is:
{ <field>: { $eq: <value> } }
Specifying $eq
the operator is equivalent to using the form { field: <value> }
, except <value>
that is a regular expression.
is Not Null
Querying values
in MongoDB
Now let’s help you get started by looking at an example of how to achieve this.
Below is the collection 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 is Not Null
the value of a string field in MongoDB
You will use $ne
the operator to pass an expression to $ne
the operator, which negates null values for a specific field, to query for string fields that are not null in this example. For this example, you can use the following query.
db.teams.find({
points: {
$ne: null
}
})
You find all points
such documents that have no field value specified as null.
$ne
Querying on numeric fields with operators
in MongoDBis Not Null
In this case, you will select those documents that do not have a numeric field with an empty value. For this example, you can use the following query.
db.teams.find({
position: {
$ne: null
}
})
You can again find documents that do not have null as a value in the position field.
$ne
Operator to query non-null values
in MongoDB
In this example, you will use $ne
the operator with a blank string to query MongoDB for non-null values.
Since the collection you used above does not have any such documents with blank string values, you can add some to the collection. The following documents will be 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
}
]
}
OK, now you have two more documents added to your collection and you are good to go. You can now use $ne
the operator to query for non-null values by specifying a blank string.
For this example, you can use the following query.
db.teams.find({
position: {
$ne: ""
}
})
This time you cannot find the newly added documents.
In MongoDB, $eq
the operator specifies a non-null value.
In this example, you will use $eq
the operator to query for non-null values because you will pass a value to look for to query for that value in layman's terms 不是空
.
Let's find the team ranked third. For this example, you can use the following query.
db.teams.find({
position: {
$eq: "3rd"
}
})
You found the team currently ranked third in the collection.
So, with the help of this MongoDB article, you have learned how to query for non-null values using the $ne
and $eq
operators.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
List all collections in MongoDB Shell
Publish Date:2025/04/29 Views:156 Category:MongoDB
-
When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c
Use find operator to join multiple conditions in MongoDB
Publish Date:2025/04/29 Views:132 Category:MongoDB
-
$lookup Today, we will see how to concatenate multiple conditions using the operator in MongoDB . In addition, we will explore some examples to demonstrate the use of $group the stage and $unionWidth the aggregation stage. $lookup Use the o
Date comparison in MongoDB
Publish Date:2025/04/28 Views:170 Category:MongoDB
-
This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie
Find objects between two dates in MongoDB
Publish Date:2025/04/28 Views:95 Category:MongoDB
-
In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio
Comparing Dates in MongoDB
Publish Date:2025/04/28 Views:115 Category:MongoDB
-
Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th
Convert string to date in MongoDB
Publish Date:2025/04/28 Views:162 Category:MongoDB
-
MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.
Building the MongoDB REST API
Publish Date:2025/04/28 Views:71 Category:MongoDB
-
MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD
Using ORM with MongoDB
Publish Date:2025/04/28 Views:118 Category:MongoDB
-
MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general
Locking mechanism in MongoDB
Publish Date:2025/04/28 Views:96 Category:MongoDB
-
In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock