- 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
53 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" : [ ... { ... "userId" : "1001", ... "userName":"Chris" ... }, ... { ... "userId" : "1002", ... "userName":"David" ... } ... ], ... "otherDetails" : [ ... { ... CountryName:"US", ... EmailId:["Chris@gmail.com","David@gmail.com"] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea4276904263e90dac943fc") }Display all documents from a collection with the help of find() method −> db.demo678.find();This will produce the following output −{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [ { "userId" : "1001", "userName" : "Chris" }, { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [ { "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] } ] }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
225 Views
Let us create a collection with documents −> db.demo677.insertOne({Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f404263e90dac943f8") } > db.demo677.insertOne({Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f704263e90dac943f9") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fa04263e90dac943fa") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fe04263e90dac943fb") }Display all documents from a collection with the help of find() method −> db.demo677.find();This will produce the following output −{ "_id" : ObjectId("5ea421f404263e90dac943f8"), "Value" : 10 } { "_id" : ObjectId("5ea421f704263e90dac943f9"), "Value" : 50 } { "_id" : ObjectId("5ea421fa04263e90dac943fa"), "Value" : 20 } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
157 Views
For group query, use MongoDB $group and get the count with $sum. Let us create a collection with documents −> db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41eed04263e90dac943f2") } > db.demo676.insertOne({"Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef304263e90dac943f3") } > db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef404263e90dac943f4") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef704263e90dac943f5") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef804263e90dac943f6") }Display all documents from a collection with the help of find() method −> db.demo676.find();This will produce the following output −{ "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
100 Views
Let us create a collection with documents −> db.demo675.insertOne({ ... "ListOfNames":["John", "Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5b404263e90dac943ef") } > db.demo675.insertOne({ "ListOfNames":["Bob", "Johnson", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5c804263e90dac943f0") } > db.demo675.insertOne({ "ListOfNames":["Jace", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5d304263e90dac943f1") }Display all documents from a collection with the help of find() method −> db.demo675.find();This will produce the following output −{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
98 Views
The $or operator performs a logical OR operation on an array of two or more expressions. Let us create a collection with documents −> db.demo674.insertOne({Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }Display all documents from a collection with the help of find() method −> db.demo674.find();This will produce the following output −{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
58 Views
Use max field in order to limit the number of documents in the collection. Following is the query to use the max field in the capped collection −> db.createCollection("demo673", { capped : true, size : 100, max :50 } ) { "ok" : 1 }Let us create a collection with documents −> db.demo673.insertOne({Name:"John", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7304263e90dac943e8") } > db.demo673.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7804263e90dac943e9") } > db.demo673.insertOne({Name:"David", Age:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7f04263e90dac943ea") }Display all documents from a collection with the help of find() ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
191 Views
To fetch a specific document, use dot notation in MongoDB find(). Let us create a collection with documents −> db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Oppo"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Samsung"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"OnePlus"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }Display all documents from a collection with the help of find() method −> db.demo672.find();This will produce the following output −{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] } { "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
578 Views
To get fields from multiple sub-documents, use MongoDB aggregate with $unwind. Let us create a collection with documents −> db.demo671.insertOne( ... { ... ... "details" : [ ... { ... "id" : "1" ... }, ... { ... CountryName:"US", ... "details1" : [ ... { ... "id" : "1" ... }, ... { ... "id" : "2" ... } ... ] ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
725 Views
To access subdocuments in MongoDB, use find() with dot notation. Let us create a collection with documents −> db.demo670.insertOne({ ... id:101, ... "details": ... { ... Name:"Chris", ... Age:21, ... CountryName:"US", ... SubjectName:"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e31d04263e90dac943de") } > db.demo670.insertOne({ id:102, "details": { Name:"David", Age:22, CountryName:"UK", SubjectName:"MySQL" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e33604263e90dac943df") }Display all documents from a collection with the help of find() method −> db.demo670.find();This will produce the following output −{ "_id" : ObjectId("5ea3e31d04263e90dac943de"), "id" : 101, "details" : { "Name" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
91 Views
To get the maximum element from the collection, sort in descending order with limit. Let us create a collection with documents −> > db.demo669.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133c04263e90dac943d9") } > db.demo669.insertOne({"Marks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133f04263e90dac943da") } > db.demo669.insertOne({"Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134204263e90dac943db") } > db.demo669.insertOne({"Marks":88}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134504263e90dac943dc") } > db.demo669.insertOne({"Marks":79}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134e04263e90dac943dd") }Display all documents from a collection with the help of find() method −> db.demo669.find();This will produce the following output ... Read More