
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
851 Views
You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo701.insertOne({"ListOfValues":[100, 200, 300]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500, 700, 1000]}); ... Read More

AmitDiwan
141 Views
For this, use $regex in MongoDB. We will search for document field value with name “David”, irrespective of case. Let us create a collection with documents −> db.demo700.insertOne( { details: [ { Name:"david" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e6b1551299a9f98c93ac") } > db.demo700.insertOne( { details: [ { ... Read More

AmitDiwan
204 Views
For multiple atomic updates, use update() along with $set. Let us create a collection with documents −> db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") } > db.demo699.insertOne({Name:"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") } > db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, ... Read More

AmitDiwan
462 Views
For this, use forEach() along with print() to display the email-id values. Let us create a collection with documents −> db.demo690.insertOne({"UserName":"John", "UserEmailId":"John@gmail.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6db31551299a9f98c939c") } > db.demo690.insertOne({"UserName":"Bob", "UserEmailId":"Bob@gmail.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6db3c551299a9f98c939d") } > db.demo690.insertOne({"UserName":"David", "UserEmailId":"David@gmail.com"}); { ... Read More

AmitDiwan
150 Views
To update only a single value and increment it in MongoDB, use $inc along with update(). Let us create a collection with documents −> db.demo698.insertOne({Score:78}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d8a4551299a9f98c9398") } > db.demo698.insertOne({Score:56}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d8a7551299a9f98c9399") } > db.demo698.insertOne({Score:65}); { ... Read More

AmitDiwan
338 Views
To get number of records, use count() in MongoDB. Let us create a collection with documents −> db.demo697.insertOne({Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d7d1551299a9f98c9395") } > db.demo697.insertOne({Name:"Bob", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d7d8551299a9f98c9396") } > db.demo697.insertOne({Name:"David", Age:24}); { "acknowledged" : true, ... Read More

AmitDiwan
424 Views
To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −> db.demo679.insertOne( ... { ... id:1, ... "details": [ ... { ... CountryName:"US", ... ... Read More

AmitDiwan
342 Views
For this, use find() along with //i. Let us create a collection with documents −> db.demo696.insertOne({Message:"/Good/"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d664551299a9f98c9391") } > db.demo696.insertOne({Message:"(good)"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d67a551299a9f98c9392") } > db.demo696.insertOne({Message:"/Bye/"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d68b551299a9f98c9393") } ... Read More

AmitDiwan
102 Views
For this, use ensureIndex(). Let us create a collection with documents −> db.demo678.ensureIndex({id:1, "details.userId":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo678.insertOne( ... { ... id:101, ... ... "details" : [ ... Read More

AmitDiwan
193 Views
To match all in MongoDB, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo695.insertOne({"ListOfValues":[100, 200, 500, 800]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d4c4551299a9f98c938f") } ... Read More