
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
356 Views
To update array with another field, use $push. Let us create a collection with documents −> db.demo283.insertOne({"Name":"Chris", "Status":["Active", "Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ab97ddd099650a5401a75") }Display all documents from a collection with the help of find() method −> db.demo283.find();This will produce the following output −{ "_id" : ... Read More

AmitDiwan
182 Views
To retrieve document from collection by _id, use find() with $in. Let us create a collection with documents −> db.demo281.insertOne({"Name":"Chris", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4aac28dd099650a5401a66") } > db.demo281.insertOne({"Name":"Bob", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4aac46dd099650a5401a67") } > db.demo281.insertOne({"Name":"David", "Age":28}); { "acknowledged" ... Read More

AmitDiwan
279 Views
To delete element from an array, use $pull. Let us create a collection with documents −> db.demo279.insertOne({id:[101, 103, 105, 110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490af7dd099650a5401a58") } > db.demo279.insertOne({id:[107, 111, 110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490b06dd099650a5401a59") }Display all documents from a collection with ... Read More

AmitDiwan
97 Views
Create an index and then use explain(). Let us create a collection with documents −> db.demo278.ensureIndex({"Subjects":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo278.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e49096edd099650a5401a55") } > db.demo278.insertOne({"Subjects":["C", ... Read More

AmitDiwan
108 Views
Let us first create a collection with documents −> db.demo277.insertOne({"details":[{"FirstName":"John"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb21dd099650a5401a52") } > db.demo277.insertOne({"details":[{"FirstName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb27dd099650a5401a53") } > db.demo277.insertOne({"details":[{"FirstName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb2bdd099650a5401a54") }Display all documents from a collection with ... Read More

AmitDiwan
614 Views
To split a document by its subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo276.insertOne({"Name":"Chris", "Subjects":["MySQL", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48f953dd099650a5401a51") }Display all documents from a collection with the help of find() method −> db.demo276.find().pretty();This will produce the following output ... Read More

AmitDiwan
2K+ Views
To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().Let us create a collection with documents −> db.demo275.insertOne({"Number":10}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eac4dd099650a5401a43") } > db.demo275.insertOne({"Number":12}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eac7dd099650a5401a44") } > ... Read More

AmitDiwan
113 Views
Yes, you can achieve this by indexing like “properties.k” for key and “properties.v” for value. The same is used to be implemented in ensureIndex().Let us first see an example and create a collection with documents −> db.demo274.insertOne({"details":[{StudentFirstName:"Chris", StudentLastName:"Brown"}, ... {StudentFirstName:"David", StudentLastName:"Miller"}, ... {StudentFirstName:"John", StudentLastName:"Smith"}, ... {StudentFirstName:"John", StudentLastName:"Doe"} ... Read More

AmitDiwan
2K+ Views
To insert LONG numbers, use NumberLong(). It is used to handle 64-bit integers. Let us create a collection with documents −> db.demo273.insert({ ... Name:"Robert", ... id: NumberLong("100000000000001"), ... isActive:true ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ... Name:"David", ... id: NumberLong("98888888888888888"), ... isActive:false ... Read More

AmitDiwan
129 Views
For this, you can use aggregate() in MongoDB. Let us create a collection with documents −> db.demo149.insertOne({"Status":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e350386fdf09dd6d08539c4") } > db.demo149.insertOne({"Status":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e350388fdf09dd6d08539c5") } > db.demo149.insertOne({"Status":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e35038afdf09dd6d08539c6") ... Read More