AmitDiwan has Published 10744 Articles

How to compare two fields in aggregation filter with MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 12:26:21

1K+ Views

For this, use aggregate() along with $filter. Let us create a collection with documents −> db.demo137.insertOne( ...    { ...       Name1:"Chris", ...       Name2:"David", ...       Detail1:[ ...          {_id:"John", Name3:"Chris"}, ...          {_id:"Chris", Name3:"Chris"}, ...   ... Read More

MongoDB query to sort subdocuments

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 12:21:34

203 Views

Let us create a collection with documents −> db.demo136.insertOne( ...    { ... ...       "Name":"Chris", ...       "Details":[ ...          { ...             "Id":"101", ...             "EmployeeName":"Mike", ...         ... Read More

MongoDB query to update a specific document from a collection

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 12:08:12

263 Views

To update, use $set along with UPDATE. Let us create a collection with documents −>db.demo135.insertOne({"Details":[{"EmployeeId":101, "EmployeeName":"Chris", "EmployeeSalary":45000}, {"EmployeeId":102, "EmployeeName":"Chris", "EmployeeSalary":45000}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31a5ddfdf09dd6d085399c") }Display all documents from a collection with the help of find() method −> db.demo135.find().pretty();This will produce the following output −{   ... Read More

How to update array of subdocuments in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 12:04:39

2K+ Views

To update, use update() along with $set. Let us create a collection with documents −>db.demo134.insertOne({"EmployeeId":101, "EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeAge":27}, {"EmployeeName":"Bob", "EmployeeAge":28}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e319b2f68e7f832db1a7f7c") } >db.demo134.insertOne({"EmployeeId":102, "EmployeeDetails":[{"EmployeeName":"David", "EmployeeAge":24}, {"EmployeeName":"Carol", "EmployeeAge":29}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e319b4468e7f832db1a7f7d") }Display all documents from a collection with ... Read More

Group by day/month/week based on the date range in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 12:01:22

2K+ Views

To group, use $week and $month in MongoDB. Let us create a collection with documents −> db.demo133.insertOne({"Rank":18, "DueDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31980968e7f832db1a7f78") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31982568e7f832db1a7f79") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-02-01")}); {    "acknowledged" ... Read More

Group across two columns in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 11:59:08

241 Views

To group across two columns, use $lookup. Let us create a collection with documents −> db.demo132.insertOne({"CountryName1":"US", "CountryName2":"UK", Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31950468e7f832db1a7f75") } > db.demo132.insertOne({"CountryName1":"UK", "CountryName2":"AUS", Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31951d68e7f832db1a7f76") } > db.demo132.insertOne({"CountryName1":"AUS", "CountryName2":"US", Value:40}); {    "acknowledged" : ... Read More

MongoDB aggregation to sum individual properties on an object in an array across documents

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 11:57:03

1K+ Views

For this, use aggregate() in MongoDB. Let us first create a collection with documents −> db.demo131.insertOne( ...    { ...       "_id": 101, ...       "Details": [ ...          { ...             "PlayerScore": 500, ...       ... Read More

How to query MongoDB a value with $lte, $in and $not to fetch specific values?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 11:53:29

226 Views

Let us first create a collection with documents −> db.demo130.insertOne( ...    { ... ...       "PlayerDetails":[{Score:56}, {Score:78}, {Score:89}, {Score:97}] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3065bf68e7f832db1a7f6d") } > db.demo130.insertOne( ... { ... ...    "PlayerDetails":[{Score:45}, {Score:56}] ... } ... ); ... Read More

Get distinct first words in a string with MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 11:51:36

165 Views

To get distinct first words in a string, use split(). Let us first create a collection with documents −> db.demo129.insertOne({"Words":"This is the MySQL", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e305d6368e7f832db1a7f6b") } > db.demo129.insertOne({"Words":"MongoDB is NOSQL database", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e305d7b68e7f832db1a7f6c") }Display ... Read More

Get distinct values from a column in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 11:48:32

553 Views

To get distinct values from a column, use distinct() in MongoDB. Let us first create a collection with documents −> db.demo128.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e30583d68e7f832db1a7f5d") } > db.demo128.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e30584068e7f832db1a7f5e") } > db.demo128.insertOne({"Name":"David"}); {    "acknowledged" : true, ... Read More

Advertisements