
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
400 Views
To push an element into array, use $push. Let us first create a collection with documents −> db.demo6.insertOne({"ListOfNames":["Bob", "David", "Mike", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e0b6b3f25ddae1f53b62228") }Following is the query to display all documents from a collection with the help of find() method −> db.demo6.find().pretty();This will ... Read More

AmitDiwan
844 Views
Yes, MongoDB can easily index a null value. Let us first create a collection with documents −> db.demo170.createIndex({"Value":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo170.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo170.insert({"Value":null}); WriteResult({ "nInserted" : 1 ... Read More

AmitDiwan
2K+ Views
To fetch all the ids, simply use find() in MongoDB. Let us create a collection with documents −> db.demo169.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e36975e9e4f06af551997d7") } > db.demo169.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3697629e4f06af551997d8") } > db.demo169.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" ... Read More

AmitDiwan
133 Views
To increment a date, use setDate, getDate() and perform increment operation. Let us first create a collection with documents −> db.demo168.insertOne({"DueDate":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e3695ae9e4f06af551997d6") }Display all documents from a collection with the help of find() method −> db.demo168.find();This will produce the following output −{ ... Read More

AmitDiwan
345 Views
For this, use aggregate() and $group. To get minimum and maximum value, use $min and $max.Let us create a collection with documents −> db.demo167.insertOne({"Score":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693a79e4f06af551997d1") } > db.demo167.insertOne({"Score":80}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693ab9e4f06af551997d2") } > db.demo167.insertOne({"Score":60}); { ... Read More

AmitDiwan
390 Views
Let us create a collection with documents −> db.demo166.insertOne({"details" : { "UserName" : "Chris", "UserAge":29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } }); { "acknowledged" : true, "insertedId" : ObjectId("5e368b159e4f06af551997cf") } > db.demo166.insertOne({"details" : { "UserName" : "David", "UserAge":21, "PhoneDetails" : { "PhoneNumber" : "87664534654" } } ... Read More

AmitDiwan
136 Views
To remove only a single document in MongoDB, use remove(). Let us create a collection with documents −> db.demo165.insertOne({"ClientId":101, "ClientName":"Chris", "ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e36895c9e4f06af551997cc") } > db.demo165.insertOne({"ClientId":102, "ClientName":"Bob", "ClientAge":32}); { "acknowledged" : true, "insertedId" : ObjectId("5e3689659e4f06af551997cd") } > db.demo165.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); { ... Read More

AmitDiwan
215 Views
Use aggregate() and within that to sort, use $sort in MongoDB. Let us create a collection with documents −> db.demo164.insertOne({"StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e36883d9e4f06af551997c8") } > db.demo164.insertOne({"StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5e3688409e4f06af551997c9") } > db.demo164.insertOne({"StudentAge":22}); { "acknowledged" : true, ... Read More

AmitDiwan
377 Views
In MongoDB, $all is used to select the documents where the value of a field is an array that contains all the specified elementsLet us create a collection with documents −> db.demo163.insertOne( ... { ... "ClientDetails": [{ ... "ClientName": "Chris" ... ... ... Read More

AmitDiwan
902 Views
This can be easily achieved using MongoDB update(). Let us create a collection with documents −> db.demo162.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3684359e4f06af551997c2") } > db.demo162.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3684389e4f06af551997c3") } > db.demo162.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e36843c9e4f06af551997c4") ... Read More