Node Js with MongoDB Query Examples for Developers

Introduction

In this tutorial our hope is to demonstrate some basic query examples. Specifically we want to demo Node Js with MongoDB query examples that can be used as a starting point for understanding more complex queries. Let’s jump in!

MongoDB provides several methods that can be used with collections. Almost each of these methods has a query filter part. The query filter part determines how the resulting documents will be filtered. Let’s understand with the help of the demo data set below. It is a collection called smarthphones:

{ "_id" : ObjectId("5e12d83b86d564bd4487658a"), "name" : "Apple Iphone 11", "price" : 1000, "inStock" : true }
{ "_id" : ObjectId("5e12d84c86d564bd4487658b"), "name" : "Samsung S10", "price" : 800, "inStock" : true }
{ "_id" : ObjectId("5e12d86186d564bd4487658c"), "name" : "Apple Iphone 11 pro", "price" : 1200, "inStock" : false }
{ "_id" : ObjectId("5e12d8bf86d564bd4487658d"), "name" : "Huawei P30 pro", "price" : 900, "inStock" : false }
{ "_id" : ObjectId("5e12d91486d564bd4487658e"), "name" : "Apple Iphone 11 pro max", "price" : 1400, "inStock" : true }

Query Examples

The smartphones collection has five documents. We can use the find() method to retrieve all these documents. But suppose, we need only those documents where the value of the field inStock is true. Then what? For such cases where we need to filter the result, we have the query filter part. A query filter is an object that is passed to a method as the first parameter. The condition(s) is specified inside this object.

db.smartphones.find({ inStock: true });

Observe the object that is passed to the find() method. When this query will be executed, it will return only those documents where the value of the inStock field is true. Let’s execute it through the mongo shell.

Image from Gyazo

There are various ways of specifying a query. We can give as many conditions, or we can use operators as well. So, suppose we need all the documents where the value of the inStock field is true and the value of the name field is “Apple Iphone 11”. This time we have more than one condition. To do this, we simply specify conditions separated by commas. Let’s do it.

db.smartphones.find({ inStock: true, name: "Apple Iphone 11" });

Image from Gyazo

Yes! It returns only those documents where the value of the inStock field is true and the name field is Apple Iphone 11. Let’s modify this example a bit. Say, we want all those documents where the value of the inStock field is true or the value of the name field is “Apple Iphone 11”. How can we do this? The $or operator. The or operator returns true even if one of the given condition is true. Technically, the last example we discussed was regarding the and operator. It returned only those documents that matched all the conditions.

Now, let’s try with the $or operator.

db.smartphones.find({ $or: [{ inStock: true }, { name: "Apple Iphone 11" }] });

The $or operator takes an array of objects as its value and it matches all the documents that even match a single condition.

Image from Gyazo

Suppose, we want all those documents where the value of the price field is either 800, 1000 or 1200. We can go for the $or operator here too, but MongoDB provides a better operator for such cases. This operator is called $in operator. It takes an array as its value. Let’s try it.

db.smartphones.find({"price" : {$in : [800, 1000,1200}})

This query will match all the documents where the value of the price field is either 800 or 1000 or 1200.

Image from Gyazo

Let’s move further now. We also have comparison operators in MongoDB Such as $gt (greater than), $lt (less than), $gte (greater than equal to), etc. Let’s match all those documents where the value of the price field is greater than 1000. We can use a $gt operator.

db.smartphones.find({"price" : {$gt : 1000})

Image from Gyazo

There are many ways of defining the query in MongoDB. It all depends upon the requirements. All these methods and query filters we discussed are also supported in Nodejs. There is absolutely no difference.

Conclusion

We hope that these examples of queries with NodeJS and MongoDB were of some assistance. We have articles with details on more complex queries but we hope this was a good starting point for your journey into more complex queries.

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.