- 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 6702 Articles for Database
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
134 Views
Let us first create a collection with documents −> db.demo194.insertOne( ... { ... "_id": 101, ... "details": { ... "otherDetails": { ... "List1": ["MongoDB", "MySQL"], ... "List2": ["Java"], ... "List3": ["MongoDB", "C"] ... } ... } ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo194.insertOne( {"_id": 102, "details": { "otherDetails": { "List1": ["Java", "C"], "List2": ["C++"], "List3": ["Python", "Spring"] } } } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
166 Views
To improve the execution time of a query, use index along with unique:true. Let us create a collection with documents −> db.demo193.createIndex({"LastName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo193.insertOne({"FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade1803d395bdc21346d1") } > db.demo193.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade1f03d395bdc21346d2") } > db.demo193.insertOne({"FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade2803d395bdc21346d3") }Display all documents from a collection with the help of find() method −> db.demo193.find();This will produce the following output −{ "_id" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
129 Views
To group several fields, use $group in MongoDB. Let us create a collection with documents −> db.demo192.insertOne({"Name":"Chris", "Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adb9f03d395bdc21346cd") } > db.demo192.insertOne({"Name":"David", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adba103d395bdc21346ce") } > db.demo192.insertOne({"Name":"Chris", "Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adba503d395bdc21346cf") } > db.demo192.insertOne({"Name":"Mike", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adbbf03d395bdc21346d0") }Display all documents from a collection with the help of find() method −> db.demo192.find();This will produce the following output −{ "_id" : ObjectId("5e3adb9f03d395bdc21346cd"), "Name" : "Chris", "Age" : 22 } { "_id" : ObjectId("5e3adba103d395bdc21346ce"), ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
555 Views
To get values of cursor in MongoDB, use hasNext(). Let us create a collection with documents −> db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95303d395bdc21346c5") } > db.demo191.insertOne({"EmployeeId":2, "EmployeeName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95f03d395bdc21346c6") } > db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad96803d395bdc21346c7") } > db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad97003d395bdc21346c8") }Display all documents from a collection with the help of find() method −> db.demo191.find();This will produce the following output −{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
476 Views
To implement aggregation with multiple keys, use aggregate() along with $group. Let us create a collection with documents −> db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-01-01"), ... "Value" : 10, ... "Name" : "Chris" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346bf") } > > db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-02-05"), ... "Value" : 30, ... "Name" : "David" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346c0") } > db.demo190.insertOne( ... { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
264 Views
To sort by the sum of specified object inside inner array, use $match along with $sort. Let us create a collection with documents −> db.demo189.insertOne( ... { ... "_id" : 100, ... "List" : [ ... { ... "Value" : 10 ... }, .. .{ ... "Value" : 20 ... }, ... { ... "Value" : 10 ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
364 Views
To find same value multiple times, use $where in MongoDB. Let us create a collection with documents −> db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 100} ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad1c203d395bdc21346bd") } > db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 300} ... } ...); { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
126 Views
Let us first create a collection with documents −> db.demo187.insertOne( ... { ... "_id" : "101", ... "Details" : [ ... { "Subject" : "MongoDB" }, ... { "Subject" : "MySQL" } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" } > db.demo187.insertOne( ... { ... "_id" : "102", ... "Details" : [ ... { } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "102" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
208 Views
Let us first create a collection with documents −> db.demo186.insertOne({"UserEmailId":"JOHN@GMAIL.COM", "UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d769e4f06af55199808") } > db.demo186.insertOne({"UserEmailId":"chris@gmail.com", "UserName":"chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d879e4f06af55199809") } > db.demo186.insertOne({"UserEmailId":"DAVID@GMAIL.COM", "UserName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d979e4f06af5519980a") }Display all documents from a collection with the help of find() method −> db.demo186.find();This will produce the following output −{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "JOHN@GMAIL.COM", "UserName" : "John" } { "_id" : ObjectId("5e399d879e4f06af55199809"), "UserEmailId" : "chris@gmail.com", "UserName" : "chris" } { "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "DAVID@GMAIL.COM", "UserName" : "David" }Following is the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
65 Views
To fetch records with $or on the basis of existence, use $or along with $exists. Let us create a collection with documents −>db.demo185.insertOne({_id:101, details:{Name:"Chris", Score:78, Subjects:{"Name":"MySQL"}}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo185.insertOne({_id:102, details:{Name:"Bob", Score:78}}); { "acknowledged" : true, "insertedId" : 102 } >db.demo185.insertOne({_id:103, details:{Name:"David", Score:78, Subjects:{"Name":"MongoDB"}}}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo185.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Score" : 78, "Subjects" : { "Name" : "MySQL" } } } { "_id" ... Read More