
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
AmitDiwan has Published 10744 Articles

AmitDiwan
281 Views
For this, use $where and compare with ==. Let us create a collection with documents −> db.demo69.insertOne( ... { ... "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), ... "Details" : { ... ... "id" : ObjectId("507c7f79bcf86cd7994f6c0e") ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0e") ... Read More

AmitDiwan
253 Views
To sort, use $match along with aggregate. Let us create a collection with documents −> db.demo67.insertOne({"StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e289edf602d9a2ff1828ed8") } > db.demo67.insertOne({"StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e289ee1602d9a2ff1828ed9") } > db.demo67.insertOne({"StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e289ee3602d9a2ff1828eda") }Display ... Read More

AmitDiwan
175 Views
To get index of an array element, use $indexOfArray. Let us create a collection with documents −> db.demo65.insertOne({"ListOfValues":[10, 20, 30]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28990ecfb11e5c34d89938") } > db.demo65.insertOne({"ListOfValues":[50, 60, 70, 100]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28991ecfb11e5c34d89939") } > db.demo65.insertOne({"ListOfValues":[30, 40, 89, 91, ... Read More

AmitDiwan
887 Views
To merge, use aggregate() along with $PUSH. Let us create a collection with documents −> db.demo64.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28762bcfb11e5c34d89936") } > db.demo64.insertOne({"Subjects":["Oracle", "Spring", "Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28763fcfb11e5c34d89937") }Display all documents from a collection with the help ... Read More

AmitDiwan
413 Views
You need to use $group to group documents with specified _id expression. Let us first create a collection with documents −> db.aggreagateDemo.insertOne({"Product_Id":1, "ProductPrice":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3c025ddae1f53b621d9") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":100}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3c625ddae1f53b621da") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":500}); { ... Read More

AmitDiwan
165 Views
To check for existence of a record, use findOne() in MongoDB. Let us first create a collection with documents −> db.existsAlternateDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d23f9e4dae213890ac5c") } > db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d2559e4dae213890ac5d") } >db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":22, "StudentCountryName":"US"}); { "acknowledged" ... Read More

AmitDiwan
2K+ Views
To sum specific fields, use aggregate along with $sum. Let us first create a collection with documents −> db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":50, "Status":"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06cec29e4dae213890ac55") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":102, "Price":200, "Status":"Inactive"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ced19e4dae213890ac56") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":3000, "Status":"Active"}); { ... Read More

AmitDiwan
8K+ Views
To insert records in MongoDB and avoid duplicates, use “unique:true”. Let us first create a collection with documents.Here, we are trying to add duplicate records −> db.insertWithoutDuplicateDemo.createIndex({"StudentFirstName":1}, { unique: true } ); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } ... Read More

AmitDiwan
350 Views
To apply a condition, use $setIsSubset. Let us first create a collection with documents −> db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e063e49150ee0e76c06a052") } > db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e063e4f150ee0e76c06a053") }Following is the query to display all documents ... Read More

AmitDiwan
1K+ Views
At first, get the current month and subtract by 1 to fetch previous month records. Let us first create a collection with documents −> db.findOneMonthAgoData.insertOne({"CustomerName":"Chris", "PurchaseDate":new ISODate("2019-12-26")}); { "acknowledged" : true, "insertedId" : ObjectId("5e04e16c150ee0e76c06a04f") } > db.findOneMonthAgoData.insertOne({"CustomerName":"David", "PurchaseDate":new ISODate("2019-11-26")}); { "acknowledged" : true, "insertedId" : ObjectId("5e04e178150ee0e76c06a050") ... Read More