Suppose my Schema looks like so:
class User(Document):
username = StringField()
password = StringField()
category = StringField()
Imagine we have these existing categories: "avengers", "justice-leaguers", "villains"
, and I want to perform a "group by"
query for User.objects.all()
so that I can get something like this:
[
[<User: IronMan object>, <User: Thor object>, <User: Hulk object>],
[<User: Superman object>,<User: Batman object>],
[<User: Ultron object>, <User: Joker object>, <User: LexLuthor object>]
]
Or better yet:
{
"avengers": [<User: IronMan object>, <User: Thor object>, <User: Hulk object>],
"justice-leaguers": [<User: Superman object>,<User: Batman object>],
"villains": [<User: Ultron object>, <User: Joker object>, <User: LexLuthor object>]
}
I looked into MongoEngine's docs and have yet to find anything helpful. Thanks guys!
MongoDB group by is used to group data from the collection, we can achieve group by clause using aggregate function and group method in MongoDB. While using aggregate function with group by clause query operations is faster as normal query, basically aggregate function is used in multiple condition.
mongo. db; var pipeline = [ { "$group": { "_id": "$card_id", "likes": { "$sum": { "$cond": [ { "$eq": [ "$vote", 1 ] }, 1, 0 ] } }, "dislikes": { "$sum": { "$cond": [ { "$eq": [ "$vote", 2 ] }, 1, 0 ] } }, "total": { "$sum": { "$cond": [ { "$eq": [ "$vote", 1 ] }, 1, -1 ] } } }}, { "$sort": { "total": -1 } } ]; db.
The use of the $cond operator here determines whether the "status" is actually a defect or not and the result is a conditional $sum where only the "defect" values are counted. Once those are grouped per day you simply $divide the result, with another check with $cond to make sure you are not dividing by zero.
First, I discovered grouping with Mongoose is called Aggregate.
Since the OP asked for mongoengine (and thats what I needed) here is an example of this using mongoengine:
categories = User.objects.aggregate([{
'$group': { '_id': '$category', 'username': { '$push': '$username' }}
}])
This will return a pymongo command cursor iterator.
print list(categories)
Will return:
[{ "_id": "villains", "username": ["Ultron", "Joker", "LexLuthor"]},
{ "_id": "justice-leagers", "username": ["Superman", "Batman"]},
{ "_id": "avengers", "username": ["IronMan", "Thor", "Hulk"]}]
Using the aggregation framework, you only need to $group
documents by category:
db.User.aggregate([
{
$group: { _id: "$category", username: { $push: "$username" }}
}
])
Using the $push aggregation function, you will build an array containing all the username sharing the same category.
Given you sample data:
> db.User.find({},{category:1,username:1,_id:0})
{ "category" : "avengers", "username" : "IronMan" }
{ "category" : "avengers", "username" : "Thor" }
{ "category" : "avengers", "username" : "Hulk" }
{ "category" : "justice-leagers", "username" : "Superman" }
{ "category" : "justice-leagers", "username" : "Batman" }
{ "category" : "villains", "username" : "Ultron" }
{ "category" : "villains", "username" : "Joker" }
{ "category" : "villains", "username" : "LexLuthor" }
This will produce:
{ "_id" : "villains", "username" : [ "Ultron", "Joker", "LexLuthor" ] }
{ "_id" : "justice-leagers", "username" : [ "Superman", "Batman" ] }
{ "_id" : "avengers", "username" : [ "IronMan", "Thor", "Hulk" ] }
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