In this article I’ll tell you about how to connect to the MongoDB using NodeJS with Mongoose and MongoDB drivers. As well as Save data and Find data in MongoDB.
For work on this post You need to install NodeJS and MongoDB
NodeJS — https://nodejs.org
MongoDB — https://www.mongodb.com
If You do not want to install MongoDB You can use mLab.com. This service provides 500 Mb of MongoDB for Free
First, we need to install all necessary packages for work. Create a package.json
file
{
"name": "name",
"main": "server.js",
"dependencies" : {
"express": "4.13.4",
"mongoose": "4.5.0",
"cors": "2.7.1"
}
}
and run npm install
to install packages.
Learn more about these packages you can on NPM Website
Express — Fast, unopinionated, minimalist web framework — https://www.npmjs.com/package/express
Mongoose — Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment — https://www.npmjs.com/package/mongoose
Cors — Middleware for dynamically or statically enabling CORS in express/connect applications — https://www.npmjs.com/package/cors
There is also a MongoDB driver, I’ll tell you about it in this article, but I prefer to use the Mongoose because it provides the opportunity to create the database schema
After successful install packages, in project folder create server.js
file and in your terminal or command prompt run MongoDB with mongod
command.
Use Mongoose Driver
server.js
var express = require('express');
var cors = require('cors');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var app = express();
var schemaName = new Schema({
request: String,
time: Number
}, {
collection: 'collectionName'
});
var Model = mongoose.model('Model', schemaName);
mongoose.connect('mongodb://localhost:27017/dbName');
app.get('/save/:query', cors(), function(req, res) {
var query = req.params.query;
var savedata = new Model({
'request': query,
'time': Math.floor(Date.now() / 1000) // Time of save the data in unix timestamp format
}).save(function(err, result) {
if (err) throw err;
if(result) {
res.json(result)
}
})
})
app.get('/find/:query', cors(), function(req, res) {
var query = req.params.query;
Model.find({
'request': query
}, function(err, result) {
if (err) throw err;
if (result) {
res.json(result)
} else {
res.send(JSON.stringify({
error : 'Error'
}))
}
})
})
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log('Node.js listening on port ' + port);
});
Let’s analyze this code for details
On lines 1 — 6 we include all the necessary dependencies
var express = require('express');
var cors = require('cors');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var app = express();
On lines 7 — 12 create database schema
At the end of this article I will show how to use mongodb command line to access the data
var schemaName = new Schema({
request: String,
time: Number
}, {
collection: 'collectionName'
})
On lines 14 and 15 create model and connect to MongoDB
var Model = mongoose.model('Model', schemaName);
mongoose.connect('mongodb://localhost:27017/dbName');
If you decide to use mLab just replace mongodb://localhost:27017/dbName
with database URL. For example:
mongoose.connect('mongodb://dbuser:dbpassword@ds055555.mlab.com:55555/dbName');
On lines 18 — 31 is implemented a route which we will use to insert data to the database
app.get('/save/:query', cors(), function(req, res) {
var query = req.params.query;
var savedata = new Model({
'request': query,
'time': Math.floor(Date.now() / 1000)
}).save(function(err, result) {
if (err) throw err;
if(result) {
res.json(result)
}
})
})
Here, to variable query
input request /:query
which then stored to a database into request
key
var savedata = new Model({
'request': query,
...
Then, if you save the data error has occurred, you will receive an error on the console. If all goes successful you will see the stored data in JSON format on the page.
...
}).save(function(err, result) {
if (err) throw err;
if(result) {
res.json(result)
}
})
On lines 33 — 48 has a function find for search data in database. In request
key
app.get('/find/:query', cors(), function(req, res) {
var query = req.params.query;
Model.find({
'request': query
}, function(err, result) {
if (err) throw err;
if (result) {
res.json(result)
} else {
res.send(JSON.stringify({
error : 'Error'
}))
}
})
})
As in a save
function, request is recorded to query
variable /:query
then passed to the function find
to search
Model.find({
'request': query
If an error occurs when you search you will see it in the console. If all goes correctly you will get all the documents in JSON format containing the current query in request
key
}, function(err, result) {
if (err) throw err;
if (result) {
res.json(result)
} else {
res.send(JSON.stringify({
error : 'Error'
}))
}
})
Finally we create a server
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log('Node.js listening on port ' + port);
})
Usage
To save you can just go in your browser to the following URL
http://localhost:8080/save/:query
where :query
it’s your request for save. Example:
http://localhost:8080/save/JavaScript is Awesome
Output
For convenient viewing of JSON in your browser, you can use JSONView Chrome Extension
For use find you can just go to the browser to the following URL
http://localhost:8080/find/:query
where :query
it’s your request for find. Example:
http://localhost:8080/find/JavaScript is Awesome
Output
Use MongoDB Driver
First, Install mongodb driver with npm install mongodb
. And replace code in your server.js
file with:
var express = require('express');
var cors = require('cors');
var mongo = require('mongodb');
var app = express();
mongo.connect('mongodb://localhost:27017/dbName', function(err, db) {
db.createCollection('collectionName');
var collectionName = db.collection('collectionName');
var saveObject = {};
app.get('/save/:query', cors(), function(req, res) {
var query = req.params.query;
saveObject = {
"request": query,
"time": Math.floor(Date.now() / 1000)
}
res.send(saveObject)
collectionName.save(saveObject, function(err) {
if (err) throw err;
})
})
app.get('/find/:query', cors(), function(req, res) {
var query = req.params.query;
collectionName.find({'request': query}).toArray(function (err, result) {
if (err) throw err;
res.send(result);
})
})
})
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log('Node.js listening on port ' + port);
})
Quickly analyze that code
MongoDB Driver
var mongo = require('mongodb');
Connect and create collection
mongo.connect('mongodb://localhost:27017/dbName', function(err, db) {
db.createCollection('collectionName');
var collectionName = db.collection('collectionName');
...
...
})
Save
saveObject = {
"request": query,
"time": Math.floor(Date.now() / 1000)
}
res.send(saveObject)
collectionName.save(saveObject, function(err) {
if (err) throw err;
})
Find
collectionName.find({'request': query}).toArray(function (err, result) {
if (err) throw err;
res.send(result);
})
MongoDB Command Line Interface
Open another terminal and run the command mongo
to open MongoDB CLI
\>mongo
MongoDB shell version: 3.2.0
connecting to: test
\>_
To see a list of existing databases, use the command show dbs
\>mongo
MongoDB shell version: 3.2.0
connecting to: test
\> show dbs
dbName 0.000GB
local 0.000GB
\>_
In this article, as the database name I used a dbName
. To use this database, run the command use dbName
\>mongo
MongoDB shell version: 3.2.0
connecting to: test
\> show dbs
dbName 0.000GB
local 0.000GB
\> use dbName
switched to db dbName
\>_
Show collections with show collections
\>mongo
MongoDB shell version: 3.2.0
connecting to: test
\> show dbs
dbName 0.000GB
local 0.000GB
\> use dbName
switched to db dbName
\> show collections
collectionName
\>_
We see that in the database dbName
have a collection named collectionName
which contains our data. Let’s read this data using the command db.collectionName.find().pretty()
\>mongo
MongoDB shell version: 3.2.0
connecting to: test
\> show dbs
dbName 0.000GB
local 0.000GB
\> use dbName
switched to db dbName
\> show collections
collectionName
\> db.collectionName.find().pretty()
{
"_id" : ObjectId("578abe97522ad414b8eeb55a"),
"request" : "JavaScript is Awesome",
"time" : 1468710551
}
{
"_id" : ObjectId("578abe9b522ad414b8eeb55b"),
"request" : "JavaScript is Awesome",
"time" : 1468710555
}
{
"_id" : ObjectId("578abea0522ad414b8eeb55c"),
"request" : "JavaScript is Awesome",
"time" : 1468710560
}
\>_