How to use MongoDB with Node.js

Please Click for MongoDB Installation
Module installation
The mongodb module is used for using MongoDB with Node.js.
The module is downloaded using the following commands.
1 2 3 |
npm install mongodb veya npm install mongodb --save |
The module is included in the project.
1 |
const mongo = require('mongo'); |
The module has various objects, methods and properties for MongoDB operations.
MongoDB Connection
For MongoDB connection, MongoClient object in the module connect method is used.
1 2 3 4 5 6 7 8 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; console.log('MongoDB connection was successfully'); client.close(); }); |
The module allows various connection options for MongoDB connection.
1 |
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] |
Database Connection
For the MongoDB database connection, the feedback function of the connect method is used.
1 2 3 4 5 6 7 8 9 10 11 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('DB_NAME'); console.log('MongoDB db connection was successfully'); client.close(); }); |
Returns the object with the methods to be used in MongoDB operations as the connection return value.
Creating a collection
The createCollection method is used to create a collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); db.createCollection('users', (err, result) => { if (err) throw err; console.log('Create collection.'); client.close(); }); }); |
NOTE: To create a collection, just add data.
Adding data
The insertOne or insertMany method is used to add data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let data = { name: 'taha', surname: 'sivaci' }; db.collection('users').insertOne(data, (err, result) => { if (err) throw err; console.log('user was successfully added); client.close(); }); }); |
The insertMany method is used to add multiple data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('kisi'); let data = [ { name: 'taha', surname: 'sivaci' }, { name: 'yasin', surname: 'sample' }, { name: 'gulsen', surname: 'yilmaz' }, { name: 'can', surname: 'sezer' } ]; db.collection('users').insertMany(data, (err, result) => { if (err) throw err; console.log(result.insertedCount + ' data added.'); client.close(); }); }); |
NOTE: The feedback function is used to get information about the process result.
Query
The find or findOne method is used to query and retrieve data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; db.collection('users').findOne(query, (err, result) => { if (err) throw err; console.log(result); client.close(); }); }); |
The received data is accessed by the dot (.) Operator.
1 |
console.log(result._id); |
The find method is used to pull multiple data.
The method takes the query and query settings as objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; db.collection('users').find(query, { projection: { _id: 0 } }).toArray((err, result) => { if (err) throw err; console.log(result); client.close(); }); }); |
The sort method is used to sort the received data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; db.collection('users').find(query).sort({ name: -1 }).toArray((err, result) => { if (err) throw err; console.log(result); client.close(); }); }); |
Limit and skip method is used to retrieve certain data as a result of the query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; db.collection('users').find(query).skip(2).limit(2).toArray((err, result) => { if (err) throw err; console.log(result); client.close(); }); }); |
Data update
The updateOne or updateMany method is used to update the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = { name: 'taha' }; let newValue = { $set: { name: 'taha', surname: 'sivaci' } }; db.collection('users').updateOne(query, newValue, (err, result) => { if (err) throw err; console.log('Successfully updated.'); client.close(); }); }); |
The method will query and retrieve the new value of the data.
The keyword $set is used to update data.
When the keyword is not used, the new content of the matching data will be the new value.
The updateMany method is used to update multiple data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; let newValue = { $set: { name: 'taha', surname: 'sivaci' } }; db.collection('users').updateMany(query, newValue, (err, result) => { if (err) throw err; console.log(result.modifiedCount + ' data updated.'); client.close(); }); }); |
Deleting Data
The deleteOne or deleteMany method is used to delete data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); let query = {}; db.collection('users').deleteOne(query, (err, result) => { if (err) throw err; console.log('successfully deleting'); client.close(); }); }); |
The deleteMany method is used to delete multiple data.
Deleting a collection
The dropCollection method is used to delete a collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const MongoClient = require('mongodb').MongoClient; const URL = 'mongodb://localhost:27017'; MongoClient.connect(URL, (err, client) => { if (err) throw err; const db = client.db('user'); db.dropCollection('users', (err, result) => { if (err) throw err; if (result) console.log('collection was deleted successfully.'); client.close(); }) }); |
The drop method can also be used to delete a collection.
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments