MongoEngine / MongoEngine/mongoengine
aggregate_sum and EmbeddedDocumentListField
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
I had to manually do an “$unwind” to obtain a result, otherwise the result always returned a “0".
How can I develop an “aggregate sum” over a document with a “EmbeddedDocumentListField” or a “List” in order to obtain that result?. Is “$unwind” the correct / only way to do it?
class WordUsed(mongoengine.EmbeddedDocument):
word_id = mongoengine.IntField()
name = mongoengine.StringField()
hits = mongoengine.IntField()
errors = mongoengine.IntField()
class LogGames(mongoengine.EmbeddedDocument):
game_id = mongoengine.IntField()
used_words = mongoengine.EmbeddedDocumentListField(WordUsed)
used_phrases = mongoengine.EmbeddedDocumentListField(PhraseUsed)
class LogSession(mongoengine.Document):
session_date = mongoengine.DateTimeField()
log_games = mongoengine.EmbeddedDocumentListField(LogGames)
used_words = mongoengine.EmbeddedDocumentListField(WordUsed)
used_phrases = mongoengine.EmbeddedDocumentListField(PhraseUsed)
Original code:
>>> sessions = LogSession.objects.all()
>>> sessions.sum("used_words.hits")
0
>>> sessions.aggregate_sum("used_words.hits")
0
My manual code:
>>> result = LogSession.objects.aggregate({"$unwind": "$used_words"}, {'$group': {'_id': 'total', 'total': {'$sum': '$used_words.hits'}}})
{'_id': 'total', 'total': 45.0}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file or test is named. Reproduce the provided LogSession example and compare aggregate_sum("used_words.hits") with the manual aggregation using $unwind and $group. Done means establishing whether the zero result is a bug or supported limitation, with a regression test or clarified documented behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100