One of the most popular NoSQL database is MongoDB and is a perfect fit for Node.JS applications.
Node.js can be used in database applications.Lets get started
- You have to install the package
- Install via npm
- npm install mongodb
- Next lets connect MongoDB with Node.js
- After installing package, go to your Node.js, create a variable and call the MongoDB to work
- var MongoClient = require('mongodb').MongoClient;
- example code:
- var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
}); - The Db.connect method here, use a uri (Uniform Resource Identifier) to connect to the Mongo database, where localhost:27017 is the server host and port and exampleDb the db we wish to connect to.
- After the url notice the hash containing the auto_reconnect key , Auto reconnect tells the driver to retry sending a command to the server if there is a failure during its execution.
- Creating DB using Node.js
- Use the MongoDM db creating command into Node.js file
- Create a database called "uki":
- Sample Code:
- var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
}); - Save this file [eg: Uki.js] and go to Terminal, start MongoDB, and run to open this file like this node uki.js you'll got a out put Database created!
- Creating Table
- To create a table in MongoDB, we use the method:
createCollection()
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.createCollection("Students", function(err, res) {
if (err) throw err;
console.log("Table created!");
db.close();
});
});- Save this file [eg: Uki.js] and go to Terminal, start MongoDB, and run to open this file like this node uki.js you'll got a out put Table created!
Insert Records into Table- Sample Code:
- var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = { name: "Abiramy", address: "Sullipuram" }; db.collection("Students").insertOne(myobj, function(err, res) { if (err) throw err;
console.log("1 record inserted");
db.close();
});
}); - Save this file [eg: Uki.js] and go to Terminal, start MongoDB, and run to open this file like this node uki.js you'll got a out put 1 record inserted
- To add many Records in a time:
- MongoClient.connect(url, function(err, db) {
if (err) throw err;var myobj = [{ name: 'Mino', address: 'Jaffna'},{ name: 'Sheno', address: 'Jaffna'},{ name: 'Shama', address: 'Jaffna'},{ name: 'Janu', address: 'Jaffna'},{ name: 'Dialni', address: 'Karainagar'}, ]; db.collection("Students").insert(myobj, function(err, res) {if (err) throw err;
console.log("Number of records inserted: " + res.insertedCount);
db.close();
});
}); - Find Command
- To select data from a table in MongoDB, we can use the .find
()
method. - To find all
- var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/uki";
MongoClient.connect(url, function(err, db) {if (err) throw err;db.collection("Students").find({}).toArray(function(err, result) {if (err) throw err;console.log(result);
db.close();
});
}); Using Variables- MongoClient.connect(url, function(err, db) {
if (err) throw err;var stu1 = { address: "Jaffna" };db.collection("Students").find(stu1).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});
}); Sorting- Use to sort Data s
- { name: 1 } // ascending
- { name: -1 } // descending
- var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var mysort = { name: 1 };
db.collection("students").find().sort(mysort).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
No comments:
Post a Comment