How to Use MongoDB Logical Query Operators in Java

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This tutorial will explain how to go about using MongoDB Logical Query in Java to retrieve documents with the $or, the $nor and the $and commands. The MongoDB and compatible version of MongoDB Java driver must be properly installed and configured in order to use MongoDB Logical Query Operators in Java. The examples in this tutorial will use MongoDB version 4.0 and MongoDB Java Driver version 3.8.2.

Prerequisite

  • The MongoDB and MongoDB Java driver must be properly installed and configured before beginning.

  • The latest version of Java JDK must be properly installed and configured before beginning.

  • Confirm the MongoDB service is running.

NOTE: The examples in this tutorial use MongoDB version 4.0 and MongoDB Java Driver version 3.8.2

The MongoDB Dataset

  • The following dataset will be used for all of the examples in this tutorial:
IDNameRatingCountry
5ceb7d233fd48a1ebd04318eChang Kai-shek11China
5ceb7d233fd48a1ebd04318fSun Yat-sen8China
5ceb7d233fd48a1ebd043190Muammar al-Gaddafi15Libya
5ceb7d233fd48a1ebd043191Oda Nobunaga15Japan
5ceb7d233fd48a1ebd043192Cao Cao19China
5ceb7d233fd48a1ebd043193Sun Tzu10China

MongoDB Query Operator $or

  • The $or operator executes a logical OR operation on a multiple array <expression> and selects the documents that meets the criteria of at least one of the <expressions>.

  • Here is an example of using the $or in Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MongoDatabase db = mongo.getDatabase("warlordDB");
FindIterable<document> findIt = db.getCollection("warlordCollection").find(or
(
eq("name","Cao Cao"),
eq("name", "Oda Nobunaga")
)
);

MongoCursor<document> cursor = findIt.iterator();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}

NOTE: As with the “MongoDB Comparison Query Operator” it is recommend the static import for the method Filters() be included (import static com.mongodb.client.model.Filters.*;).

  • The results should resemble something that looks like the following:
1
2
Document{{_id=5cebb0223fd48a338db1746e, name=Oda Nobunaga, country=Japan, rating=15}}
Document{{_id=5cebb0223fd48a338db1746f, name=Cao Cao, country=China, rating=19}}

MongoDB Query Operator $not

  • The operator $not selects documents that do not match the specified criteria.
1
2
3
4
5
6
7
8
9
10
11
MongoDatabase db = mongo.getDatabase("warlordDB");
FindIterable<document> findIt = db.getCollection("warlordCollection").find(not(eq("name","Cao Cao")));

MongoCursor<document> cursor = findIt.iterator();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
  • The results should resemble something that looks like the following:
1
2
3
4
5
Document{{_id=5cebb0223fd48a338db1746b, name=Chang Kai-shek, country=china, rating=11}}
Document{{_id=5cebb0223fd48a338db1746c, name=Sun Yat-sen, country=china, rating=8}}
Document{{_id=5cebb0223fd48a338db1746d, name=Muammar al-Gaddafi, country=Libya, rating=15}}
Document{{_id=5cebb0223fd48a338db1746e, name=Oda Nobunaga, country=Japan, rating=15}}
Document{{_id=5cebb0223fd48a338db17470, name=Sun Tzu, country=China, rating=10}}

MongoDB Query Operator $nor

  • The $nor command performs a logical NOR operation in multiple-array expressions and will only select documents that will fail all the set expressions in the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MongoDatabase db = mongo.getDatabase("warlordDB");
FindIterable<document> findIt = db.getCollection("warlordCollection").find(
nor
(
eq("name","Cao Cao"),
eq("name","Oda Nobunaga"),
lt("rating", 10)
)
);

MongoCursor<document> cursor = findIt.iterator();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
  • Note there are several filters used within the nor operation to produce more complex query results when using MongoDB Logical Query in Java.

  • The results should resemble something that looks like the following:

1
2
3
Document{{_id=5cebb0223fd48a338db1746b, name=Chang Kai-shek, country=china, rating=11}}
Document{{_id=5cebb0223fd48a338db1746d, name=Muammar al-Gaddafi, country=Libya, rating=15}}
Document{{_id=5cebb0223fd48a338db17470, name=Sun Tzu, country=China, rating=10}}

MongoDB Query Operator $and

  • The $and operation will join filters with a logical AND operator and will only select documents that match both the set conditions of the filters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MongoDatabase db = mongo.getDatabase("warlordDB");
FindIterable<document> findIt = db.getCollection("warlordCollection").find(
and
(
eq("name","Cao Cao"), gt("rating",8)
)
);

MongoCursor<document> cursor = findIt.iterator();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
  • The above code will only retrieve documents that have a rating field value that is greater than eight and also have a country field equivalent to “China”.

    • The results should resemble something that looks like the following:
1
2
Document{{_id=5cebb0223fd48a338db1746f, name=Cao Cao, country=China, rating=19}}
Document{{_id=5cebb0223fd48a338db17470, name=Sun Tzu, country=China, rating=10}}

Conclusion

This tutorial explained three basic ways of going about using MongoDB Logical Query in Java. To summarize, the $or operation will only select the documents that meet the criteria of at least one of the defined expressions, the $nor command will only select documents that will fail all the set expressions in the array and the $and operation will select documents that match both the set conditions of the filters. Bear in mind that the examples in this tutorial were executed using MongoDB version 4.0 and MongoDB Java Driver version 3.8.2. Using other versions of either may not produce similar results. Also remember users must have the latest version of Java JDK properly installed and configured in order to use MongoDB Logical Query in Java.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.