
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
577 Views
Let us create a collection with documents −> db.demo601.insertOne( ... { ... id:1, ... userDetails: ... { ... userName:"John", ... userMailId:"John@gmail.com" ... } ... Read More

AmitDiwan
497 Views
For this, use userAdminAnyDatabase since it gives all permission like admin. Following is the syntax −use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ] } )Let us implement the above syntax ... Read More

AmitDiwan
485 Views
To update, use update() and following is the syntax to create and use a sample custom variable −var anyVariableName=yourValue; db.yourCollectionName.update({filter}, {$set:{yourFieldName:yourVariableName}});Let us create a collection with documents −> db.demo600.insertOne({id:1, Name:"Robert"});{ "acknowledged" : true, "insertedId" : ObjectId("5e94a063f5f1e70e134e2699") } > db.demo600.insertOne({id:2, Name:"Mike"});{ "acknowledged" : true, "insertedId" : ObjectId("5e94a06bf5f1e70e134e269a") } > ... Read More

AmitDiwan
365 Views
To subtract values from document field values, use $subtract in MongoDB aggregate(). Let us create a collection with documents −> db.demo599.insertOne({"TotalPrice":250, "DiscountPrice":35});{ "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db.demo599.insertOne({"TotalPrice":400, "DiscountPrice":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697") } > db.demo599.insertOne({"TotalPrice":1550, "DiscountPrice":50});{ "acknowledged" : true, "insertedId" : ... Read More

AmitDiwan
436 Views
For this, use $avg along with $group and aggregate(). Let us create a collection with documents −> db.demo598.insertOne( ... { ... Information:'Student', ... id:100, ... details:[ ... {Name:'Chris', Marks:75}, ... {Name:'Bob', Marks:55} ... Read More

AmitDiwan
937 Views
To find which document contains a specific string, use $regex along with find(). Let us create a collection with documents −> db.demo597.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947ae3f5f1e70e134e2690") } > db.demo597.insertOne({"Name":"John Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e947ae8f5f1e70e134e2691") } > db.demo597.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" ... Read More

AmitDiwan
398 Views
Use findOne() in MongoDB for this. The findOne() returns one document that satisfies the specified query criteria on the collection.Let us create a collection with documents −> db.demo596.insertOne({_id:1, "FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo596.insertOne({_id:2, "FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : 2 } ... Read More

AmitDiwan
2K+ Views
Let us create a collection with documents −> db.demo595.insertOne( { "Information": [ { "_id": new ObjectId(), Name:"Chris" }, { _id:new ObjectId(), Name:"Robert" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5e93369cfd2d90c177b5bce4") }Display all documents from a collection with the help of find() method −> ... Read More

AmitDiwan
179 Views
For this, use $slice. Let us create a collection with documents −> db.demo594.insertOne( ... { ... id:1, ... details:[ ... {Name:"Chris", Age:21}, ... {Name:"Bob", Age:20}, ... {Name:"David", Age:23}, ... ... Read More

AmitDiwan
485 Views
To fetch specific multiple documents in MongoDB, use $in. Let us create a collection with documents −> db.demo593.insertOne({id:1, "Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e93177dfd2d90c177b5bcd9") } > db.demo593.insertOne({id:2, "Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e931785fd2d90c177b5bcda") } > db.demo593.insertOne({id:3, "Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e93178cfd2d90c177b5bcdb") } > ... Read More