- 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)
714 Views
To find documents that contain specific field, use $exists. Let us create a collection with documents −> db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"Chris", "ClientAge":34}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2a31627c0c63e7dba69") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"John", "ClientLastName":"Smith", "ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2be1627c0c63e7dba6a") } > db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientAge":33}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2cd1627c0c63e7dba6b") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientLastName":"Miller", "ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2de1627c0c63e7dba6c") }Display all documents from a collection with the help of find() method −> db.demo247.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e46b2a31627c0c63e7dba69"), "ClientDetails" : [ { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
108 Views
To skip a specific number of documents, use skip() along with limit. Let us create a collection with documents −> db.demo246.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0ea1627c0c63e7dba67") } > db.demo246.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0f91627c0c63e7dba68") }Display all documents from a collection with the help of find() method −> db.demo246.find();This will produce the following output −{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
658 Views
To sort by subdocument, use $sort in MongoDB. Let us create a collection with documents −> db.demo245.insertOne( ... { ... "_id": 101, ... "deatils": [ ... { "DueDate": new ISODate("2019-01-10"), "Value": 45}, ... {"DueDate": new ISODate("2019-11-10"), "Value": 34 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo245.insertOne( ... { ... "_id": 102, ... "details": [ ... { "DueDate": new ISODate("2019-12-11"), "Value": 29}, ... {"DueDate": ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
381 Views
To coalesce values means to merge them. To merge them into a single array, use $project in MongoDB.Let us create a collection with documents −> db.demo244.insertOne({"Value1":10, "Value2":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e4582e31627c0c63e7dba63") } > db.demo244.insertOne({"Value1":20, "Value2":30}); { "acknowledged" : true, "insertedId" : ObjectId("5e4582f11627c0c63e7dba64") }Display all documents from a collection with the help of find() method −> db.demo244.find();This will produce the following output −{ "_id" : ObjectId("5e4582e31627c0c63e7dba63"), "Value1" : 10, "Value2" : 20 } { "_id" : ObjectId("5e4582f11627c0c63e7dba64"), "Value1" : 20, "Value2" : 30 }Following is the query to coalesce values from different properties into ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
238 Views
For this, simply use aggregate() in MongoDB. Let us create a collection with documents −> db.demo243.insertOne({"userId":1, dueDate:new ISODate("2019-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4575f81627c0c63e7dba5f") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2019-11-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576011627c0c63e7dba60") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2020-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576151627c0c63e7dba61") } > db.demo243.insertOne({"userId":1, dueDate:new ISODate("2010-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e45761b1627c0c63e7dba62") }Display all documents from a collection with the help of find() method −> db.demo243.find();This will produce the following output −{ "_id" : ObjectId("5e4575f81627c0c63e7dba5f"), "userId" : 1, "dueDate" : ISODate("2019-01-10T00:00:00Z") } { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
529 Views
The easiest way to sort an array in MongoDB, use $sort. Let us create a collection with documents −> db.demo242.insertOne( ... ... {"details2": ... [ ... {"ShipingDate":new ISODate("2019-10-11"), "Price":1400}, ... {"ShipingDate":new ISODate("2019-10-01"), "Price":1600 } ... ] ... } ... ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4429229af932883c61ea44") }Display all documents from a collection with the help of find() method −> db.demo242.find();This will produce the following output −{ "_id" : ObjectId("5e4429229af932883c61ea44"), "details2" : [ { "ShipingDate" : ISODate("2019-10-11T00:00:00Z"), "Price" : 1400 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
192 Views
To get the maximum marks records, use sort() in descending order along with limit 1. Let us create a collection with documents −> db.demo241.insertOne({"Marks":67}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f729af932883c61ea40") } > db.demo241.insertOne({"Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f769af932883c61ea41") } > db.demo241.insertOne({"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f789af932883c61ea42") }Display all documents from a collection with the help of find() method −> db.demo241.find();This will produce the following output −{ "_id" : ObjectId("5e441f729af932883c61ea40"), "Marks" : 67 } { "_id" : ObjectId("5e441f769af932883c61ea41"), "Marks" : 89 } { "_id" : ObjectId("5e441f789af932883c61ea42"), "Marks" : 78 }Following ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
100 Views
To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −> db.demo240.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9d9af932883c61ea3e") } > db.demo240.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441da19af932883c61ea3f") }Display all documents from a collection with the help of find() method −> db.demo240.find();This will produce the following output −{ "_id" : ObjectId("5e441d969af932883c61ea3c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e441d9a9af932883c61ea3d"), "StudentName" : "Bob" } { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
222 Views
To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −> db.demo240.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9d9af932883c61ea3e") } > db.demo240.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441da19af932883c61ea3f") }Display all documents from a collection with the help of find() method −> db.demo240.find();This will produce the following output −{ "_id" : ObjectId("5e441d969af932883c61ea3c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e441d9a9af932883c61ea3d"), "StudentName" : "Bob" } { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
444 Views
For such cases, use $elemMatch. This 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.demo239.insertOne( ... { ... "Name" : "Chris", ... "details" : [ ... { "DueDate" : new ISODate("2019-01-21"), "ProductPrice" : 1270 }, ... { "DueDate" : new ISODate("2020-02-12"), "ProductPrice" : 2000 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e441c6bf4cebbeaebec5157") } > db.demo239.insertOne( ... { ... Read More