I can't tell if MongoEngine supports the aggregate framework.
Is it possible to run this query using MongoEngine?
db.collection.aggregate([
{ "$group": {
"_id": {
"year": { "$year": "$utc_timestamp" },
"month": { "$month": "$utc_timestamp" },
"day": { "$dayOfMonth": "$utc_timestamp" },
},
"defects": {
"$sum": { "$cond": [
{ "$eq": [ "$status", "defect" ] },
1,
0
]}
},
"totalCount": { "$sum": 1 }
}},
{ "$project": {
"defect_rate": {
"$cond": [
{ "$eq": [ "$defects", 0 ] },
0,
{ "$divide": [ "$defects", "$totalCount" ] }
]
}
}}
])
If not, can I just run it directly (raw) through MongoEngine?
You can get the raw "collection" object as implemented by the pymongo driver using the ._get_collection() method on the class.
Class._get_collection().aggregate([
{ '$group': {
'_id': {
'year': { '$year': '$utc_timestamp' },
'month': { '$month': '$utc_timestamp' },
'day': { '$dayOfMonth': '$utc_timestamp' },
},
'defects': {
'$sum': { '$cond': [
{ '$eq': [ '$status', 'defect' ] },
1,
0
]}
},
'totalCount': { '$sum': 1 }
}},
{ '$project': {
'defect_rate': {
'$cond': [
{ '$eq': [ '$defects', 0 ] },
0,
{ '$divide': [ '$defects', '$totalCount' ] }
]
}
}}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With