NODE.JS + MONGODB
Open your terminal and make a new folder.
mkdir mongotest
Run this command to make a recent Node project. It’s a good practice.
npm init --y
Let’s install the official MongoDB node module.
npm install --save mongodb
Connecting to MongoDB
Here is the code to connect to the MongoDB using Node.
const mongo = require('mongodb');
const url = "mongodb://localhost:27017";
mongo.connect(url, {useNewUrlParser: true}, (err, db) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log('database connected!');
db.close();
});
Next, we need to select the database and create our collection in it.
Here is that the code to do the same.
const mongo = require('mongodb');
const url = "mongodb://localhost:27017";
mongo.connect(url, {useNewUrlParser: true}, (err, db) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log('database connected!');
var dbo = db.db('codeforgeek');
dbo.createCollection('users', (err, result) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log('collection created!');
db.close();
});
});
After a successful MongoDB connection, we choose the MongoDB database.
var dbo = db.db('codeforgeek');
Then, we try to create a collection naming users within the codeforgeek database.
Inserting data in the collection
Let’s add some entries in our collection. consider the code below.
const mongo = require('mongodb');
const url = "mongodb://localhost:27017";
mongo.connect(url, {useNewUrlParser: true}, (err, db) => {
if(err) {
console.log(err);
process.exit(0);
}
let data = [{
"id": 100,
"name": "Shahid"
},{
"id": 101,
"name": "Rahil"
},{
"id": 102,
"name": "John"
}];
var dbo = db.db('codeforgeek');
console.log('database connected!');
var collection = dbo.collection('users');
collection.insertMany(data, (err, result) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(result);
db.close();
});
});
In the code shown on top of, we are using insertMany() function to add multiple entries at one go. However, you'll be able to conjointly use insertOne() function to add single entries if you wish.
Find the data in collection
We can use find() function to go looking for the records in MongoDB.
Here could be a code to indicate all the records present in the collection.
const mongo = require('mongodb');
const url = "mongodb://localhost:27017";
mongo.connect(url, {useNewUrlParser: true}, (err, db) => {
if(err) {
console.log(err);
process.exit(0);
}
var dbo = db.db('codeforgeek');
console.log('database connected!');
var collection = dbo.collection('users');
collection.find().toArray((err, results) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(results);
db.close();
});
});
You can also add search parameters to help you find specific documents. simply change the query to this:
collection.find({name: 'shahid'}).toArray((err, results) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(results);
db.close();
});
Update the document in the collection
You can update the prevailing document using updateOne() operate. the first parameter is for looking out the document and the second is to a change it with new values. Here is the snipping.
collection.updateOne({name: 'Shahid'}, {'$set': {'name': 'Shahid Shaikh'}}, (err, results) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(results);
db.close();
});
Delete the document in the collection
You can delete the prevailing documents in the collection using deleteOne() function.
collection.deleteOne({name: 'Shahid'}, (err, results) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(results);
db.close();
});
No comments:
Post a Comment