MongoDB find multiple values

In this MongoDB tutorial, we will learn how to find multiple values in MongoDB. We will also cover this with different examples. These are some of the topics that we are going to cover in this tutorial:

  • How to find multiple values in MongoDB
  • MongoDB query find multiple values
  • MongoDB find multiple values in the array
  • MongoDB find between two values
  • MongoDB find multiple values same field
  • MongoDB find multiple values greater than
  • MongoDB find multiple values less than
  • MongoDB find not equal multiple values
  • MongoDB find matches multiple values
  • MongoDB compass find multiple values

How to find multiple values in MongoDB

MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents.

To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself. When we have large data set and we only want to find the documents of a particular value in that situation we use aggregation operations.

MongoDB query find multiple values

In this topic, we discuss the query that is used to find multiple values or documents from the documents.

Here, we also cover an example that will help you to understand how to find out the documents with multiple values.

Syntax:

db.Collection_name.find(query, projection)
ParameterTypeDescription
querydocumentUse this parameter to return all the documents of a collection.
projectiondocumentUse to return documents that match the query filter. It is used to return all fields in the matching documents.

The projection parameter takes the documents in the following form:

{ field1: <value>, field2: <value> ... }

Example:

The following documents were inserted into the fruits collection:

db.fruits.insertMany([
{ "_id": 1, "name": "apples", "qty": 5 },
{ "_id": 2, "name": "bananas", "qty": 7 },
{ "_id": 3, "name": "oranges", "qty": { "in stock": 8, "ordered": 12 } },
{ "_id": 4, "name": "avocados", "qty": "fourteen" }
])

After inserting the documents into the fruit collection apply the following query to find multiple values.

db.fruits.find( { qty: { $gt: 4}} )

Here, we used the find() method and apply the $gt operator in the parameter to return values where the qty is greater than 4.

The query will return the following result:

MongoDB query find multiple values
MongoDB query find multiple values

Read: How to store images in MongoDB

MongoDB find multiple values in the array

In this section, we will learn about MongoDB find multiple values in the array. The user will provide a list of array fields to find multiple values or documents and the query will return all of them. Let’s understand with the help of an example:

Example:

We have inserted the following documents into the info collection:

db.info.insertMany([
   {
       "_id" : 1,
       "name" : {
           "first" : "John",
           "last" : "Backus"
       },
       "contribs" : [
           "Fortran",
           "ALGOL",
           "Backus-Naur Form",
           "FP"
       ]    
   },
   {
       "_id" : 2,
       "name" : {
           "first" : "John",
           "last" : "McCarthy"
       },
       "contribs" : [
           "Lisp",
           "Artificial Intelligence",
           "ALGOL"
       ]
   },
   {
       "_id" : 3,
       "name" : {
           "first" : "Grace",
           "last" : "Hopper"
       },
       "title" : "Rear Admiral",
       "contribs" : [
           "UNIVAC",
           "compiler",
           "FLOW-MATIC",
           "COBOL"
       ]
   }
       
])

Now, you have to apply the following query to return documents in the bios collection where the array field contribs contains the element “ALGOL” or “Lisp”.

db.info.find( { contribs: { $in: [ "ALGOL", "Lisp" ]} } ).pretty()

The query will return the following output:

MongoDB find multiple values in the array
MongoDB find multiple values in the array

Read: MongoDB group by count

MongoDB find between two values

In this topic, we will learn to find multiple values or documents between two values. The condition could be:

  • Display all documents between two dates.
  • Display all documents with the price value between 100 and 200

Let’s understand this with the help of an example:

Example:

The following documents were inserted into the marks collection:

db.marks.insertMany([
{ "_id": 1, "subject": "History", "score": 88 },
{ "_id": 2, "subject": "Science", "score": 92 },
{ "_id": 3, "subject": "Math", "score": 97 },
{ "_id": 4, "subject": "English", "score": 88 },
{ "_id": 5, "subject": "Hindi", "score": 69 },
{ "_id": 6, "subject": "Social Science", "score": 80 },
{ "_id": 7, "subject": "Computer", "score": 73 }
])

Now, we will apply the below query to find all the documents where the score values are in between 75 to 100.

db.marks.find({ "score": { "$gt": 75, '$lt': 100} })
MongoDB find between two values
MongoDB find between two values

Read: How to check if MongoDB is installed

MongoDB find multiple values same field

In this topic, we will read about how we can find all the multiple values or documents with the same field with the help of an example.

Example:

The following documents were inserted into the store collection:

db.store.insertMany([
{"_id": 1, "subject": "coffee", "author": "xyz", "views": 50 },
{"_id": 2, "subject": "Coffee Shopping", "author": "efg", "views": 5 },
{"_id": 3, "subject": "Baking a cake", "author": "abc", "views": 90 },
{"_id": 4, "subject": "baking", "author": "xyz", "views": 100 },
{"_id": 5, "subject": "Café Con Leche", "author": "abc", "views": 20 }
])

After that find all the multiple values same field by applying the following query:

db.store.find({ "author": { "$in": ["xyz"]} })

Here, we define all the parameters in the find() method and return all values where the author is xyz. The $in operator elects the documents where the value of a field equals any value in the specified array.

MongoDB find multiple values same field
MongoDB find multiple values same field

Read: MongoDB sort by date

MongoDB find not equal multiple values

In this topic, we will read about how we can find all the multiple values or documents that are not equal with the help of an example.

Example:

These documents we are going to insert into the store collection:

db.store.insertMany([
{ "_id": 1, "subject": "coffee", "author": "xyz", "views": 50 }
{ "_id": 2, "subject": "Coffee Shopping", "author": "efg", "views": 5 }
{ "_id": 3, "subject": "Baking a cake", "author": "abc", "views": 90 }
{ "_id": 4, "subject": "baking", "author": "xyz", "views": 100 }
{ "_id": 5, "subject": "Café Con Leche", "author": "abc", "views": 20 }
])

Now, we will apply the below query to the store collection and find not equal multiple values:

db.store.find({ "author": { "$nin": ["xyz"]} }).sort( {subject: 1} )

Here, we use the $nin (not in) comparison query operator in the find() method. The use of the $nin operator is to select documents where the value of the field is not equal to any of the given values in the array and the field that does not exist. After that, we use the sort() method to sort the documents in ascending order as the subject field.

MongoDB find not equal multiple values
MongoDB find not equal multiple values

Read: MongoDB sort by field

MongoDB find multiple values greater than

Sometimes when we have a lot of data in the database and we only have to return those documents that are greater than a particular number.

In this topic, we will learn to find multiple values or documents that are greater than with the help of an example.

Example:

Here, we use the same collection as we used in the above topic. For reference check the above store collection.

MongoDB find multiple values greater than
MongoDB find multiple values greater than

Now, we apply the following query and find only those multiple values or documents where views are greater than 50.

db.store.find({ "views": { "$gt": 50 } })
MongoDB find multiple values are greater than
MongoDB find multiple values greater than

Read: Create tables in MongoDB

MongoDB find multiple values less than

In this topic, we will learn to find all the multiple values or documents that are less than. Let’s understand with the help of an example:

Example:

In this example, we use the same collection store as we used the above example and find out all the values where views are less than 50.

For reference see the documents stored in the MongoDB database.

MongoDB find multiple values greater than
MongoDB find multiple values greater than

Now, we will apply the following query to find out all the values where views are less than 50.

db.store.find({ "views": { "$gt": 50 } })
MongoDB find multiple values less than
MongoDB find multiple values less than

Read: Pros and cons of MongoDB

MongoDB find matches multiple values

In this topic, we will learn to find matches multiple values in MongoDB. This operation performs when we have several data set and we only have to return matches multiple values. Let’s make out with the help of an example.

Example:

The following documents will insert into the person collection:

db.person.insertMany([
{"name" : "James", age : 25, "gender" : "male", "city": "United States"},
{"name" : "John", age : 28, "gender" : "male", "city": "Canada"},
{"name" : "Mary", age : 33, "gender" : "female", "city": "Canada"},
{"name" : "David", age : 22, "gender" : "male", "city": "Australia"},
{"name" : "Jessica", age : 27, "gender" : "female", "city": "USA"},
{"name" : "Daniel", age : 27, "gender" : "male", "city": "United Kingdom"},
]);

After inserting the documents into the database, we will apply the following query to find matches multiple values from the documents.

db.person.find({ "gender": "male" }).sort({ "name": -1 })

Here, we match the values and return only those documents where the gender value is Male and also apply the sort() method for displaying the documents in descending order as per the name field.

MongoDB find matches multiple values
MongoDB find matches multiple values

Read: MongoDB backup and restore

MongoDB compass find multiple values

The MongoDB Compass is also a powerful tool where we can perform all the operations querying, aggregation, analyzing the data in the virtual environment. In this topic, we will learn how we can find multiple values from the documents with the help of an example.

Example:

To understand with help of an example you have to follow the below steps and you will easily find multiple values from the database collection.

  • Open MongoDB Compass and connect to server.
  • Select the database and collection or either create a new database and collection and insert the documents.
  • Here, I am using an existing database and collection, Company and artists respectively.
MongoDB compass find multiple values
MongoDB compass find multiple values
  • Click on OPTIONS drop-down button on the left side of the FIND button.
  • And it provides various option that can perform FILTER, PROJECT, SORT, and COLLATION.
  • Now, we define following two queries and find multiple values from the documents as per the condition.
{ year_died: { $lt: 1940} }    # Define in FILTER section


{ first_name: 1 }              # Define in SORT section

Here, In the FILTER section, we define a query that displays documents where the year_died values are less than since 1940 and the SORT section define query means sorting the documents in ascending order (1) according to the first_name field.

  • After applying the query click on the FIND button and this will find or return all multiple values.
MongoDB compass find multiple values from documents
MongoDB compass find multiple values

We successfully find multiple values from the documents using MongoDB Compass.

Also, take a look at some of the latest MongoDB articles.

In this tutorial, you have learned about “MongoDB find multiple values” with different operations and examples. And we have also covered the following list of topics:

  • How to find multiple values in MongoDB
  • MongoDB query find multiple values
  • MongoDB find multiple values in the array
  • MongoDB find between two values
  • MongoDB find multiple values same field
  • MongoDB find multiple values greater than
  • MongoDB find multiple values less than
  • MongoDB find not equal multiple values
  • MongoDB find matches multiple values
  • MongoDB compass find multiple values