
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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

1K+ Views
To check for duplicates in an array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo756.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eb01e0d5637cd592b2a4add") } > db.demo756.insertOne({"SubjectName":["MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eb01e2b5637cd592b2a4ade") }Display all documents from a collection with the help of find() method −> db.demo756.find();This will produce the following output −{ "_id" : ObjectId("5eb01e0d5637cd592b2a4add"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL" ] }Following is the query to check for ... Read More

352 Views
To sort, use sort() in MongoDB. Let us create a collection with documents −> db.demo755.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e72a930c785c834e572") } > db.demo755.insertOne({"Value":10.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e75a930c785c834e573") } > db.demo755.insertOne({"Value":9.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e79a930c785c834e574") } > db.demo755.insertOne({"Value":12.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e7fa930c785c834e575") } > db.demo755.insertOne({"Value":11.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e87a930c785c834e576") } > db.demo755.insertOne({"Value":6}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e97a930c785c834e577") }Display all documents from a collection with the help of find() method −> db.demo755.find();This will produce ... Read More

2K+ Views
Let us create a collection with documents −> db.demo754.insertOne({"DateOfBirth":new Date("2000-05-03")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9b2da930c785c834e56f") } > db.demo754.insertOne({"DateOfBirth":new Date("2010-01-21")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9b34a930c785c834e570") } > db.demo754.insertOne({"DateOfBirth":new Date("2018-05-03")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9b3da930c785c834e571") }Display all documents from a collection with the help of find() method −> db.demo754.find();This will produce the following output −{ "_id" : ObjectId("5eae9b2da930c785c834e56f"), "DateOfBirth" : ISODate("2000-05-03T00:00:00Z") } { "_id" : ObjectId("5eae9b34a930c785c834e570"), "DateOfBirth" : ISODate("2010-01-21T00:00:00Z") } { "_id" : ObjectId("5eae9b3da930c785c834e571"), "DateOfBirth" : ISODate("2018-05-03T00:00:00Z") }Following is the query to convert date of birth to age −> db.demo754.aggregate( ... Read More

531 Views
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

681 Views
To get max values for distinct elements, use sortandgroup 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

204 Views
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

410 Views
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

108 Views
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

2K+ Views
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

379 Views
To concatenate with condition in MongoDB, use condandinthat,workwithconcat. 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