- 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
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
176 Views
To return only a specific number of elements, use aggregate() and $slice. Let us create a collection with documents −> db.demo75.insertOne({"Name":["Sam", "Mike", "Carol", "David", "Bob", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bcd7671bf0181ecc42278") }Display all documents from a collection with the help of find() method −> db.demo75.find();This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), "Name" : [ "Sam", "Mike", "Carol", "David", "Bob", "John" ] }Following is the slice query in MongoDB −> db.demo75.aggregate([ { $project: { Name: { $slice: [ "$Name", 4 ] } } } ]);This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
449 Views
Let us create a collection with documents −> db.demo74.insertOne( ... { ... StudentName: "Chris", ... StudentDetails: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }, { ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }, { ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b671bf0181ecc4226f") } > db.demo74.insertOne({ ... name: "document2", ... data: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }, { ... "Subject": "MongoDB", ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
851 Views
To get the first and last document in MongoDB, use aggregate() along with $first and $last respectively. Let us create a collection with documents −> db.demo73.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c41b71bf0181ecc4226c") } . > db.demo73.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c41e71bf0181ecc4226d") } > db.demo73.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c42271bf0181ecc4226e") }Display all documents from a collection with the help of find() method −> db.demo73.find();This will produce the following output −{ "_id" : ObjectId("5e29c41b71bf0181ecc4226c"), "Name" : "Chris" } { "_id" : ObjectId("5e29c41e71bf0181ecc4226d"), "Name" : "Bob" } { "_id" : ObjectId("5e29c42271bf0181ecc4226e"), "Name" ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
539 Views
For this, use $let along with $expr. Here, $let is used to define temporary variable. The $expr is used for aggregation expressions. Let us create a collection with documents −> db.demo72.insertOne( ... { ... StudentDetails:[ ... { ... Name: "Chris", ... Age: 21 ... }, ... { ... Name: "David", ... Age: 23 ... }, ... { ... Name: "Bob", ... ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
848 Views
To calculate sum of string in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo71.insertOne({"Price":"20"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af210912fae76b13d76e") } > db.demo71.insertOne({"Price":"50"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af240912fae76b13d76f") } > db.demo71.insertOne({"Price":"20"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af270912fae76b13d770") } > db.demo71.insertOne({"Price":"10"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af2d0912fae76b13d771") }Display all documents from a collection with the help of find() method −> db.demo71.find();This will produce the following output −{ "_id" : ObjectId("5e29af210912fae76b13d76e"), "Price" : "20" } { "_id" : ObjectId("5e29af240912fae76b13d76f"), "Price" : "50" } { "_id" ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
67 Views
Concatenate deep sub-lists using aggregate() along with $unwind. Let us create a collection with documents −> db.demo70.insertOne( ... { ... ... "first" : [ ... { ... "details" : { ... "second" : [ ... { ... "StudentDetails" : { ... "Score" : 10 ... ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
209 Views
For this, use $where and compare with ==. Let us create a collection with documents −> db.demo69.insertOne( ... { ... "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), ... "Details" : { ... ... "id" : ObjectId("507c7f79bcf86cd7994f6c0e") ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0e") } > db.demo69.insertOne( ... { ... "_id" : ObjectId("507c7f79bcf86cd7994f6c0f"), "Details" : { ... "id" :ObjectId("507c7f79bcf86cd7994f6c0a") ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0f") }Display all documents from a collection with the help of find() method −> db.demo69.find();This will produce ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
179 Views
To sort, use $match along with aggregate. Let us create a collection with documents −> db.demo67.insertOne({"StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e289edf602d9a2ff1828ed8") } > db.demo67.insertOne({"StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e289ee1602d9a2ff1828ed9") } > db.demo67.insertOne({"StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e289ee3602d9a2ff1828eda") }Display all documents from a collection with the help of find() method −> db.demo67.find();This will produce the following output −{ "_id" : ObjectId("5e289edf602d9a2ff1828ed8"), "StudentAge" : 23 } { "_id" : ObjectId("5e289ee1602d9a2ff1828ed9"), "StudentAge" : 21 } { "_id" : ObjectId("5e289ee3602d9a2ff1828eda"), "StudentAge" : 24 }Following is the query to sort with aggregate in ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
127 Views
To get index of an array element, use $indexOfArray. Let us create a collection with documents −> db.demo65.insertOne({"ListOfValues":[10, 20, 30]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28990ecfb11e5c34d89938") } > db.demo65.insertOne({"ListOfValues":[50, 60, 70, 100]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28991ecfb11e5c34d89939") } > db.demo65.insertOne({"ListOfValues":[30, 40, 89, 91, 98]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28992bcfb11e5c34d8993a") }Display all documents from a collection with the help of find() method −> db.demo65.find();This will produce the following output −{ "_id" : ObjectId("5e28990ecfb11e5c34d89938"), "ListOfValues" : [ 10, 20, 30 ] } { "_id" : ObjectId("5e28991ecfb11e5c34d89939"), "ListOfValues" : [ 50, 60, ... Read More
![Naveen Singh](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ Views
To calculate average of a particular field, use aggregate() along with $avg. Let us create a collection with documents −> db.demo214.insertOne({"Marks":56, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e319403d395bdc2134705") } > db.demo214.insertOne({"Marks":86, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e319c03d395bdc2134706") } > db.demo214.insertOne({"Marks":78, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e31a403d395bdc2134707") }Display all documents from a collection with the help of find() method −> db.demo214.find();This will produce the following output −{ "_id" : ObjectId("5e3e319403d395bdc2134705"), "Marks" : 56, "Name" : "David" } { "_id" : ObjectId("5e3e319c03d395bdc2134706"), "Marks" : 86, "Name" : "Bob" } { "_id" ... Read More