Introduction
Mongo DB is a cross-platform, open-source document-oriented database. Mongod is a NoSQL database (no structure). Mongo DB stores JSON data in the form of collections. MongoDB has no structure of the data so it’s called NoSQL data. Mongo DB has no limit to store collection and documents to the database like SQL. Mongo DB also has no required declared length of the field like SQL / Oracle / SqlYog.
If you create a database and define4 fields (id, name, salary, mobile) in your collection document, if you want to add more the 6 fields in the collection document, then you don’t need to define all fields in the collection. You directly add all fields to your collection. MongoDB database automatic adds extra fields with all values which you define in the query. This query is applied for all data in the collection. It will take null value for added extra fields.
Functions of Mongo DB
The functions of MongoDB are create, insert ,delete,update, read operation,
.InsertOne()
Insert one functions are use to insert a record in database at a time. If you want to insert a record in a database then you need to use insertOne().
- var customer = {_id:111, name:"Santosh Kumar" , address: "B-222, Sector-19, NOIDA", orderdata:"Arrow Shirt"};
- mersCollection.insertOne(customer, function(error, response) { }
.insertMany()
InsertMany() functions are use to store a number of records in a collection.
- var customers = [
- {name:"Rakesh Kumar" , address: "B-222, Sector-12, NOIDA", orderdata:"TV"},
- {name:"Amit Kumar" , address: "C-222, Sector-22, NOIDA", orderdata:"Sofa set"},
- {name:"Kamesh Kumar" , address: "D-222, Sector-32, NOIDA", orderdata:"Dinig table"},
- {name:"Deleep Kumar" , address: "E-222, Sector-42, NOIDA", orderdata:"jeans"}
- ];
- nodeDB.collection("customers").insertMany(customers, function(error, response) }
deleteOne()
DeleteOne() functions are use to delete one record from the collection.
- var deleteQuery = { username:"croma campus"};
- nodeDB.collection("customers").deleteOne(deleteQuery, function(err, response) {
- if (err) throw err;
- console.log(response.result.n + " document deleted");
- });
DeleteMany()
DeleteMany() are use to delete one multiple record from collection.
- var deleteQuery = {};
- nodeDB.collection("customers").deleteMany(deleteQuery, function(err, obj) { }
findOne()
findOne() are use to find one record from collection.
- nodeDB.collection("users").findOne({price:4.5}, function(err, result) {
- if (err) throw err;
- console.log(result.name + ", " + result.address + ", " + result.orderdata);
- });
findMany()
findMany() are use to find many record from collection
- nodeDB.collection("customers").find({}).toArray(function(err, totalCustomers) {
- if (err) throw err;
-
- for(i = 0; i < totalCustomers.length; i++) {
- let customer = totalCustomers[i];
- console.log(customer.name + ", " + customer.address + ", " + customer.orderdata);
- }
updateOne()
updateOne() are use to update one record from collection.
- var whereClause = { address: "B-222, Sector-12, NOIDA" };
- var newvalues = {$set: { address: "B-44444444, Sector-12, NOIDA" }};
- nodeDB.collection("customers").updateOne(whereClause, newvalues, function(err, res) {
- if (err) throw err;
- console.log(res.result.n + " document updated");
- });
UpdateMany()
update many function are use to update many record from collection.
- var newvalues = {$set: {name: "Mukesh"} };
- nodeDB.collection("customers").updateMany(myquery, newvalues, function(err, res) {
- if (err) throw err;
- console.log(res.result.nModified + " document(s) updated");
- databases.close();
- });
- });
Different between SQL and MongoDB
SQL Database |
Mongo DB Database |
Sql store record in the table |
Mongo db store data in collection |
Data manage in row and column and each row represent a option in oops |
Data manage in the form of JSon |
Predefine nature |
Nature is not define (no nature) |
Record created |
Document created |
Insert into table |
Insert into Json |
Select*from table use to display table |
Db.collection.find use to find document |
Advantages of MongoDB
- MongoDB is a document database in which one collection has many documents, field sizes, and content.
- No complex of join
- Mongodb is easy to scale
- No need for mapping/conversion
- Data stored in the form of JSON style
- Store big data
- Fast in-place update
How to download MongoDB compass
Open your browser and search on the browser Download mongodb compass. You can download it directly from the
link.
Afterward, download your Mongo DB open .exe file and install. After completion, go to the web services and start mongodb service.
then go to "C:MongoDB\bin\mongod.exe" ---- config "C:\MongoDB\mongod.cfg" --- install then go to the cmd and run command on cmd,
After a few minutes, all mongodb packages should be completely installed. Now MongoDB is installed in your system.