- Comparison query operators
- $eq
- $gt
- $gte
- $lt
- $lte
- $in
- $ne
- $nin
Comparison query operators
Comparison query operators allow you to compare the elements in a document to a given query value.
Go to the comparison query operators:
Operator| Description
---|---
$eq| Selects documents with elements that are equal to a given query value
$gt| Selects documents with elements that are greater than a given query value
$gte| Selects documents greater than or equal to specified query
$lt| Selects documents with elements that are less than a given query value
$lte| Selects documents with elements less than or equal to given query value
$in| Selects documents that contain the elements in a given array query
$ne| Selects documents with elements that are not equal to given query value
$nin| Selects documents that do not contain the elements in a given array query
For the examples in this section, insert the following documents into the employees collection:
1. db.employees.insertMany([
2. {
3. name: {
4. first: 'Earl',
5. last: 'Thomas'
6. },
7. employeeID: 1234,
8. age: 23,
9. role: 'salesperson',
10. catalog: ['printer', 'cardboard', 'crayons', 'books']
11. },
12. {
13. name: {
14. first: 'Sam',
15. last: 'Johnson'
16. },
17. employeeID: 2234,
18. age: 35,
19. role: 'salesperson',
20. catalog: ['cabinet', 'fridge', 'blender', 'utensils']
21. },
22. {
23. name: {
24. first: 'Clarke',
25. last: 'Dane'
26. },
27. employeeID: 3234,
28. age: 21,
29. role: 'salesperson',
30. catalog: ['printer', 'pencils', 'crayons', 'toys']
31. }
32. ])
$eq
Syntax : { <field>: { $eq: <element> } }
To select documents that exactly match a given query value, use the $eq operator.
This operator can be used to match values of different types, including documents, array, embedded documents, etc.
Example: The following operation queries the employees collection for all documents where the field age equals 21.
1. db.employees.find({
2. age: {
3. $eq: 21
4. }
5. })
The response returns a single document that matches the query:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0e"),
4. name: { first: 'Clarke', last: 'Dane' },
5. employeeID: 3234,
6. age: 21,
7. role: 'salesperson',
8. catalog: ['printer', 'pencils', 'crayons', 'toys']
9. }
10. ]
Example: To query values in an embedded document, use dot notation. The following operation queries the employees collection for documents that match the field first in the embedded document name.
1. db.employees.find({
2. 'name.first': {
3. $eq: 'Earl'
4. }
5. })
The response returns a single document that matches the query:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. }
10. ]
$gt
Syntax : { <field>: { $gt: <element> } }
To identify documents containing elements that have a greater value than the specified one in the query, use the $gt operator.
Example: Use the following operation to query for all the documents in the employees collection where the field age is greater than 21.
1. db.employees.find({
2. age: {
3. $gt: 21
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. },
10. {
11. _id: ObjectId("639a3cce071b6bed396a8f0d"),
12. name: { first: 'Sam', last: 'Johnson' },
13. employeeID: 2234,
14. age: 35,
15. role: 'salesperson',
16. catalog: ['cabinet', 'fridge', 'blender', 'utensils']
17. }
18. ]
$gte
Syntax : { <field>: { $gte: <element> } }
Use the $gte to select document with elements that are greater than or equal to a specified value.
Example: The following operation selects documents based on the specified query, where the field age is greater than or equal to 21.
1. db.employees.find({
2. age: {
3. $gte: 21
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. },
10. {
11. _id: ObjectId("639a3cce071b6bed396a8f0d"),
12. name: { first: 'Sam', last: 'Johnson' },
13. employeeID: 2234,
14. age: 35,
15. role: 'salesperson',
16. catalog: ['cabinet', 'fridge', 'blender', 'utensils']
17. },
18. {
19. _id: ObjectId("639a3cce071b6bed396a8f0e"),
20. name: { first: 'Clarke', last: 'Dane' },
21. employeeID: 3234,
22. age: 21,
23. role: 'salesperson',
24. catalog: ['printer', 'pencils', 'crayons', 'toys']
25. }
26. ]
$lt
Syntax : { <field>: { $lt: <element> } }
Contrary to the $gt operator, the $lt operator is ideal for selecting documents with elements that are of a lesser value than that of the specified query.
Example: The following operation queries for documents where the field age is less than 25.
1. db.employees.find({
2. age: {
3. $lt: 25
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. },
10. {
11. _id: ObjectId("639a3cce071b6bed396a8f0e"),
12. name: { first: 'Clarke', last: 'Dane' },
13. employeeID: 3234,
14. age: 21,
15. role: 'salesperson',
16. catalog: ['printer', 'pencils', 'crayons', 'toys']
17. }
18. ]
$lte
Syntax : { <field>: { $lte: <element> } }
The $lte operator is the opposite of the $gte operator. Use the $lte operator to select documents with elements that are less than or equal to the specified query value.
Example: The following operation queries for documents where the field age is less than or equal to 21.
1. db.employees.find({
2. age: {
3. $lte: 21
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0e"),
4. name: { first: 'Clarke', last: 'Dane' },
5. employeeID: 3234,
6. age: 21,
7. role: 'salesperson',
8. catalog: ['printer', 'pencils', 'crayons', 'toys']
9. }
10. ]
$in
Syntax : { <field>: { $in: [<element1>, <element2>, ... <elementN> ] } }
To select documents containing any of the listed elements in a specified array field, use the $in operator.
Example: The following operation queries the employees collection for documents where the value of the field age is either 21 or 35.
1. db.employees.find({
2. age: {
3. $in: [21, 35]
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0d"),
4. name: { first: 'Sam', last: 'Johnson' },
5. employeeID: 2234,
6. age: 35,
7. role: 'salesperson',
8. catalog: ['cabinet', 'fridge', 'blender', 'utensils']
9. },
10. {
11. _id: ObjectId("639a3cce071b6bed396a8f0e"),
12. name: { first: 'Clarke', last: 'Dane' },
13. employeeID: 3234,
14. age: 21,
15. role: 'salesperson',
16. catalog: ['printer', 'pencils', 'crayons', 'toys']
17. }
18. ]
$ne
Syntax : { <field>: { $ne: <element> } }
Use the $ne operator to select all the documents with elements that are not equal to a given query.
Example: The following operation queries the employees collection for documents where the field age is not equal to 21.
1. db.employees.find({
2. age: {
3. $ne: 21
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. },
10. {
11. _id: ObjectId("639a3cce071b6bed396a8f0d"),
12. name: { first: 'Sam', last: 'Johnson' },
13. employeeID: 2234,
14. age: 35,
15. role: 'salesperson',
16. catalog: ['cabinet', 'fridge', 'blender', 'utensils']
17. }
18. ]
$nin
Syntax : { <field>: { $nin: [ <element1>, <element2> ... <elementN> ] } }
The $nin does exactly the opposite of the $in operator. Use the $nin operator when selecting documents that do match or contain any of the elements listed in an array query.
Example: The following operation queries the employees collection for documents where the value of the field age is not 21 or 35.
1. db.employees.find({
2. age: {
3. $nin: [21, 35]
4. }
5. })
The output:
1. [
2. {
3. _id: ObjectId("639a3cce071b6bed396a8f0c"),
4. name: { first: 'Earl', last: 'Thomas' },
5. employeeID: 1234,
6. age: 23,
7. role: 'salesperson',
8. catalog: ['printer', 'cardboard', 'crayons', 'books']
9. }
10. ]
