Inserting a JSON document
The NoSQL writes are speedy as compared to the SQL write operation. This is because you don’t have to maintain any schema from the start and its relevant data types.
Let us continue with our test-node-app
and implement a POST API call that sets the customer's data in the collection.
The stepwise procedure is as follows:
- By now, we have the mongoDb connection instance inside the
app.js
file. So first we need to remove the following code fromapp.js
. Keep the code in some temp file as we are going to use it later:
const MongoClient = require('mongodb').MongoClient; MongoClient.connect(constants.mongodb.url) .then(function() { console.log("Connected successfully to mongodb server") }) .catch(function(err) { console.log("An error occurred while connecting to mongodb!", err) })
- We are going to create a new piece of middleware that will use the existing DB connection to make a query to the particular...