MongoDB Aggregate subfields with individual date ranges - database

I have a schema which is:
{
"_id" : "12345678",
"action1" : [
{
"date" : "2021-01-15",
"value" : 20
},
{
"date" : "2021-01-14",
"value" : 16
}
],
"action2" : [
{
"date" : "2021-01-15",
"value" : 30
},
{
"date" : "2021-01-14",
"value" : 10
}
],
"action3" : [
{
"date" : "2021-01-15",
"value" : 40
},
{
"date" : "2021-01-14",
"value" : 20
}
],
"action4" : [
{
"date" : "2021-01-15",
"value" : 60
},
{
"date" : "2021-01-14",
"value" : 40
}
]
}
Now I want to write an aggregate query to filter out counts within a date range (for last 7 days, or 30 days or 90 days)
so the final sum should look something like the following:
{
_id: "12345678"
action1 : {
alltime: number,
last7Days : number,
last30Days: number,
last90Days: number
},
action2: {
alltime: number,
last7Days : number,
last30Days: number,
last90Days: number
},
action3: {
alltime: number,
last7Days : number,
last30Days: number,
last90Days: number
},
action4: {
alltime: number,
last7Days : number,
last30Days: number,
last90Days: number
},
}
I am trying to get the total number of actions using $project and $match for a particular _id
But how can I filter the past7days/30days/90days data
My query looks like the following
db.collection.aggregate([
{
$project: {
alltimeAction1: {$sum: "$action1.value"},
alltimeAction2: {$sum: "$action2.value"},
alltimeAction3: {$sum: "$action3.value"},
alltimeAction4: {$sum: "$action4.value"}
}
},
{
$match: {
_id: "12345678"
}
}
])
is mapReduce the only option available?

db.test1.aggregate([
{
"$project": {//Reshape actions, you need this as you have dynamic keys
data: {
"$objectToArray": "$$ROOT"
}
}
},
{//Denormalize
"$unwind": "$data"
},
{//Denormalize
"$unwind": "$data.v"
},
{
"$project": {//Formatting date
"_id": 1,
"key": "$data.k",
"date": {
"$dateFromString": {
"dateString": "$data.v.date",
"format":"%Y-%m-%d"
}
},
"value": "$data.v.value"
}
},
{
"$addFields": {//Getting the conditions ready
"7Days": {
$gte:["$date", new Date(new Date().getTime() - (7*24*3600*1000))]
},
"30Days": {
$gte:["$date", new Date(new Date().getTime() - (30*24*3600*1000))]
}
}
},
{
$group:{//Grouping them, you can add few more cases
"_id": "$key",
"7days":{
$sum:"$value"
},
"30days":{
$sum:"$value"
}
}
}
])
You don't need to do this much complex query if you have flattened schema where actionName can be identified by a constant field name.

Related

Write a mongo query to count the data where similar data in array?

Sample data: there are multiple similar collection:
{
"_id" : NumberLong(301),
"telecom" : [
{
"countryCode" : {
"value" : "+1"
},
"extension" : [
{
"url" : "primary",
"value" : [
"true"
]
}
],
"modifiedValue" : {
"value" : "8887778888"
},
"system" : {
"value" : "phone"
},
"useCode" : {
"value" : "Home Phone"
},
"value" : {
"value" : "8887778888"
}
},
{
"extension" : [
{
"url" : "primary",
"value" : [
"true"
]
}
],
"modifiedValue" : {
"value" : "abc#test.com"
},
"system" : {
"value" : "email"
},
"useCode" : {
"value" : "work"
},
"value" : {
"value" : "abc#test.com"
}
}
]
}
Issue: I want to cont the collection where telecom.system.value = email and countryCode doesn't exist in the email part object. here I am attaching a script but I need one line query
var count = 0,i;
db.getCollection('practitioner').find({"telecom.system.value":"email"}).forEach(function(practitioner){
//print("updating : " +practitioner._id.valueOf())
telecom = practitioner.telecom.valueOf()
for(i= 0;i<telecom.length;i++){
if(telecom[i].system.value === 'email' && telecom[i].countryCode){
count+=1;
}
}
});
print(" Total count of the practitioner with country code in email object: "+count)
Above mention, the script is working fine and the output is as I expected. but the script is not optimised and I want to write in a single line query. Thanks in advance.
You can try aggregation method aggregate(),
Approach 1:
$match condition for countryCode should exists and system.value should be email
$filter to iterate loop of telecom array and check both condition, this will return expected elements
$size to get total element from above filter result
$group by null and count total
var result = await db.getCollection('practitioner').aggregate([
{
$match: {
telecom: {
$elemMatch: {
countryCode: { $exists: true },
"system.value": "email"
}
}
}
},
{
$project: {
count: {
$size: {
$filter: {
input: "$telecom",
cond: {
$and: [
{ $ne: [{ $type: "$$this.countryCode" }, "missing"] },
{ $eq: ["$$this.system.value", "email"] }
]
}
}
}
}
}
},
{
$group: {
_id: null,
count: { $sum: "$count" }
}
}
]);
print("Total count of the practitioner with country code in email object: "+result[0].count);
Playground
Approach 2:
$match condition for countryCode should exists and system.value should be email
$unwind deconstruct telecom array
$match to filter document using above conditions
$count to get total elements count
var result = await db.getCollection('practitioner').aggregate([
{
$match: {
telecom: {
$elemMatch: {
countryCode: { $exists: true },
"system.value": "email"
}
}
}
},
{ $unwind: "$telecom" },
{
$match: {
"telecom.countryCode": { $exists: true },
"telecom.system.value": "email"
}
},
{ $count: "count" }
]);
print("Total count of the practitioner with country code in email object: "+result[0].count);
Playground
I have not tested the performance but you can check and use as per your requirement.

Querying Arrays in MongoDB

I have a Mongo collection called players and in each document there are two entries called transactions and autographs. Both are arrays with objects inside containing a timestamp.
How can I use this db.collection.count( { timestamp: {$gt: 1585526400000} }) to display how many have been inputed into the db in the last 7 days?
Assuming the following two simplified records are in your database:
{
"_id" : ObjectId("5e8b8b66c1f8161eeeab762f"),
"transactions" : [
{
"timestamp" : ISODate("2020-04-05T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-04-02T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-04-01T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-01-06T00:25:20.202Z")
}
]
},
{
"_id" : ObjectId("5e8b9008b29982222cd38888"),
"transactions" : [
{
"timestamp" : ISODate("2020-04-04T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-02-03T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-02-01T00:25:20.202Z")
},
{
"timestamp" : ISODate("2020-02-06T00:25:20.202Z")
}
]
}
Then you can get the count of the array elements which your condition as follows:
db.getCollection('players').aggregate([{
$project: {
transactionsCount: {
$size: {
$filter: {
input: "$transactions",
as: "item",
cond: {
$gte: ["$$item.timestamp", ISODate('2020-03-30 00:00:00.000Z')]
}
}
}
}
}
}
])
Result:
{
"_id" : ObjectId("5e8b8b66c1f8161eeeab762f"),
"transactionsCount" : 3
}
{
"_id" : ObjectId("5e8b9008b29982222cd38888"),
"transactionsCount" : 1
}

Query Mongodb Sum of firsts element of an array of objects

I would like to write a query for summing each field payment of the first object inside an array, for each element of my database.
The schema is the following:
var schema = new Schema({
plate : String,
category : String,
brand : String,
model : String,
sign : String,
tax : [{
date : { type: Date, default: Date.now },
payment : { type: Number, default: 0 },
}],
});
I wrote the following function for my query:
function(callback){
Machine.aggregate(
[
{$unwind: "$tax"},
{$group : {
_id : null ,
tot : { $sum: "$tax.payment"}
}}
]
,callback);
}
But in this way I retrieve the sum of all the payments inside the array tax. My goal is to take only the first, so I tried with $tax.0.payment and using arrayElemAt : [$tax,0] but all my trials gave a tot = 0.
The idea here is pick out the first element of each of payment array field via $arrayElemAt with projection and then group-sum the field $group $sum.
Query:
db.collection.aggregate([
{
$project: {
firstPayment: {
$arrayElemAt: [
"$tax",
0
]
}
}
},
{
$group: {
_id: null,
PaymentSum: {
$sum: "$firstPayment.payment"
}
}
}
]);
Demo O/P:
[
{
"PaymentSum": 11,
"_id": null
}
]
Machine.aggregate({$unwind:
{path: "$tax"}
},
{$group:{
_id: "$_id",
payment: {$first: "$tax.payment"}
}},
{$group: {
_id: null,
total: {$sum: "$payment"}
}}
)
Explanation:
First I used $unwind on tax, then in the first $group stage I grouped them according to _id,
that way I will get the first payment information from unwinded tax array.
Then I used $sum to add them in the second $group stage.
I tested with this data:
Machine collection docs:
{
"_id" : ObjectId("5dbf09a4d7912bcbc61ee9e4"),
"tax" : [
{
"payment" : 10
},
{
"payment" : 20
}
]
},
{
"_id" : ObjectId("5dbf09aad7912bcbc61ee9e5"),
"tax" : [
{
"payment" : 30
},
{
"payment" : 40
}
]
},
{
"_id" : ObjectId("5dbf09afd7912bcbc61ee9e6"),
"tax" : [
{
"payment" : 50
},
{
"payment" : 60
}
]
}
The result I got is:
{ "_id" : null, "tot" : 90 }
I hope this fulfills your requirements.
Add $arrayElemAt in your aggregate like this..
Machine.aggregate(
[
{$unwind: "$tax"},
{$group : {
_id : null ,
tot : { $sum: { $arrayElemAt : [ "$tax.payment", 0 ]}
}}
]
,callback);

In mongo DB, how do I grab multiple counts in a single query?

I'm currently trying to massage out counts from the mLab API for reasons I don't have control over. So I want to grab the data I need from there in one query so I can limit the amount of API calls.
Assuming that my data looks like this:
{
"_id": {
"$oid": "12345"
},
"dancer": "Beginner",
"pirate": "Advanced",
"chef": "Mid",
"beartamer": "Mid",
"swordsman": "Mid",
"total": "Mid"
}
I know I can do 6 queries with something similar to:
db.score.aggregate({"$group": { _id: {"total":"$total"}, count: {$sum:1} }} )
but how do I query to get the count for each key? I'd like to see something akin to:
{ "_id" : { "total" : "Advanced" }, "count" : 1 }
{ "_id" : { "total" : "Mid" }, "count" : 1 }
{ "_id" : { "total" : "Beginner" }, "count" : 4 }
{ "_id" : { "pirate" : "Advanced" }, "count" : 1 }
//...etc
The following should give you precisely what you want:
db.scores.aggregate({
$project: {
"_id": 0 // get rid of the "_id" field since we do not want to count it
}
}, {
$project: {
"doc": {
$objectToArray: "$$ROOT" // transform all documents into key-value pairs
}
}
}, {
$unwind: "$doc" // flatten the resulting array into separate documents
}, {
$group: {
"_id": "$doc", // group by distinct key-value combination
"count": { $sum: 1 } // count documents per bucket
}
}, {
$project: {
"_id": { // some more transformation magic to recreate the desired output structure
$mergeObjects: [
{ $arrayToObject: [ [ "$_id" ] ] },
{ "count": "$count" }
]
},
}
}, {
$replaceRoot: {
"newRoot": "$_id" // this moves the contents of the "_id" field to the root of the documents
}
})

MongoDb SUM GROUP BY WHERE

I am very new to MongoDb. I have a collection Order which has multiple documents as follows.
{
"vendor": "amazon",
"date": ISODate("2016-12-05T21:10:39.100Z"),
"products" : [
{
"id": NumberLong(590573),
"totalSold": NumberLong(59),
"totalCost": NumberLong(7350),
"variations": [
{
"varId": NumberLong(1),
"totalSoldV": NumberLong(30),
"totalCostV": NumberLong(3000)
},
{
"varId": NumberLong(2),
"totalSoldV": NumberLong(29),
"totalCostV": NumberLong(4350)
},
]
}
]
}
So what I am trying to achieve is for a particular product.id I want to calculate sum(totalSold) and sum(totalCost) group by date. I have been playing around with aggregate but haven't been able to do so.
db.collection.aggregate([
{$unwind: "$products"},
{$match: {"products.id":NumberLong(590573) }},
{
$group: {
_id: {
year : { "$year" : "$date" },
month : { "$month" : "$date" },
day : { "$dayOfMonth" : "$date" },
hour : { "$hour" : "$date" },
minute : { "$minute" : "$date" },
},
sumTotalSold: {$sum: "$products.totalSold"}, sumTotalCost: {$sum: "$products.totalCost"}
}
}
]).pretty();
Result:
{
"_id" : {
"year" : 2016,
"month" : 12,
"day" : 5,
"hour" : 21,
"minute" : 10
},
"sumTotalSold" : NumberLong(79),
"sumTotalCost" : NumberLong(9850)
}

Resources