- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1349 Articles for MongoDB
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
226 Views
You can use positional operator $. Let us first create a collection with documents −> db.replaceAnArrayFieldValueDemo.insertOne({"StudentTechnicalSubjects":["MySQL", "SQL Server", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cea41e0ef71edecf6a1f68f") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceAnArrayFieldValueDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cea41e0ef71edecf6a1f68f"), "StudentTechnicalSubjects" : [ "MySQL", "SQL Server", "PL/SQL" ] }Following is the query to replace an array field value. Here, we are updating “SQL Server” with “MongoDB” −> db.replaceAnArrayFieldValueDemo.update( {"StudentTechnicalSubjects":"SQL Server"}, ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
147 Views
Yes, for this, use $indexOfCP operator along with aggregate framework. Let us first create a collection with documents −> db.patterDemo.insertOne( { "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68d") } > db.patterDemo.insertOne( { "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68e") }Following is the query to display all documents from a collection with the help of find() method −> db.patterDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
209 Views
For this, use aggregate framework with the $elemMatch operator. Let us first create a collection with documents −> db.matchMultipleCriteriaDemo.insertOne({ "EmployeeDetails": [ {"EmployeeName": "Chris", "Salary": 45000, "Language":"Java"}, {"EmployeeName": "Robert", "Salary": 41000, "Language":"Python"} ] }); { "acknowledged" : true, "insertedId" : ObjectId("5cea3bf0ef71edecf6a1f689") } > db.matchMultipleCriteriaDemo.insertOne({ "EmployeeDetails": [ {"EmployeeName": "David", "Salary": 55000, "Language":"C++"}, {"EmployeeName": "Bob", "Salary": 61000, "Language":"C"} ] }); { "acknowledged" : true, "insertedId" : ObjectId("5cea3bf1ef71edecf6a1f68a") }Following is the query to display all documents from a collection with the help of find() ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
136 Views
For this, you can use $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Let us first create a collection with documents −> db.filterBySeveralElementsDemo.insertOne( "_id":100, "StudentDetails": [ { "StudentName": "John", "StudentCountryName": "US", }, { "StudentName": "Carol", "StudentCountryName": "UK" } ] } ); { "acknowledged" : true, "insertedId" : 100 } > db.filterBySeveralElementsDemo.insertOne( { ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
321 Views
Use $avg operator along with aggregate framework. Let us first create a collection with documents. Here, one of the fields is StudentScore −> db.averageReturiningNullDemo.insertOne( {"StudentDetails" : { "StudentScore" : 89 } }); { "acknowledged" : true, "insertedId" : ObjectId("5ce9822e78f00858fb12e927") } > db.averageReturiningNullDemo.insertOne( {"StudentDetails" : { "StudentScore" : 34 } }); { "acknowledged" : true, "insertedId" : ObjectId("5ce9822e78f00858fb12e928") } > db.averageReturiningNullDemo.insertOne( {"StudentDetails" : { "StudentScore" : 78 } }); { "acknowledged" : true, "insertedId" : ObjectId("5ce9822e78f00858fb12e929") }Following is the query to display all documents from a collection with the help of find() ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
710 Views
MongoDB supports the BSON format data, so there is no max length of field name. Let us first create a collection with documents −>db.maxLengthDemo.insertOne({"maxLengthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh":"This is demo"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce97ac978f00858fb12e926") }Following is the query to display all documents from a collection with the help of find() method −> db.maxLengthDemo.find();This will produce the following output.{ "_id" : ObjectId("5ce97ac978f00858fb12e926"), "maxLengthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" : "This is demo" }
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
135 Views
To update a single list item, use positional operator($). Let us first create a collection with documents −> db.updateASingleListDemo.insertOne({ _id:1, "EmployeeName":"Chris", "EmployeeDetails": [ {"EmployeeId":"EMP-101", "EmployeeSalary": 18999 }] }); { "acknowledged" : true, "insertedId" : 1 }Following is the query to display all documents from a collection with the help of find() method −> db.updateASingleListDemo.find().pretty();This will produce the following output −{ "_id" : 1, "EmployeeName" : "Chris", "EmployeeDetails" : [ { "EmployeeId" : "EMP-101", "EmployeeSalary" : 18999 } ] }Following is the query ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
87 Views
To retrieve array values, use dot(.) notation. Let us first create a collection with documents −> db.retrievingArrayDemo.insertOne( { "UserDetails" : [ { "UserName" : "John", "UserAge" : 23 } ], "UserCountryName" : "AUS", "UserLoginDate" : new ISODate(), "UserMessage" : "Hello" } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9718478f00858fb12e920") } > db.retrievingArrayDemo.insertOne( { "UserDetails" : [ { "UserName" : "Sam", "UserAge" : 24 } ], "UserCountryName" : "UK", "UserLoginDate" : new ISODate(), "UserMessage" : "Bye" } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9718478f00858fb12e921") }Following is the query to display all documents from a collection with the help of find() method −> db.retrievingArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ce9718478f00858fb12e920"), ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
691 Views
To update child objects, use $set operator. Let us first create a collection with document −>db.updateChildObjectsDemo.insertOne({"StudentName":"Chris", "StudentOtherDetails":{"StudentSubject":"MongoDB", "StudentCountryName":"AUS"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ce964e078f00858fb12e91f") }Following is the query to display all documents from a collection with the help of find() method −> db.updateChildObjectsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ce964e078f00858fb12e91f"), "StudentName" : "Chris", "StudentOtherDetails" : { "StudentSubject" : "MongoDB", "StudentCountryName" : "AUS" } }Following is the query to update child objects in MongoDB −> db.updateChildObjectsDemo.update({"StudentName" : "Chris"}, {$set:{"StudentOtherDetails.StudentCountryName":"UK"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
1K+ Views
To convert a field to an array, use $set operator. Let us first create a collection with documents −> db.convertAFieldToAnArrayDemo.insertOne({"StudentSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce92d7778f00858fb12e91d") }Following is the query to display all documents from a collection with the help of find() method −> db.convertAFieldToAnArrayDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce92d7778f00858fb12e91d"), "StudentSubject" : "MongoDB" }Following is the query to convert a field to an array using update operation with $set:−> db.convertAFieldToAnArrayDemo.find().forEach(function(myDocument) { db.convertAFieldToAnArrayDemo.update( { _id: myDocument._id }, { "$set": { "StudentSubject": [myDocument.StudentSubject] } } ); })Let ... Read More