AmitDiwan has Published 10744 Articles

How to convert birth date records to age with MongoDB

AmitDiwan

AmitDiwan

Updated on 01-Jul-2020 06:41:02

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 ... Read More

Filter specific values from a MongoDB document

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:15:02

575 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", ... Read More

How to get max values for distinct elements in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:12:49

715 Views

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}); ... Read More

MongoDB query to update each field of documents in collection with a formula?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:11:21

232 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 −> ... Read More

What is the maximum size of a document in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:08:46

476 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" ... Read More

Pushing values into array with multi field set to TRUE?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:07:21

136 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"]}); ... Read More

Find posts that are older than current date in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:06:11

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" ... Read More

Concatenate with condition in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:04:42

422 Views

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"}); ... Read More

How to find a certain element in the MongoDB embedded document?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:02:54

188 Views

To find a certain element, use $project in MongoDB. Let us create a collection with documents −> db.demo744.insertOne( ...    { ...       studentInformation: ...       [ ...          { ...             studentName:"Robert", ...         ... Read More

How can I extract entire documents based on how they compare with their whole collection?

AmitDiwan

AmitDiwan

Updated on 30-Jun-2020 08:01:12

130 Views

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, ... Read More

Advertisements