
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
450 Views
To avoid performance issues in MongoDB, use the concept of index. Let us create a collection with documents −> db.demo531.createIndex({"CountryName":"text", "Name":"text"});{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo531.insertOne({CountryName:"US", "Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2b60ef4dcbee04fbbbf2") } ... Read More

AmitDiwan
719 Views
If there are null values also, then implement ORDERBY using sort().Note − Since, starting in MongoDB v3.2, the $orderby operator deprecated in the mongo shell. Use cursor.sort() instead.Let us create a collection with documents −> db.demo530.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2990ef4dcbee04fbbbec") } > db.demo530.insertOne({"Name":null});{ "acknowledged" : ... Read More

AmitDiwan
918 Views
To group by _id in MongoDB, use $group. Let us create a collection with documents −> db.demo529.insertOne({"Score":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d5bef4dcbee04fbbbe4") } > db.demo529.insertOne({"Score":20});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d5fef4dcbee04fbbbe5") } > db.demo529.insertOne({"Score":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d61ef4dcbee04fbbbe6") } > db.demo529.insertOne({"Score":10});{ ... Read More

AmitDiwan
443 Views
To match collection items by id, use $in in MongoDB. Let us create a collection with documents −> db.demo528.insertOne({"Name":"Chris", Age:21});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b00d2ef4dcbee04fbbbe0") } > db.demo528.insertOne({"Name":"Bob", Age:22});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b00d9ef4dcbee04fbbbe1") } > db.demo528.insertOne({"Name":"David", Age:20});{ "acknowledged" : true, "insertedId" ... Read More

AmitDiwan
109 Views
For this, use $group and within that, we need to work with $sum to add. Let us create a collection with documents −> db.demo527.insertOne({"Price":45.5});{ "acknowledged" : true, "insertedId" : ObjectId("5e8aff2fef4dcbee04fbbbdc") } > db.demo527.insertOne({"Price":55.5});{ "acknowledged" : true, "insertedId" : ObjectId("5e8aff34ef4dcbee04fbbbdd") } > db.demo527.insertOne({"Price":100.4});{ "acknowledged" : true, ... Read More

AmitDiwan
452 Views
To update an array with $push, use updateOne() in MongoDB. Let us create a collection with documents −> db.demo526.insertOne( ... { ... ... "CountryName": "US", ... "TeacherName": "Bob", ... "StudentInformation": [ ... { ... "Name": "Chris", ... ... Read More

AmitDiwan
1K+ Views
For this, use $and along with $regex. The $and performs a logical AND operation on an array of one or more expressions and selects the documents that satisfy all the expressions in the array.Let us create a collection with documents −> db.demo525.insertOne({"details":[{Name:"Chris", "CountryName":"US"}]});{ "acknowledged" : true, "insertedId" : ... Read More

AmitDiwan
2K+ Views
To search date between two dates in MongoDB, use $gte and $lt. Let us create a collection with documents −> db.demo524.insertOne({"EndDate":new ISODate("2020-01-19")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8adbe5437efc8605595b63") } > db.demo524.insertOne({"EndDate":new ISODate("2020-01-20")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8adbec437efc8605595b64") } > db.demo524.insertOne({"EndDate":new ISODate("2020-12-31")});{ "acknowledged" : true, ... Read More

AmitDiwan
137 Views
For float conversion, use parseFloat() in MongoDB. Let us create a collection with documents −> db.demo523.insertOne({"details":{values:"-0.45"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b7efb3fbf26334ef611f") }Display all documents from a collection with the help of find() method −> db.demo523.find();This will produce the following output −{ "_id" : ObjectId("5e89b7efb3fbf26334ef611f"), "details" : { ... Read More

AmitDiwan
360 Views
To get a distinct pair of objects, use $group. Let us create a collection with documents −> db.demo522.insertOne({"Name":"John", "Score":45});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b646b3fbf26334ef611b") } > db.demo522.insertOne({"Name":"Bob", "Score":67});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b64eb3fbf26334ef611c") } > db.demo522.insertOne({"Name":"John", "Score":55});{ "acknowledged" : true, "insertedId" : ... Read More