Node.js MongoDB
- Node.js can be used in database applications.
- One of the most popular NoSQL database is MongoDB.
Creating a Database(table)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Save the code above in a file called "demo_create_mongo_db.js" and run the file:
C:\Users\Your Name>node demo_create_mongo_db.js
Which will give you this result:
Database created!
Creating a Table
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Table created!");
db.close();
});
});
Save the code above in a file called "demo_create_mongo_table.js" and run the file:
C:\Users\Your Name>node demo_create_mongo_table.js
Which will give you this result:
Table created!
Insert Into Tabe
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = { name: "Company Inc", address: "Highway 37" };
db.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 record inserted");
db.close();
});
});
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = [
{ name: 'John', address: 'Highway 71'},
{ name: 'Peter', address: 'Lowstreet 4'},
{ name: 'Amy', address: 'Apple st 652'},
{ name: 'Hannah', address: 'Mountain 21'},
{ name: 'Michael', address: 'Valley 345'},
{ name: 'Sandy', address: 'Ocean blvd 2'},
{ name: 'Betty', address: 'Green Grass 1'},
{ name: 'Richard', address: 'Sky st 331'},
{ name: 'Susan', address: 'One way 98'},
{ name: 'Vicky', address: 'Yellow Garden 2'},
{ name: 'Ben', address: 'Park Lane 38'},
{ name: 'William', address: 'Central st 954'},
{ name: 'Chuck', address: 'Main Road 989'},
{ name: 'Viola', address: 'Sideway 1633'}
];
db.collection("customers").insert(myobj, function(err, res) {
if (err) throw err;
console.log("Number of records inserted: " + res.insertedCount);
db.close();
});
});
Save the code above in a file called "demo_mongodb_insert.js" and run the file:
C:\Users\Your Name>node demo_mongodb_insert.js
Which will give you this result:
1 record inserted
Select One from form
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("customers").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});
Save the code above in a file called "demo_mongodb_findone.js" and run the file:
C:\Users\Your Name>node demo_mongodb_findone.js
Which will give you this result:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("customers").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Filter the Result
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var query = { address: "Park Lane 38" };
db.collection("customers").find(query).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Save the code above in a file called "demo_mongodb_query.js" and run the file:
C:\Users\Your Name>node demo_mongodb_query.js
Which will give you this result:
[
{ _id: 58fdbf5c0ef8a50b4cdd9a8e
, name: 'Ben', address: 'Park Lane 38' }
]
No comments:
Post a Comment