AmitDiwan

AmitDiwan

8,390 Articles Published

Articles by AmitDiwan

Page 621 of 839

MongoDB query to get average in aggregation of array element?

AmitDiwan
AmitDiwan
Updated on 15-May-2020 1K+ Views

To get an average of array elements, use $avg. Let us create a collection with documents −> db.demo584.insertOne({"Marks":[75,50,85,60,80]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e91d827fd2d90c177b5bcc2") }Display all documents from a collection with the help of find() method −> db.demo584.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e91d827fd2d90c177b5bcc2"),    "Marks" : [       75,       50,       85,       60,       80    ] }Following is the query to find avg in the aggregation of array element −> db.demo584.aggregate([ ...    { $project: { MarksAvg: { $avg: "$Marks"} } } ... ])This will produce the following output −{ "_id" : ObjectId("5e91d827fd2d90c177b5bcc2"), "MarksAvg" : 70 }

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 218 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") } > db.demo582.insertOne({FirstName:"Chris", Age:21, createDate:new ISODate("2020-04-21")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce4ffd2d90c177b5bcbe") } > db.demo582.insertOne({FirstName:"Chris", Age:22, createDate:new ISODate("2020-02-11")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce59fd2d90c177b5bcbf") } > db.demo582.insertOne({FirstName:"Chris", Age:22, createDate:new ISODate("2020-01-12")});{    "acknowledged" : true, "insertedId" : ObjectId("5e91ce6efd2d90c177b5bcc0") }Display all documents from a collection with the help of find() method ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-May-2020 325 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 collection with the help of find() method −> db.demo581.find();This will produce the following output −{ "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb6"), "Name" : "Chris", "Score" : 56 } { "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb7"), "Name" : "Bob", "Score" : 240 } { "_id" : ObjectId("5e91cbbcfd2d90c177b5bcb8"), "Name" : "David", "Score" : 150 }Following is the query ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-May-2020 476 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", ...       "Price":10.5 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e918cebfd2d90c177b5bcae") } > > db.demo580.insertOne( ... { ...    "Name":"John", ...    "Id1":"111", ...    "Id2":"", ...    "Price":9.5 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e918cecfd2d90c177b5bcaf") }Display all documents ...

Read More

MongoDB query to slice only one element of array

AmitDiwan
AmitDiwan
Updated on 15-May-2020 378 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,90,34,45,74 ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo579.find().pretty();This will produce the following output −{    "_id" : 101,    "details" : {       "FirstName" : "John"    },    "Marks" : [       56,       78,       90,       34,       45,       74    ] }Following is the query to slice only one the element of the array −> db.demo579.find({},{Marks : {$slice : 1} ,"details":0,"_id":0})This will produce the following output −{ "Marks" : [ 56 ] }

Read More

MongoDB aggregation with equality inside array?

AmitDiwan
AmitDiwan
Updated on 15-May-2020 318 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" ...       }, ... ...       "achievements" : [ ...          { ...             "winner" : "10th", ...             "year" : 2010, ...             "by" : "School" ...   ...

Read More

Sort MongoDB Collection by Array value?

AmitDiwan
AmitDiwan
Updated on 15-May-2020 312 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": [ ...             { ...                Name:"Chris", ...                Score:45 ...             }, ...             { ...                Name:"Bob", ...                Score:33 ...   ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-May-2020 343 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" : true, "insertedId" : ObjectId("5e916c55581e9acd78b427fd") } > db.demo576.insertOne({id:102, Name:"David", Marks:75}){    "acknowledged" : true, "insertedId" : ObjectId("5e916c5e581e9acd78b427fe") }Display all documents from a collection with the help of find() method −> db.demo576.find();This will produce the following output −{ "_id" : ObjectId("5e916c3b581e9acd78b427fa"), "id" : 101, "Name" : "Chris", "Marks" : 45 } { ...

Read More

Hide id field in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-May-2020 670 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 help of find() method −> db.demo575.find();This will produce the following output −{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "id" : 101, "Information" : { "Name" : "Chris", "Age" : 21 } } { "_id" : ObjectId("5e916a5f581e9acd78b427f8"), "id" : 102, "Information" : { "Name" : "David", "Age" : 20 } } { "_id" ...

Read More

Working with Aggregation to match all the values in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-May-2020 561 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"), ...             "Name": "Chris", ... ...             "UserInformation": { ...                "Name": "John", ...                "Marks": 78 ...             }, ...       ...

Read More
Showing 6201–6210 of 8,390 articles
« Prev 1 619 620 621 622 623 839 Next »
Advertisements