For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentHow to Create MongoDB Database in Node.js

How to Create MongoDB Database in Node.js

Published
05th Sep, 2023
Views
view count loader
Read it in
15 Mins
In this article
    How to Create MongoDB Database in Node.js

    MongoDB is an open-source platform written in C++. It is a document database designed for ease of development and scaling. It is a cross-platform, document-oriented and non-structured database. 

    This article is a quick guide on how to create MongoDB database in Node.js. We will understand MongoDB compatibility and language compatibility and exploreMongo shell. We will see how to import data into the database, how to use MongoDB driver for node.js and the Node.js based CRUD operations. We will conclude with building a simple app using node express app with MongoDB to store and serve content. 

    How MongoDB is different from relational databases 

    Relational databases are SQL enabled and are organised in tables with each entry containing a specific set of columns with a specific data type.  

    It is seen that sometimes relational databases become very complex for simple schemas, for e.g., where an employee who has more than one phone number, email IDs, addresses, teams, etc. and all the related data is stored in different tables. To retrieve complete information for an employee, multiple tables needs to be joined and queried.  

    One of the disadvantages with relational databases is that to update or change schema, we would need to modify multiple tables. As the requirement changes, more updates might be needed, further complicating the design. This makes it necessary for a dedicated DB admin. 

    Mongo is a NoSql DB and is object-based containing an entire entity in document record. Unlike relational database, NoSqldb can be queried faster and easy to get complete details of a specific entity or document.  

    Another feature of Mongo is if there is no document matching to query than no error is returned. In relational database, a table contains the same set of fields whereas in Mongo DB, schema is dynamic and each one need not conform to the same schema.Mongo is developer friendly and JSON representation makes it easy to parse. 

    MongoDB stores documents in collections. Collections are analogous to tables in relational databases. In MongoDB, databases hold collections of documents. 

    In addition to collections, MongoDB supports: 

    • Read-only Views (Starting in MongoDB 3.4) 
    • On-Demand Materialized Views (Starting in MongoDB 4.2). 

    MongoDB data structure 

    • Documents – JSON Objects 
    • Store data as BSON (Binary JSON) [For the BSON spec, see bsonspec.org] 

    The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For example, the following document contains values of varying types 

    var mydoc = {_id: ObjectId("3423jksdhfi83p9fj90cd"), 
    name: { firstName: "Sachin", lastName: "Tendulkar" }, 
    birth: new Date('Apr 24, 1973'), 
    awards:[ “Bharat Ratna”, “Padma Vibhushan”, “Padma Shri”] 
    followers :NumberLong(30000000) 
    } 
    • _id holds an ObjectId,  
    • name holds an embedded document that contains firstName and lastName, 
    • birth hold values of Date types 
    • awards holds an array of strings 
    • followers holds a value of NumberLong type. 

    Note: Mongo supports nesting up to 100 levels. 

    Learn more about bson-types at https://docs.MongoDB.com/manual/reference/bson-types/ 

    Setup MongoDB environment

    FormacOS platform, download the tgz file and extract the file. Easiest way to get the items in your path is to copy them to usr/local/bin directory. Follow the below steps to get it done

    How to Create MongoDB Database in Node.js

    Now type ‘mongod’ in command prompt which is mongo daemon and  you can see it has started. Invoke another command prompt and type ‘mongo’ to start off in the shell. To make sure everything is working let’s insert a dummy data and observe that it inserted one document. 

    db.users.insert({“name”: “Sachin Tendulkar”})

    How to Create MongoDB Database in Node.js

    Database is all setup. 

    For Windows platform, download the msi and run the installation to complete the installation. Once the installation is done, you can find where it has been installed. Two things important in bin folder are mongo and MongoD. Copy the path and add to the path in environment variables.  

    Once this has been done, we should be able to run both the Mongo Server and Mongo Shell from command prompt.  

    Invoke the command prompt and do ‘md \data’ and then create data DB by ‘md \data\db’. This is the file that mongo is going to look for to use to store information about database.  

    Now type ‘mongoD’ in command prompt which is mongo daemon and  you can see it has started. Invoke another command prompt and type ‘mongo’ to start off in the shell. To make sure everything is working let’sinsert a dummy data and observe that it inserted one document. 

    db.users.insert({“name”: “Sachin Tendulkar”}) 

    Database is all setup. 

    The recommended way to get started using the Node.js driver is by using NPM (Node Package Manager) to install the dependency in your project. 

    After you’ve created your project with npminit, you can install the MongoDB driver and its dependencies with the command: 

    npm install MongoDB --save 

    This will download the MongoDB driver and add a dependency entry in your package.json file. 

    We will learn more of this as we go forward. 

    Compatibility 

    MongoDB compatibility 

    Note: The driver does not support older versions of MongoDB

    Node.js DriverMongoDB 4.2MongoDB 4.0MongoDB 3.6MongoDB 3.4MongoDB 3.2MongoDB 3.0MongoDB 2.6
    >= 3.X
    >= 3.2.1
    >= 3.1
    >= 3.0

    >=2.2.12


    Language compatibility

    Node.js
    Driver
    Node.js v0.8.XNode.js v0.10.XNode.js v0.12.XNode.js v4.X.XNode.js v6.X.XNode.js v8.X.XNode.js v10.x.xNode.js v12.x.x
    >=3.X.X


    2.2.X – 2.0.X

    >=1.4.18




    1.4.x





    Few salient points 

    When designing the schema for your database, a quick understanding of Embedded vs References is helpful. You can refer Coderwall for more information on this.  

    Three of the other features that mongo provides: Indexing, Sharding and replicationcan make Mongo the right choice for your application. 

    Indexing- supports ad hoc queries 

    • 64 indices per collection 
    • Single Field 
    • Compound (multiple Fields) 
    • Unique 

    Sharding  - Scalability 

    • Partitions data onto different machines 
    • Scale application into smaller systems 
    • Autosharding supported by Mongo 
    • Challenging to set up

    Replication

    • Reliability (Primary and n number of secondary server) 
    • Maximizes uptime 
    • Replica sets 
    • Automatic failover

    Explore Mongo Shell 

    • Note:All the examples and snaps are taken in macOS. 
    • Invoke terminal and type ‘mongod’ which will display the listening port 27017

    How to Create MongoDB Database in Node.js

    • Sinncewe are running locally, we need not secure and optimize it 
    • Invoke another terminal (command + T) and type ‘mongo’

    How to Create MongoDB Database in Node.js

    You should see that we are now connected to MongoDB://127.0.0.1:27017 

    By default, we are connected to test database  

    • Type ‘db’ to check it in terminal and observe ‘test’ 

    Let us use a different database say ‘MyNewMongoDB’ 

    • Type ‘use MyNewMongoDB’ 
    • Output: Switched to dbMyNewMongoDB 

    To view all available dbs, type ‘show dbs’.

    How to Create MongoDB Database in Node.js

    Note: We will not see the new db ‘MyNewMongoDB’. Instead the db is created when the first document is created. 

    Mongo stores data in databases which are separated into collections of documents. To Create a new collection, we need to insert a document and the collection will be created as a side effect. 

    • Type ‘db.cricketers.insert({“name”:”Sachin”}) 
    • Now check ‘show dbs’ to view our new db 
    • Type ‘show collections’ to check that our collection is created.

    How to Create MongoDB Database in Node.js

    As you can see, mongo doesn’t require any schema or setup work to start working with data. The commands we used are very similar to what we have in SQL but mongo shell uses a JavaScript interpreter. So, we can interact with database using JavaScript expressions making it easy to perform various tasks. 

    • Print(‘test’) will print test 
    • var arr = [“one”, “two”, “three”] 
    • type arrwhich will print the results. 

    Let us take this option to explore further i.e. we will insert 1000 records to db using a for loop.

    How to Create MongoDB Database in Node.js

    for(i=0;i<1000;i++){ db.numbers.insert({"number":i}) } 

    Though it says there was only one insert, it’s counting the number of operations and not individual documents. This is evident from the command ‘db.numbers.count()’ 

    Now let’s try to find a single document from the database and understand the query better. 

    • Type ‘db.numbers.find({"number": 2})’ and this should display the one record. 
    • Let’s ask the mongo to explain it for us by adding the explain command.
    • db.numbers.find({“number”:”2”}).explain() 

    How to Create MongoDB Database in Node.js

    • Explain did mentioned about the query planner but didn’t give much information about the execution. So, let’s pass execution status to get that information. 
    • db.numbers.find({“number”:”2”}).explain(“executionStats”)

    How to Create MongoDB Database in Node.js

    • Now we can see that it ran through 999 records and took 0 milliseconds. May be if we increase the count to 1 million we can find higher time in executionTime. 
    • Since we don’t want to run through all the records, let’s create a simple index to see how the outcome changes. 
    • db.numbers.createIndex({number: 2}) 
    • Here it is similar to find but the number is a special variable and it is not a string. 

    Now, let us try execution status again and see the result.

    How to Create MongoDB Database in Node.js

    As we can see, only one document is examined and imagine if we want to search in a collection of 10 million or more records. It makes a huge difference. 

    Import data into the database: 

    Though inserting data into mongo using shell or language driver is handy. Most of the time we have the data in some form which we need to import to our database. 

    And Mongoimport command can import the data in json, csv or tsv formats and no custom code needed.In our case, we are using random data generated using

    Sample view of the data downloaded:

    How to Create MongoDB Database in Node.js

    ‘mongoimport –help’ will display the options available. 

    • Namespace options display the db and the collection to which the data has to be imported. 

    • Input options is next section in which we will use the file option. Json format is default one here. However, the file being imported is an array and not an object so we need to dash dash json array flag as well. 

    Invoke the terminal and make sure we are in the file directory where the json file resides or downloaded.

    How to Create MongoDB Database in Node.js

    As we can see, we have imported 7 documents successfully.  

    Now let us check if mongo has the data what we wanted 

    • Switch to mongo client and type ‘use MyNewMongoDB’ 
    • Type ‘show collections’ to view the collections now having persons. 
    • We can count records using commanddb.persons.count() or find using db.persons.find({“isActive”: true}). 

    Mongo shell CRUD operations 

    Now that we have the data in place, let’s explore the standard CRUD operations in database.If client is not running, start it by typing mongo in terminal and switch to newly created database ‘MyNewMongoDB’ 

    • [READ] In the available data,let’s find out the number of persons having eyecolor blue. 
      • GET: db.persons.find({“eyecolor”: “blue”}).count() 
      • This should result in count of 2 records. 
    • [CREATE] Now let’s go and add one more person with eyecolor blue. (INSERT) 
      • Insert is going to be little complex and might span across lines based on the data we insert.  
    db.persons.insert({ "_id": "5eee4d58c952ec90ad43dfe8", "index": 7, "guid": "fbc2510e-678f-4843-b612-96b899ee5e9b", "isActive": true, "balance": "$3,996.47", "picture": "http://placehold.it/32x32", "age": 18, "eyeColor": "blue", "name": "SteuPoluard", "gender": "female", "company": "EXTRAWEAR"}) 

    Note: In the above case, it is spanning multiple lines and Mongo is not going to execute/process until the last parenthesis isclosed and also _id is unique. 

    • Output should display one new record is inserted. 

    WriteResult({ "nInserted" : 1 }) 
    • If we try to find the people with eye color blue, we should get 3 records.

    How to Create MongoDB Database in Node.js

    • UPDATE] Now let’s go and update the inserted record with an email Id. 
    • Updates can happen to replace the entire document or a sub-set of the data. We wanted to add the email id without losing the entire object.For this we need to use the set functionality.
    db.persons.update({"name":"SteuPoluard"}, 
    ... {$set: {"email":"Steu.Poluard@extrawear.com"}}) 

    Output: 

    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 
    • Now if we find the record to check if email is been added.

    How to Create MongoDB Database in Node.js

    • Now if we want to update the tags then we need to use a different operator called $addToSet. 

    db.persons.update({"name":"Sara Pollard"}, 
    ... { 
    ... $addToSet: {"tags":"newtag"} 
    ... })

    How to Create MongoDB Database in Node.js

    Note: There is a large set of update operators are available, refer to mongo site to explore this functionality in depth. 

    • [DELETE] Now lets finally delete the record added with name – ‘SteuPoluard’ 

    db.persons.remove({"name":"SteuPoluard"}) 
    • To verify, let’s see if find command finds it. 

    db.persons.find({"name":"SteuPoluard"}) 

    No result. So, the record got successfully deleted. 

    • Say if we want to delete the collection itself. 

    db.persons.drop() 
    • Check show collections to find the persons collection is no more.

    Using MongoDBdriver for node.js 

    Now let’s write some code in node.js to interact with the database. The drivers for all of the languages work similarly so once we have it working for node. Other language drivers should be pretty much same. 

    The official MongoDB Node.js driver provides both callback-based and Promise-based interaction with MongoDB, allowing applications to take full advantage of the new features in ES6. The 2.x series of the driver is powered by a brand-new core driver and BSON library. To understand more about the changes made from drive 1.x to 2.x, refer to thisblog

    First step is to make sure our Mongo server is up and running with db ‘MyNewMongoDB’ and the imported data collection i.e. person already in place. 

    Note: In case if the collection is dropped, please follow the above steps to create and import the data

    Steps:

    • Create an index.js file  
    • Setup the mongo client using MongoDB package. 
    var MongoClient = require('MongoDB').MongoClient; 
    • Url for the database uses the MongoDB protocol and connects to the specified server. 

    var url = 'MongoDB://localhost:27017/MyNewMongoDB'; 
    • Finally, we want to connect to the MongoClient and in the callback we log the connection is made successfully

    varMongoClient = require('MongoDB').MongoClient; 
    varurl = 'MongoDB://localhost:27017/MyNewMongoDB'; 
    MongoClient.connect(url, function(err, db) { 
    console.log("Connected successfully to server"); 
    db.close(); 
    }) 

    Invoke a terminal and navigate to the index.js file folder. 

    Type node index.js and observe that ‘Connected successfully to server’ 

    Note: If you are facing any error, check if MongoDB server is running and npm install is performed. 

    Now, let’s go ahead and try to access all the documents or specific document and print them in console. Since we have already made the connection. Let’s find the record in the callback of the connection. 

    varMongoClient = require('MongoDB').MongoClient; 
    // Server is running locally on the default port and db is name is 'MyNewMongoDB' 
    varurl = 'MongoDB://localhost:27017/MyNewMongoDB'; 
    varfindDocuments = function(db, callback) { 
    // collection name is 'persons' which is imported 
    varcollection = db.collection('persons'); 
    // looking for a document with field 'name' and value 'Sara Pollard' 
    collection.find({"name":"Sara Pollard"}).toArray(function(err, docs) { 
    console.log(docs); 
    callback(); 
        }) 
    } 
    MongoClient.connect(url, function(err, db) { 
    console.log("Connected successfully to server"); 
    // On obtaining the connection, find the record and close the db connection. 
    findDocuments(db, function () { 
    db.close(); 
        }) 
    }) 

    Type node index.js to check if the record is obtained successfully and is logged.

    How to Create MongoDB Database in Node.js

    As we can see, the same interfaces covered in the mongoShell work here as well for all CRUD operations. For sake of review, here is the list 

    • Create --> insert. 
    • Read --> find or findOne. 
    • Update --> update. 
    • Delete --> delete. 

    Node.js based CRUD operations 

    Let ussetup up a simple application using Node.js and MongoDB. We would understand how to setup the driver and perform simple CRUD operations. 

    Follow the below steps to setup project 

    • mkdirlearnMongoDB 
    • cd learnMongoDB 
    • npminit [you would be prompted for inputs related to package.json] 
    • npm install MongoDB–save [--save is to update the package.json] 

    Assuming that the mongo db server is still running. If not, start as mentioned earlier. 

    Open the directory learnMongoDB in your favourite IDE. (I am using Visual Studio code). Add a file app.js with the below snippet of code. 

    // import the MongoDB and get the client object. 
    constMongoClient = require('MongoDB').MongoClient; 
    constassert = require('assert'); 
    // Connection URL - can be obtained in the MongoDB server 
    consturl = 'MongoDB://localhost:27017'; 
    // Database Name  
    constdbName = 'myproject'; 
    // Create a new MongoClient 
    constclient = newMongoClient(url); 
    // Use connect method to connect to the Server 
    client.connect(function(err) { 
    assert.equal(null, err); 
    console.log("Connected successfully to server"); 
    constdb = client.db(dbName); 
    client.close(); 
    }); 

    Observe that we are not providing the db name in the URL. Instead we are getting the db instance on successfully connecting to the server. This way, we can get connected to different databases in the same client instances. 

    Note: We are using assert module to check if connection is successful or not by validating the error object. Will be used for other CRUD operations as well. 

    Run ‘node app.js’ and should observe the log mentioning connected successfully to server. 

    Insertion: Now we have the db object, let’s get hold of the collection and insert some objects into it. 

    Create a function ‘insertDocuments’ to perform insertion into the collection.Call this insertDocuments in the callback of successful connection. 

    // insertDocuments take the db instance 
    constinsertDocuments = function(db, callback) { 
    // Get the documents collection 
    constcollection = db.collection('documents'); 
    // Insert some documents 
    collection.insertMany([ 
          {a :1}, {a :2}, {a :3} 
        ], function(err, result) { 
    assert.equal(err, null); // assertion to check if any error is there. 
    assert.equal(3, result.result.n); 
    assert.equal(3, result.ops.length); 
    console.log("Inserted 3 documents into the collection"); 
    callback(result); // return the inserted documents 
        }); 
    } 
    // Use connect method to connect to the Server 
    client.connect(function(err) { 
    assert.equal(null, err); 
    console.log("Connected successfully to server"); 
    constdb = client.db(dbName); 
    //insert the documents and in the callback, close the client. 
    insertDocuments(db, function(result) { 
    console.log(result); 
    client.close(); 
      }) 
    }); 

    In the callback of the insertDocuments, we get the result object which is as shown below: 

    { result: { ok: 1, n: 3 }, 

    ops: 

    [ { a: 1, _id: 5eeefd2a16f61641f58a9418 }, 
    { a: 2, _id: 5eeefd2a16f61641f58a9419 }, 
    { a: 3, _id: 5eeefd2a16f61641f58a941a } ], 
    insertedCount: 3, 
    insertedIds: 
    { '0': 5eeefd2a16f61641f58a9418, 
    '1': 5eeefd2a16f61641f58a9419, 
    '2': 5eeefd2a16f61641f58a941a } } 

    As you can see, result object gives an overview of the action. Ops is the objects added to the collection. And other fields are self-explanatory. 

    Note: You might have noticed that a new field ‘_id’ has been added which is generated by MongoDB for unique identification. If we are not happy with the unique id, we can as well supply like we did during importing of the data. 

    Find: Let’s check if documents added can be retrieved using find. 

    Collection object has a method called ‘find’ which take a parameter i.e. search query to identify the objects. If it is empty then all documents are returned.Invoke the findDouments in the callback of insertion for validating the records inserted. We can use a query filter to get specific item as well. 

    // findDocuments from the db instance 
    constfindDocuments = function(db, callback) { 
    // Get the documents collection 
    constcollection = db.collection('documents'); 
    // Finds all documents. If required, parameterize the query filter to get specific items. 
    collection.find({}).toArray(function(err, docs) { 
    assert.equal(err, null); 
    console.log("Found the following records"); 
    console.log(docs) 
    callback(docs); 
        }); 
      } 
    // Use connect method to connect to the Server 
    client.connect(function(err) { 
    assert.equal(null, err); 
    console.log("Connected successfully to server"); 
    constdb = client.db(dbName); 
    //insert the documents and in the callback, close the client. 
    insertDocuments(db, function(result) { 
    findDocuments(db, function() { 
    client.close(); 
        })     
      }) 
    }); 

    For example, if we want to find a specific set of documents based on pre-defined query filter like ‘{‘a’:3}’ : 

    // Find specific document with a query filter 
    constfindDocuments = function(db, callback) { 
    // Get the documents collection 
    const collection = db.collection('documents'); 
    // Find some documents 
    collection.find({'a': 3}).toArray(function(err, docs) { 
    assert.equal(err, null); 
    console.log("Found the following record(s)"); 
        console.log(docs); 
    callback(docs); 
      }); 
    } 

    Updation: Now go andupdate a specific document. There are various methods available for updation like update, updateMany, updateOne etc. Use updateOne method to update the first one which matches the query. 

    Signature: updateOne(filter, update, options, callback) 

    Let’s send the filter as {a : 2 } i.e. document whose field a is equal to 2, update as $set: {b:1} to avoid replacement of entire object i.e. adding a new field b to document with value set as 1. Ignore options which is an optional and have a callback to capture post actions. 

    constupdateDocument = function(db, callback) { 
    // Get the documents collection 
    constcollection = db.collection('documents'); 
    // Update document where a is 2, set b equal to 1 
    collection.updateOne({ a :2 } 
          , { $set: { b :1 } }, function(err, result) { 
    assert.equal(err, null); 
    assert.equal(1, result.result.n); 
    console.log("Updated the document with the field a equal to 2"); 
    callback(result); 
        });   
      }   
    // Use connect method to connect to the Server 
    client.connect(function(err) { 
    assert.equal(null, err); 
    console.log("Connected successfully to server"); 
    constdb = client.db(dbName); 
    //insert the documents and in the callback, close the client. 
    insertDocuments(db, function(result) { 
    findDocuments(db, function() { 
    updateDocument(db, function() { 
    client.close(); 
            }) 
        })    
      }) 
    }); 

    Deletion: Let’s use deleteOne method to find and delete the first item with field a and value 3. 

    constremoveDocument = function(db, callback) { 
    // Get the documents collection 
    constcollection = db.collection('documents'); 
    // Delete document where a is 3 
    collection.deleteOne({ a :3 }, function(err, result) { 
    assert.equal(err, null); 
    assert.equal(1, result.result.n); 
    console.log("Removed the document with the field a equal to 3"); 
    callback(result); 
        });     
      } 
    // Use connect method to connect to the Server 
    client.connect(function(err) { 
    assert.equal(null, err); 
    console.log("Connected successfully to server"); 
    constdb = client.db(dbName); 
    //insert the documents and in the callback, close the client. 
    insertDocuments(db, function(result) { 
    findDocuments(db, function() { 
    updateDocument(db, function() { 
    removeDocument(db, function(){ 
    client.close(); 
                }) 
            }) 
        }) 
      }) 
    }); 

    Note: All the methods on the collection returns promise object if callback is not passed. So to avoid callback hell as we see above, we can rely on promise and program the code better. 

    Node express app with MongoDB to store and serve content 

    Now that we have fair idea of how to work with MongoDB using mongoshell and node.js driver. Let us build a simple app using node express app with MongoDB to store and serve content. 

    Note: Since this topic is more about MongoDB rather than express, we will keep the express app a basic version. 

    Let us go and comment out all the code in the app.js file from the previous example. We should be left with this: 

    // import the MongoDB and get the client object. 
    constMongoClient = require('MongoDB').MongoClient; 
    const { equal } = require('assert'); 
    // Connection URL - can be obtained in the MongoDB server 
    consturl = 'MongoDB://localhost:27017'; 
    // Database Name  
    constdbName = 'myproject'; 

    Invoke a terminal and navigate to the directory where the app.js file is located. 

    Let us install express with the below command. 

    • npm install express –save 

    Import the express and body-parser as shown below: 

    constexpress = require('express'); 
    constbodyParser = require('body-parser'); 
    create an app instance and use bodyParser.json(). 
    constapp = express(); 
    app.use(bodyParser.json()); 
    Let the express app be listening on port 3000 with a default route. 
    app.get('/', (req, res) =>res.send('Express app with MongoDB!')); 
    app.listen(3000, () =>console.log("App listening at http://localhost:3000")); 

    Consolidated code: 

    constexpress = require('express'); 
    constbodyParser = require('body-parser'); 
    // import the MongoDB and get the client object. 
    constMongoClient = require('MongoDB').MongoClient; 
    const { equal } = require('assert'); 
    // Connection URL - can be obtained in the MongoDB server 
    consturl = 'MongoDB://localhost:27017'; 
    // Database Name  
    constdbName = 'myproject'; 
    constapp = express(); 
    app.use(bodyParser.json()); 
    app.get('/', (req, res) =>res.send('Express app with MongoDB!')); 
    app.listen(3000, () =>console.log("App listening at http://localhost:3000")); 

    In the terminal, run ‘node app.js’ and should see the console log that app is listening in port 3000. To check the default route is working correctly. Invoke the postman and perform get operation.

    How to Create MongoDB Database in Node.js

    Now let us create a new route to retrieve the document based on field a and parameterized value. 

    app.get('/api/documents/:name', (req,res) => { 
    }) 

    :name is the parameter being passed. So let’s retrieve it from request parameters. And since we know it is numeric and not string. Let’s parse it to integer. 

    constname = parseInt(req.params.name); 

    Now lets connect to the MongoClient as earlier. This time we will not be using callback approach rather rely on the promise being returned. 

    constclient = awaitMongoClient.connect(url, {useNewUrlParser:true});

    Since returned object is a promise and in order for the promise to be resolved or rejected. We need to use await keyword before proceeding further. 

    Note: Promise object has two properties i.e. state and result. Promise state property initially has ‘pending’ and post execution will be either ‘fulfilled’(resolve) or ‘rejected’(reject). Similarly, result will be ‘undefined’ initially and then changes to ‘value’[resolve(value)] or ‘error’[reject(error)]. 

    Let us get the db ‘myproject’ and find the first document based on the query filter from collection using findOne method. 

    constdb = client.db(dbName); 
    constdocument = awaitdb.collection('documents').findOne({"a":name}); 

    Once we get the document, lets return the status as 200 with the obtained document and close the client connection. 

    res.status(200).json(document); 
    client.close(); 

    As we are using await keyword, we need to add async keyword to the callback. And also lets add a try-catch block to return an error status in case of connection failure as internal server error. 

    app.get('/api/documents/:name',async (req,res) => { 
    try {    
    constname = parseInt(req.params.name); 
    constclient = awaitMongoClient.connect(url, {useNewUrlParser:true}); 
    constdb = client.db(dbName); 
    constdocument = awaitdb.collection('documents').findOne({"a":name}); 
    res.status(200).json(document); 
    client.close();       
        } catch(error) { 
    res.status(500).json({message:"Error connecting to db", error}); 
        } 
    }) 

    Now we are good to go and restart our express app. Press ctrl + c and run node app.js to see app is listening at. Make sure that MongoDB Server is up and running in the background. 

    Switch to postman and perform a GET operation. This should return the first record in the collection as shown below. 

    {"_id":"5eeefd2a16f61641f58a9419","a":2,"b":1} 

    Same GET request can be performed for other documents by changing the name as 2

    How to Create MongoDB Database in Node.js

    Similar to get operations, we can configure the routes to perform other CRUD operations on MongoDB to store and serve the content.  

    Conclusion 

    In this guide, we have gone overMongoDB compatibility and language compatibility and explored the Mongo shell. We have seen how to import data into the database, how to use MongoDB driver and the Node.js based CRUD operations. We have also looked at how to build a simple app using node express app with MongoDB to store and serve content.

    Armed with this knowledge, you are ready to take a deeper dive into MongoDB and NodeJS. Here’s wishing you the best! 

    Real coding requires real exercises. Practice live coding with our programming  experts on live workshops now.

    Profile

    Sumanth Reddy

    Author

    Full stack,UI Architect having 14+ Years of experience in web, desktop and mobile application development with strong Javascript/.Net programming skills .Strong experience in microsoft tech stack and Certified in OCI associate. Go-to-guy on integration of applications, building a mobile app by zeroing on tech stack. Majorily experienced on engineering based IIOT products and Marekting Automation products deployed on premise and on-cloud. 

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon