NodeJS MongoDB Select

MongoDB database has one or many collections in which records are present, called documents in MongoDB.

The documents can be selected from a MongoDB database in NodeJS using the find() and findOne() methods. 

Selecting MongoDB Documents in NodeJS

MongoDB database is implemented in NodeJS using the MongoDB module, which provides a MongoClient which then has a method connect() to connect to the database, inside the callback it returns a database object which is used to select a collection using collection() method, when done selecting a collection then the methods like find() and findOne() can use accordingly to select the document from the respective collection.

Methods to Select Documents in NodeJS

In NodeJS to select documents from a collection of a MongoDB database, we have two methods find() and findOne().

Using the findOne() Method to Select a Single Document

This method selects a single document from a collection. If there are many documents then it selects the first one.

This method can take an object and a callback function as an argument. Inside the object, the MongoDB query can be passed to filter the selection. We have covered MongoDB Query in another tutorial if you want to read it. Inside the callback, it returns the error if occurs and the selected document.

Syntax:

database.collection(collectionName).findOne({}, function(err, document) {
    if (err) throw err;
    console.log(document);
  });

where,

  • database is a database object,
  • collectionName is the name of a collection

Example

We have multiple documents in a collection.

Documents

If you want to learn to insert multiple records in a collection then read our previous tutorial NodeJS MongoDB Insert Record.

Below is an example to select a single record from a collection.

const { MongoClient } = require('mongodb');

MongoClient.connect("mongodb://localhost:27017/", function (err, client) {
  if (err) throw err;
  const database = client.db("newdatabase");

  database.collection("newcollection").findOne({}, function(err, document) {
    if (err) throw err;
    console.log(document);
  });
});

Run the Code:

To run the above code, create a project folder, and copy and paste the above code into a JavaScript file.

Then locate the folder in the terminal and execute the below command to install the MongoDB module.

npm install mongodb

Then execute the below command to run the code.

node fileName

where fileName is the name of the JavaScript file where you have inserted the code.

Output:

Select Single Document

Using the find() Method to Select Multiple Documents

This method selects multiple documents. It can select all the documents from a collection. It is equivalent to SELECT * in MySQL.

This method takes a query object as an argument used to filter the documents to select the documents on the basis of some conditions if the query is not passed then select all the documents from a collection. This method returns an object which can be converted into an array containing each document as a separate object.

Syntax:

database.collection(collectionName).find({}).toArray(function(err, documentsArray) {
    if (err) throw err;
    console.log(documentsArray);
});

where,

  • database is a database object,
  • collectionName is the name of a collection

Example

Below is an example to select multiple records from a collection.

const { MongoClient } = require('mongodb');

MongoClient.connect("mongodb://localhost:27017/", function (err, client) {
  if (err) throw err;
  const database = client.db("newdatabase");

  database.collection("newcollection").find({}).toArray(function(err, documentsArray) {
    if (err) throw err;
    console.log(documentsArray);
  });
});

Run the Code:

To run the above code, create a project folder, and copy and paste the above code into a JavaScript file.

Then locate the folder in the terminal and execute the below command to install the MongoDB module.

npm install mongodb

Then execute the below command to run the code.

node fileName

where fileName is the name of the JavaScript file where you have inserted the code.

Output:

Select All Documents

Summary

Documents are selected from a collection of a MongoDB database in NodeJS using the find() and findOne() methods. find() method can select multiple documents whereas findOne() can select only one of many documents. This method also takes a query to filter the selected document. Read out the next tutorial to learn about MongoDB Query. Hope this one helps you to understand the methods to select MongoDB documents in NodeJS.

Reference

https://www.mongodb.com/docs/drivers/node/current/usage-examples/findOne/

https://www.mongodb.com/docs/drivers/node/current/usage-examples/find/

Aditya Gupta
Aditya Gupta
Articles: 109