AmitDiwan has Published 10744 Articles

Filter sub documents by sub document in MongoDB?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:28:34

633 Views

For this, use aggregate() along with $unwind. Let us create a collection with documents −> db.demo583.insert([ ...    { ...       "details1" : [ ...          { ...             "details2" : [ ...               ... Read More

Apply the Group Accumulator Operator $first on the system variable $$ROOT to return reference to the root document?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:23:46

172 Views

The accumulators are operators that maintain their state as documents progress through the pipeline.The $ROOT references the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.Let us create a collection with documents −> db.demo582.insertOne({FirstName:"Chris", Age:21, createDate:new ISODate("2020-01-10")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce41fd2d90c177b5bcbd") } ... Read More

How do I order a list and add position to its items in MongoDB?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:19:28

299 Views

To order a list, use sort(). Let us create a collection with documents −> db.demo581.insertOne({"Name":"Chris", "Score":56});{    "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb6") } > db.demo581.insertOne({"Name":"Bob", "Score":240});{    "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb7") } > db.demo581.insertOne({"Name":"David", "Score":150});{    "acknowledged" : true, "insertedId" : ObjectId("5e91cbbcfd2d90c177b5bcb8") }Display all documents from a ... Read More

Sum unique properties in different collection elements in MongoDB and get the resultant Price?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:17:28

442 Views

To calculate the sum of unique properties in different collection elements, use $cond along with $group. This gives the resultant price.Let us create a collection with documents −> db.demo580.insertOne( ...    { ...       "Name":"John", ...       "Id1":"110", ...       "Id2":"111", ...     ... Read More

MongoDB query to slice only one element of array

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:12:27

348 Views

To slice only one element of array, use $slice in MongoDB. Let us create a collection with documents −> db.demo579.insertOne( ...    { ...       "_id" : 101, ...       "details" : { "FirstName" : "John" }, ...       "Marks" : [ 56, 78, ... Read More

MongoDB aggregation with equality inside array?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 06:10:03

273 Views

For this, use aggregate() along with $group. Let us create a collection with documents −> db.demo578.insertOne( ...    { ...       "_id" : 1, ...       "Info" : { ...          "firstName" : "Chris", ...          "lastName" : "Brown" ... ... Read More

Sort MongoDB Collection by Array value?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 05:56:12

274 Views

To sort MongoDB collection by Array value, use aggregate() along with $sort. Let us create a collection with documents −> db.demo577.insertOne( ...    { ... ...       "student": { ...          "details": [ ...             { ...       ... Read More

Sort and get only the first two fields in a “$group” operation in MongoDB

AmitDiwan

AmitDiwan

Updated on 15-May-2020 05:52:34

315 Views

Let us create a collection with documents −> db.demo576.insertOne({id:101, Name:"Chris", Marks:45}){    "acknowledged" : true, "insertedId" : ObjectId("5e916c3b581e9acd78b427fa") } > db.demo576.insertOne({id:101, Name:"John", Marks:55}){    "acknowledged" : true, "insertedId" : ObjectId("5e916c43581e9acd78b427fb") } > db.demo576.insertOne({id:101, Name:"John", Marks:65}){    "acknowledged" : true, "insertedId" : ObjectId("5e916c47581e9acd78b427fc") } > db.demo576.insertOne({id:102, Name:"David", Marks:37}){    "acknowledged" : ... Read More

Hide id field in MongoDB

AmitDiwan

AmitDiwan

Updated on 15-May-2020 05:50:19

625 Views

Let us create a collection with documents −> db.demo575.insertOne({id:101, Information:{Name:"Chris", Age:21}});{    "acknowledged" : true, "insertedId" : ObjectId("5e916a55581e9acd78b427f7") } > db.demo575.insertOne({id:102, Information:{Name:"David", Age:20}});{    "acknowledged" : true, "insertedId" : ObjectId("5e916a5f581e9acd78b427f8") } > db.demo575.insertOne({id:101, Information:{Name:"Bob", Age:23}});{    "acknowledged" : true, "insertedId" : ObjectId("5e916a67581e9acd78b427f9") }Display all documents from a collection with the ... Read More

Working with Aggregation to match all the values in MongoDB

AmitDiwan

AmitDiwan

Updated on 15-May-2020 05:48:36

531 Views

To match all the values in MongoDB, use $match along with $and in Aggregation. Let us create a collection with documents −> db.demo574.insertOne( ...    { ...       "details1": { ...          "details2": { ...             "dueDate": new ISODate("2020-01-10"), ... ... Read More

Advertisements