Node Js MongoDB Multiple Queries

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

Introduction

This article is about how with Node JS and MongoDB to do multiple queries by combining query arguments into the find() method.

Query in MongoDB is the part that is used to match documents in a collection. Sometimes, we do not need to perform an operation on all the documents, we only need some of them. So, we have the query for it. Usually, the first argument of a method in MongoDB is the query part. It is a mandatory argument. If we wish to apply the method on the whole collection, then we just pass an empty object as a query. But sometimes, we need to perform multiple queries. Observe the following collection.

1
2
3
4
5
6
{ "_id" : ObjectId("5e36e646b07fe088bcb23c60"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5e36e64ab07fe088bcb23c61"), "name" : "Sam", "age" : 27, "location" : "Chicago" }
{ "_id" : ObjectId("5e36e657b07fe088bcb23c62"), "name" : "John", "age" : 24, "location" : "Washington DC" }
{ "_id" : ObjectId("5e36e66bb07fe088bcb23c63"), "name" : "John", "age" : 21, "location" : "Detroit" }
{ "_id" : ObjectId("5e36e66fb07fe088bcb23c64"), "name" : "Lisa", "age" : 23, "location" : "Texas" }
{ "_id" : ObjectId("5e36e67bb07fe088bcb23c65"), "name" : "John", "age" : 29, "location" : "New York" }

This is the details collection. Suppose, we need to find a specific person whose name is “John”. Now, there are multiple documents where the value of the name field is “John”. So here, if we do something like below, it won’t work.

1
db.details.find({ name: "John" });

What do you think will be the result when this query is executed? Let’s see.

1
2
3
4
5
6
> db.details.find({"name" : "John"})
{ "_id" : ObjectId("5e36e646b07fe088bcb23c60"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5e36e657b07fe088bcb23c62"), "name" : "John", "age" : 24, "location" : "Washington DC" }
{ "_id" : ObjectId("5e36e66bb07fe088bcb23c63"), "name" : "John", "age" : 21, "location" : "Detroit" }
{ "_id" : ObjectId("5e36e67bb07fe088bcb23c65"), "name" : "John", "age" : 29, "location" : "New York" }
>

Well, it gives us all the Johns in the details collection, but it does not give us the specific “John” we need. Now to get the perfect result, we need to provide more information in the query. So, in this article, we will discuss how to use multiple queries in MongoDB.

Multiple queries in MongoDB

When we gave the following query, it did not provide favorable results.

1
db.details.find({ name: "John" });

Say, the John we are looking for is twenty-one years old. So alongside the above query, we need to provide another one. How can we do this? It is very simple. We just add another condition separated by a comma.

1
db.details.find({ name: "John", age: 21 });

This time, we have more than one query. Let’s execute this.

1
2
3
4
> db.details.find({"name" : "John", "age" : 21})
{ "_id" : ObjectId("5e36e646b07fe088bcb23c60"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5e36e66bb07fe088bcb23c63"), "name" : "John", "age" : 21, "location" : "Detroit" }
>

Look, the result is shortened. Now, we have two documents in the result. But still, we have two documents where the value of the name field is “John” and both of them have 21 as the value of the age field. Suppose, the John we are looking for is twenty-one years old and he is in “New York”. This means we need to add another condition to the query. Let’s add it as we did earlier.

1
db.details.find({ name: "John", age: 21, location: "New York" });

Now we have three conditions here. Let’s execute it.

1
2
3
> db.details.find({"name" : "John", "age" : 21, "location" : "New York"})
{ "_id" : ObjectId("5e36e646b07fe088bcb23c60"), "name" : "John", "age" : 21, "location" : "New York" }
>

And finally, we have the John we wanted from the start.

Conclusion

So this is how we use multiple queries in MongoDB. It is the same in nodejs and mongoose. All we have to is separate the condition with a comma.

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.