
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
679 Views
To find records that does not match a condition, use $ne. Let us create a collection with documents −> db.demo148.insertOne({"Message":"Hello"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); { "acknowledged" : true, ... Read More

AmitDiwan
358 Views
To compare fields from an array, use $gt and $lt. Let us create a collection with documents −> db.demo147.insertOne({"Details":[{"Score":45}, {"Score":46}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fa21fdf09dd6d08539be") } > db.demo147.insertOne({"Details":[{"Score":65}, {"Score":86}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fa40fdf09dd6d08539bf") }Display all documents from a collection with the ... Read More

AmitDiwan
194 Views
To remove an array element, simply use $pull along with update(). Let us create a collection with documents −> db.demo146.insertOne({"ListOfEmployeeNames":["Chris", "David", "Bob", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f54ffdf09dd6d08539bd") }Display all documents from a collection with the help of find() method −> db.demo146.find();This will produce the following ... Read More

AmitDiwan
220 Views
For writing an equality, you can simply use find() along with match value. Let us create a collection with documents −> db.demo145.insertOne({"ListOfNames":["Chris", "David", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f37bfdf09dd6d08539bb") } > db.demo145.insertOne({"ListOfNames":["Bob", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f384fdf09dd6d08539bc") }Display all documents from ... Read More

AmitDiwan
289 Views
Let us first create a collection with documents −>db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeEmail":"Chris12@gmail.com"}, {"EmployeeName":"Bob", "EmployeeEmail":"bo22@gmail.com"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f1d8fdf09dd6d08539b9") } >db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"David", "EmployeeEmail":"david@gmail.com"}, {"EmployeeName":"Carol", "EmployeeEmail":"Carol@gmail.com"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f1f5fdf09dd6d08539ba") }Display all documents from a collection with the help of find() method −> db.demo144.find();This will ... Read More

AmitDiwan
874 Views
For this, simply use update() to update. Let us create a collection with documents −> db.dem0143.insertOne({"StudentId":1, "Details":{"Name":"Chris"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e32eb9efdf09dd6d08539b7") } > db.dem0143.insertOne({"StudentId":2, "Details":{"Name":"David"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e32eba5fdf09dd6d08539b8") }Display all documents from a collection with the help of find() ... Read More

AmitDiwan
905 Views
For this, you can use aggregate(). Let us create a collection with documents −> db.demo142.insertOne({"Value":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e32e9c6fdf09dd6d08539b2") } > db.demo142.insertOne({"Value":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e32e9cafdf09dd6d08539b3") } > db.demo142.insertOne({"Value":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e32e9cdfdf09dd6d08539b4") } > ... Read More

AmitDiwan
6K+ Views
To find last object in collection, at first sort() to sort the values. Use limit() to get number of values i.e. if you want only the last object, then use limit(1).Let us first create a collection with documents −> db.demo141.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c347fdf09dd6d08539ae") } ... Read More

AmitDiwan
295 Views
For such kind of fetching, use only $nin and $in. Let us create a collection with documents −> db.demo140.insertOne({"Id":101, "Subjects":["MongoDB", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c149fdf09dd6d08539a9") } > db.demo140.insertOne({"Id":102, "Subjects":["MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c14cfdf09dd6d08539aa") } > db.demo140.insertOne({"Id":103, "Subjects":["MongoDB", "PL/SQL"]}); { ... Read More

AmitDiwan
984 Views
For this, use aggregate(). Let us create a collection with documents −>db.demo138.insertOne({"Id":101, "PlayerDetails":[{"PlayerName":"Chris", "PlayerScore":400}, {"PlayerName":"David", "PlayerScore":1000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bb9ffdf09dd6d08539a1") } >db.demo138.insertOne({"Id":102, "PlayerDetails":[{"PlayerName":"Bob", "PlayerScore":500}, {"PlayerName":"Carol", "PlayerScore":600}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bbcefdf09dd6d08539a2") }Display all documents from a collection with the help of ... Read More