AmitDiwan has Published 10744 Articles

MongoDB aggregate to convert multiple documents into single document with an array?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:09:09

981 Views

For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo248.insertOne({"id":101, "Name":"Chris", "Age":21, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101, "Name":"Bob", "Age":22, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102, "Name":"Mike", "Age":20, "CountryName":"AUS"}); {   ... Read More

Find MongoDB documents that contains specific field?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:07:18

789 Views

To find documents that contain specific field, use $exists. Let us create a collection with documents −> db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"Chris", "ClientAge":34}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2a31627c0c63e7dba69") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"John", "ClientLastName":"Smith", "ClientAge":31}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b2be1627c0c63e7dba6a") } > db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientAge":33}]}); {    "acknowledged" : true, ... Read More

MongoDB query to skip n first documents?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:48:41

166 Views

To skip a specific number of documents, use skip() along with limit. Let us create a collection with documents −> db.demo246.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); {   ... Read More

Sort by subdocument in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:46:58

848 Views

To sort by subdocument, use $sort in MongoDB. Let us create a collection with documents −> db.demo245.insertOne( ...   { ...      "_id": 101, ...      "deatils": [ ...         { "DueDate": new ISODate("2019-01-10"), "Value": 45}, ...         {"DueDate": new ISODate("2019-11-10"), "Value": ... Read More

Coalesce values from different properties into a single array with MongoDB aggregation

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:44:46

640 Views

To coalesce values means to merge them. To merge them into a single array, use $project in MongoDB.Let us create a collection with documents −> db.demo244.insertOne({"Value1":10, "Value2":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4582e31627c0c63e7dba63") } > db.demo244.insertOne({"Value1":20, "Value2":30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4582f11627c0c63e7dba64") }Display ... Read More

A single MongoDB query to orderby date and group by user

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:42:26

289 Views

For this, simply use aggregate() in MongoDB. Let us create a collection with documents −> db.demo243.insertOne({"userId":1, dueDate:new ISODate("2019-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4575f81627c0c63e7dba5f") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2019-11-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4576011627c0c63e7dba60") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2020-01-31")}); {    "acknowledged" : ... Read More

Easiest way to sort an array in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:40:53

626 Views

The easiest way to sort an array in MongoDB, use $sort. Let us create a collection with documents −> db.demo242.insertOne( ... ...   {"details2": ...      [ ...         {"ShipingDate":new ISODate("2019-10-11"), "Price":1400}, ...         {"ShipingDate":new ISODate("2019-10-01"), "Price":1600 } ...      ] ... ... Read More

Get the maximum mark records from a collection with documents in MongoDB query

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:39:20

269 Views

To get the maximum marks records, use sort() in descending order along with limit 1. Let us create a collection with documents −> db.demo241.insertOne({"Marks":67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441f729af932883c61ea40") } > db.demo241.insertOne({"Marks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441f769af932883c61ea41") } > db.demo241.insertOne({"Marks":78}); {   ... Read More

Get the maximum mark records from a collection with documents in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:32:25

157 Views

To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −> db.demo240.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" ... Read More

MongoDB Aggregate to limit the number of records

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:30:28

284 Views

To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −> db.demo240.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" ... Read More

Advertisements