- 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 1357 Articles for MongoDB

Updated on 30-Jun-2020 08:15:02
To filter specific values, use $filter in MongoDB. Let us create a collection with documents −> db.demo751.insertOne( ... { ... _id: 101, ... details: [ ... { Name: "Robert", id:110, Age:21}, ... { Name: "Rae", id:110, Age:22}, ... {Name: "Ralph", id:116, Age:23} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo751.find().pretty();This will produce the following output −{ "_id" ... Read More 
Updated on 30-Jun-2020 08:12:49
To get max values for distinct elements, use $sort and $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo750.insertOne({id:101, value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102, value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74c8a930c785c834e567") } > db.demo750.insertOne({id:101, value:110}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74dba930c785c834e568") }Display all documents from a collection with the help of find() method −> db.demo750.find();This will produce the following output −{ "_id" : ObjectId("5eae74b2a930c785c834e566"), "id" : 101, "value" : 50 } { "_id" : ObjectId("5eae74c8a930c785c834e567"), "id" : 102, "value" : 40 } ... Read More 
Updated on 30-Jun-2020 08:11:21
To update each field of documents in collection with a formula, use MongoDB update(). Let us create a collection with documents −> db.demo749.insertOne({"details":[{"id":1, a:10}, {"id":2, a:5}, {"id":3, a:20}]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6fb0a930c785c834e565") }Display all documents from a collection with the help of find() method −> db.demo749.find().pretty();This will produce the following output −{ "_id" : ObjectId("5eae6fb0a930c785c834e565"), "details" : [ { "id" : 1, "a" : 10 }, { "id" : 2, ... Read More 
Updated on 30-Jun-2020 08:08:46
The document is a record in a collection. Each document has the limitation of 16 MB size. The document is wrapped inside the curly bracket ({}).Let us create a collection with documents −> db.demo748.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo748.insertOne({_id:102, Name:"Bob", Age:20}); { "acknowledged" : true, "insertedId" : 102 } > db.demo748.insertOne({_id:103, Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : 103 } > db.demo748.insertOne({_id:104, Name:"Sam", Age:19}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo748.find();This will produce the following output −{ "_id" ... Read More 
Updated on 30-Jun-2020 08:07:21
To push values, use $push along with update() with multi field set to TRUE. Let us create a collection with documents −> db.demo747.insertOne({"CountryName":["US", "IND"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a50a930c785c834e55f") } > db.demo747.insertOne({"CountryName":["UK", "US"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a57a930c785c834e560") } > db.demo747.insertOne({"CountryName":["UK", "IND"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a60a930c785c834e561") }Display all documents from a collection with the help of find() method −> db.demo747.find();This will produce the following output −{ "_id" : ObjectId("5eae6a50a930c785c834e55f"), "CountryName" : [ "US", "IND" ] } { "_id" : ObjectId("5eae6a57a930c785c834e560"), "CountryName" : [ "UK", "US" ] } ... Read More 
Updated on 30-Jun-2020 08:06:11
To find posts older than current date in MongoDB, use $lte. Let us create a collection with documents −> db.demo746.insertOne({DueDate:new Date("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eca930c785c834e55b") } > db.demo746.insertOne({DueDate:new Date("2020-10-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eda930c785c834e55c") } > db.demo746.insertOne({DueDate:new Date("2020-03-05")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eea930c785c834e55d") } > db.demo746.insertOne({DueDate:new Date("2020-05-04")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67f1a930c785c834e55e") }Display all documents from a collection with the help of find() method −> db.demo746.find();This will produce the following output −{ "_id" : ObjectId("5eae67eca930c785c834e55b"), "DueDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5eae67eda930c785c834e55c"), ... Read More 
Updated on 30-Jun-2020 08:04:42
To concatenate with condition in MongoDB, use $cond and in that, work with $concat. Let us create a collection with documents −> db.demo745.insertOne({Value1:"100", Value2:"100"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6419a930c785c834e554") } > db.demo745.insertOne({Value1:"40", Value2:"50"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6421a930c785c834e555") } > db.demo745.insertOne({Value1:"13", Value2:"45"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6429a930c785c834e556") }Display all documents from a collection with the help of find() method −> db.demo745.find();This will produce the following output −{ "_id" : ObjectId("5eae6419a930c785c834e554"), "Value1" : "100", "Value2" : "100" } { "_id" : ObjectId("5eae6421a930c785c834e555"), "Value1" : "40", "Value2" : "50" } ... Read More 
Updated on 30-Jun-2020 08:02:54
To find a certain element, use $project in MongoDB. Let us create a collection with documents −> db.demo744.insertOne( ... { ... studentInformation: ... [ ... { ... studentName:"Robert", ... grade:"A" ... }, ... { ... studentName:"Bob", ... grade:"C" ... }, ... { ... ... Read More 
Updated on 30-Jun-2020 08:01:12
For this, use $$ROOT in MongoDB. Let us create a collection with documents −> db.demo743.insertOne({id:1, "ShippingDate":"2020-01-21", value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ead893a57bb72a10bcf0680") } > db.demo743.insertOne({id:2, "ShippingDate":"2020-05-10", value:30}); { "acknowledged" : true, "insertedId" : ObjectId("5ead893c57bb72a10bcf0681") } > db.demo743.insertOne({id:3, "ShippingDate":"2020-05-10", value:60}); { "acknowledged" : true, "insertedId" : ObjectId("5ead894657bb72a10bcf0682") } > db.demo743.insertOne({id:1, "ShippingDate":"2020-05-11", value:75}); { "acknowledged" : true, "insertedId" : ObjectId("5ead895657bb72a10bcf0683") }Display all documents from a collection with the help of find() method −> db.demo743.find();This will produce the following output −{ "_id" : ObjectId("5ead893a57bb72a10bcf0680"), "id" : 1, "ShippingDate" : "2020-01-21", "value" : 50 ... Read More 
Updated on 30-Jun-2020 07:59:03
To fetch a specific element, iterate with forEach(). Let us create a collection with documents −> db.demo742.insertOne({ "userDetails": [ { "userName":"Robert", "CountryName":"UK" }, { "userName":"David", "CountryName":"AUS" } ]} ); { "acknowledged" : true, "insertedId" : ObjectId("5ead790b57bb72a10bcf0677") }Display all documents from a collection with the help of find() method −> db.demo742.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ead790b57bb72a10bcf0677"), "userDetails" : [ { "userName" : "Robert", "CountryName" : "UK" }, { "userName" : "David", ... Read More Advertisements