- 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)
2K+ Views
The $match filters the documents to pass only the documents that match the specified condition to the next pipeline stage.The $project passes along the documents with the requested fields to the next stage in the pipeline.Let us see an example and create a collection with documents −> db.demo545.insert({Name:"Chris", details:{SubjectScore1:56, SubjectScore2:56}}) WriteResult({ "nInserted" : 1 }) > db.demo545.insert({Name:"David", details:{SubjectScore1:78, SubjectScore2:78}}) WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo545.find();This will produce the following output −{ "_id" : ObjectId("5e8e246e9e5f92834d7f05d5"), "Name" : "Chris", "details" : { "SubjectScore1" : 56, "SubjectScore2" : 56 } } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
520 Views
The mongo shell provides the NumberLong() wrapper to handle 64-bit integers. Following is the syntax using custom variable and print using toString() −var anyVariableName=NumberLong("yourLongNumber"); yourVariableName.toString();To understand the above concept, let us implement the above syntax −> var number=NumberLong("231231231231121231"); > number.toString();This will produce the following output −NumberLong("231231231231121231")The second example is as follows to display NumberLong −> var anotherNumber=NumberLong("765765765765567576"); > anotherNumber.toString();This will produce the following output −NumberLong("765765765765567576")
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
438 Views
To aggregate, use aggregate() in MongoDB. It calculates aggregate values for the data in a collection.Let us create a collection with documents −> db.demo544.insertOne({"DueTime":new ISODate("2020-01-10 12:10:20"), Amount:100});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1f029e5f92834d7f05ce") } > db.demo544.insertOne({"DueTime":new ISODate("2020-01-12 12:00:00"), Amount:500});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1f089e5f92834d7f05cf") } > db.demo544.insertOne({"DueTime":new ISODate("2020-01-12 12:10:20"), Amount:900});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1f109e5f92834d7f05d0") }Display all documents from a collection with the help of find() method −> db.demo544.find();This will produce the following output −{ "_id" : ObjectId("5e8e1f029e5f92834d7f05ce"), "DueTime" : ISODate("2020-01-10T12:10:20Z"), "Amount" : 100 } { "_id" : ObjectId("5e8e1f089e5f92834d7f05cf"), "DueTime" : ISODate("2020-01-12T12:00:00Z"), ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
311 Views
To find a document that matches the same array elements, use find() and within that, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements.Let us create a collection with documents −> db.demo543.insertOne({id:101, subject:["MySQL", "Java" ,"C", "Python"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1b2f9e5f92834d7f05c9") } > db.demo543.insertOne({id:102, subject:["MySQL", "MongoDB" ,"SQL Server"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1b2f9e5f92834d7f05ca") }Display all documents from a collection with the help of find() method −> db.demo543.find();This will produce the following output −{ "_id" : ObjectId("5e8e1b2f9e5f92834d7f05c9"), "id" : 101, "subject" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
178 Views
For this, use $ifNull. It evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. Let us first create a collection with documents −> db.demo542.insertOne({"ListOfName":["Chris", "David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e8cabc6ef4dcbee04fbbc17") } > db.demo542.insertOne({"ListOfName":null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8cabc8ef4dcbee04fbbc18") } > db.demo542.insertOne({"ListOfName":["David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e8cabd3ef4dcbee04fbbc19") } > db.demo542.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8cabdaef4dcbee04fbbc1a") }Display all documents from a collection with the help of find() method −> db.demo542.find();This will produce the following output −{ "_id" : ObjectId("5e8cabc6ef4dcbee04fbbc17"), "ListOfName" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
16K+ Views
To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −db.demo541.insertOne({"software":{"services":["gmail", "facebook", "yahoo"]}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca845ef4dcbee04fbbc11") } > db.demo541.insertOne({"software":{"services":["whatsapp", "twitter"]}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca85cef4dcbee04fbbc12") }Display all documents from a collection with the help of find() method −> db.demo541.find();This will produce the following output −{ "_id" : ObjectId("5e8ca845ef4dcbee04fbbc11"), "software" : { "services" : [ "gmail", "facebook", "yahoo" ] } } { "_id" : ObjectId("5e8ca85cef4dcbee04fbbc12"), "software" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
406 Views
To select documents grouped by field in MongoDB, use $group along with $project. Let us first create a collection with documents −> db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca368ef4dcbee04fbbc0e") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca36bef4dcbee04fbbc0f") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca370ef4dcbee04fbbc10") }Display all documents from a collection with the help of find() method −> db.demo540.find();This will produce the following output −{ "_id" : ObjectId("5e8ca368ef4dcbee04fbbc0e"), "id" : 1, "Name" : "Chris", "CountryName" : "US" } { "_id" : ObjectId("5e8ca36bef4dcbee04fbbc0f"), "id" : 1, "Name" : "Chris", "CountryName" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
719 Views
To remove subdocument from a document, use $pull along with update(). Let us first create a collection with documents −> db.demo538.insertOne( ... { ... id:101, ... "details": ... { ... anotherDetails: ... [ ... { ... "Name":"Chris", ... Age:21 ... }, ... { ... "Name":"David", ... Age:23 ... }, ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
129 Views
For sub-documents, use the dot notation. Let us first create a collection with documents −> db.demo537.insertOne({"details":{"SubjectName":"MongoDB"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a10ef4dcbee04fbbc05") } > db.demo537.insertOne({"details":{"SubjectName":"MySQL"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a4bef4dcbee04fbbc06") } > db.demo537.insertOne({"details":{"SubjectName":"Java"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a51ef4dcbee04fbbc07") }Display all documents from a collection with the help of find() method −> db.demo537.find();This will produce the following output −{ "_id" : ObjectId("5e8c8a10ef4dcbee04fbbc05"), "details" : { "SubjectName" : "MongoDB" } } { "_id" : ObjectId("5e8c8a4bef4dcbee04fbbc06"), "details" : { "SubjectName" : "MySQL" } } { "_id" : ObjectId("5e8c8a51ef4dcbee04fbbc07"), "details" : { "SubjectName" : "Java" } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
809 Views
For this, use MongoDB aggregate and within that, use $cond. The $cond evaluates a boolean expression to return one of the two specified return expressions.Let us first create a collection with documents −> db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843eef4dcbee04fbbc01") } > db.demo536.insertOne({"Name1":"David", "Name2":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc02") } > db.demo536.insertOne({"Name1":"Bob", "Name2":"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc03") } > db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc04") }Display all documents from a collection with the help of find() method −> db.demo536.find();This will produce the following output −{ "_id" ... Read More