How to Perform a Node Js MongoDB Select Query

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

Introduction

Right up-front we want to let you know that there is no select query in MongoDB. The select query is used in SQL based systems but there is an equivalent in MongoDB which we will introduce you to.

How to Perform the Equivalent of a SQL Select Query with NodeJS and MongoDB

Almost all SQL queries have their equivalent queries in MongoDB. Beginners often get confused about this. In SQL, we use the select statement for reading records from tables. The equivalent of the select statement in MongoDB is the find() method. It is very easy to use. In this article, we will compare the SQL select statement with its equivalent, find() in MongoDB.

In SQL, we have tables. In MongoDB, we have collections. We will use the smartphones collection shown below to do our demo.

1
2
3
4
5
{ "_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 }

Observe the following SQL query.

1
SELECT * FROM smartphones

This query will retrieve all the records from the smartphones table. Let’s see what is the equivalent of this query in MongoDB.

1
db.smartphones.find();

Let’s execute this using the mongo shell.

The * in the SQL query means select all the columns of a row. Similarly in the find() method, we did not pass any parameter. It means select all the fields in a document. Now, suppose we want to select only some certain columns of a row. In SQL, we use the following approach.

1
SELECT name, price FROM smartphones

This time, the records will contain only two columns – name and price. Let’s see what is the equivalent of this in MongoDB.

1
db.smartphones.find({}, { name: 1, price: 1 });

We passed two parameters to the find() method. Both of them are objects. Ignore the first object, we will discuss it later. The second parameter is called projection.

1
{name: 1, price: 1}

It will return all the documents and these documents will contain the name, price and _id fields only. Yes! The auto-generated _id field is always there. To remove it from the result, we have to use the following approach.

1
db.smartphones.find({}, { name: 1, price: 1, _id: 0 });

Now, let’s discuss the empty object that was passed to the find method earlier.

1
db.smartphones.find({});

This statement is similar to the following statement.

1
db.smartphones.find();

Both of them will return all the documents in the smartphones collection. The first parameter of the find() method is the query. It is an object that may or may not contain conditions according to which all the documents will be filtered. Suppose, we want only those documents where the value of the inStock field is true. First, let’s see how this is done in SQL.

1
2
SELECT * FROM smartphones
WHERE inStock = "true"

And this is how we do it in MongoDB.

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

Let’s combine both query and projection in find method.

1
db.smartphones.find({ inStock: true }, { name: 1, price: 1, _id: 0 });

This statement will return all the documents where the value of the inStock field in true and these documents will contain only name and price fields.

The equivalent to this in SQL is:

1
2
SELECT name, price FROM smartphones
WHERE inStock = "true"

Conclusion

The equivalent of a select in sql based databases is the find() method in MongoDB. We showed you some sql queries and their equivalents in MongoDB and hope that you now understand how they work. Thanks for dropping in for another ObjectRocket tutorial!

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.