Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do "group by" in mongoengine

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!

like image 618
benjaminz Avatar asked May 12 '15 17:05

benjaminz


People also ask

Can we do group by in MongoDB?

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.

How can you group by a particular value in MongoDB?

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.

How do I write a group condition in MongoDB?

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.

WHAT IS group in mongoose?

First, I discovered grouping with Mongoose is called Aggregate.


2 Answers

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"]}]
like image 64
Realistic Avatar answered Oct 13 '22 11:10

Realistic


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" ] }
like image 44
Sylvain Leroux Avatar answered Oct 13 '22 12:10

Sylvain Leroux