- Node.js can be used in database applications.
- One of the most popular NoSQL databases is MongoDB.
- To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct IP address and the name of the database you want to create.
- MongoDB will create the database if it does not exist, and make a connection to it.
create a database called "my_data"
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/my_data";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Creating a Collection
- To create a collection in MongoDB, use the createCollection( )
method
Create a collection called "customers"
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("my_data");
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
Insert into Collection
- To insert a record, or document as it is called in MongoDB, into a collection, we use the insertOne( ) method.
- A document in MongoDB is the same as a record in MySQL
- The first parameter of the insertOne( ) a method is an object containing the name(s) and value(s) of each field in the document you want to insert.
- It also takes a callback function where you can work with any errors or the result of the insertion:
Example :
Insert a document in the "customers" collection:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("my_data");
var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
Insert Multiple Documents
- To insert multiple documents into a collection in MongoDB, we use the insertMany( ) method.
- The first parameter of the insertMany( ) a method is an array of objects, containing the data you want to insert.
- It also takes a callback function where you can work with any errors or the result of the insertion:
Example :
Insert multiple documents in the "customers" collection:
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("my_data");
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'}
];
dbo.collection("customers").insertMany(myobj, function(err, res) {
if (err) throw err;
console.log("Number of documents inserted: " + res.insertedCount);
db.close();
});
});
Node.js MongoDB Find
- In MongoDB we use the find and findOne methods to find data in a collection.
- Just like the SELECT statement is used to find data in a table in a MySQL database.
- To select data from a collection in MongoDB, we can use the
findOne()
method.
- The
findOne()
method returns the first occurrence in the selection.
- The first parameter of the
findOne()
method is a query object. In this example we use an empty query object, which selects all documents in a collection (but returns only the first document).
Find the first document in the customers collection:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("customers").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});
Find All
No comments:
Post a Comment