
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
189 Views
Use insertMany() to insert multiple documents into a collection. With that, to update performance you can use ensureIndex().Let us create a collection with documents and insert multiple documents −> db.demo325.insertMany( [ ... { _id: 101, Name: "Chris", Age: 23 }, ... { _id: 102, Name: "David", Age: 24 ... Read More

AmitDiwan
110 Views
Let us create a collection with documents −> db.demo324.insertOne({"ListOfValues":[10, 20, 30]}); { "acknowledged" : true, "insertedId" : ObjectId("5e516349f8647eb59e562073") }Display all documents from a collection with the help of find() method −> db.demo324.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e516349f8647eb59e562073"), "ListOfValues" : [ ... Read More

AmitDiwan
130 Views
At first, let us create a collection with documents and also use ensureIndex() to create an index −> db.demo323.insertOne({"details":{"Name":"Chris", "Age":34}}); { "acknowledged" : true, "insertedId" : ObjectId("5e51157af8647eb59e56206e") } > db.demo323.insertOne({"details":{"Name":"David", "Age":31}}); { "acknowledged" : true, "insertedId" : ObjectId("5e511581f8647eb59e56206f") } > db.demo323.insertOne({"details":{"Name":"Bob", "Age":28}}); { "acknowledged" : ... Read More

AmitDiwan
251 Views
To check for existing documents/embedded documents, use $exists in MongoDB. Let us create a collection with documents −> db.demo322.insertOne( ... {'id':1001, ... 'details':[{'Score':10000, Name:"Bob"}, ... {'Score':98000, Name:"Sam"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113e2f8647eb59e56206c") } > db.demo322.insertOne( ... Read More

AmitDiwan
2K+ Views
Let us first create a collection with documents wherein we are storing array values −>db.demo321.insertOne({"UserDetails":[{"UserId":101, "UserName":"Chris"}, {"UserId":102, "UserName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511248f8647eb59e56206a") } >db.demo321.insertOne({"UserDetails":[{"UserId":103, "UserName":"Bob"}, {"UserId":104, "UserName":"Sam"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511259f8647eb59e56206b") }Display all documents from a collection with the help ... Read More

AmitDiwan
204 Views
The fastest way of updating is update(). Let us create a collection with documents and see how update() works −> db.demo320.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee51f8647eb59e562066") } > db.demo320.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee55f8647eb59e562067") } > db.demo320.insertOne({"Name":"Mike"}); { "acknowledged" : true, ... Read More

AmitDiwan
436 Views
In MongoDB, you cannot use capped for sub-collection. However, use capped on the overall document. To display a specific number of values from an array, prefer $slice.Let us create a collection with documents −> db.demo319.insertOne({"Scores":[100, 345, 980, 890]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ecf6f8647eb59e562064") } > db.demo319.insertOne({"Scores":[903, ... Read More

AmitDiwan
504 Views
To pull a specific value, use UPDATE with $pull. Let us create a collection with documents −> db.demo318.insertOne({Subject:["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea6df8647eb59e562062") } > db.demo318.insertOne({Subject:["Spring", "Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea78f8647eb59e562063") }Display all documents from a collection with the ... Read More

AmitDiwan
343 Views
To get values greater than a specific value, use $gt along with find(). Let us create a collection with documents −> db.demo317.insertOne( ... {'id':101, ... 'details':[{'Score':78, Name:"Chris"}, ... {'Score':88, Name:"David"} ... ] ... } ... ); { ... Read More

AmitDiwan
192 Views
For this, use ensureIndex() and set unique:true. Let us create a collection with documents. Here, when we try to inert duplicate item, then a duplicate key error occurs −> db.demo316.ensureIndex({"SubjectName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > ... Read More