[SERVER-35182] Is it possible to limit the results of a group function? Created: 23/May/18  Updated: 28/Jun/18  Resolved: 23/May/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: AntonisMak Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Is it possible to limit the results of group function? I have a huge dataset and I want to group the results by an id and for each id to create an array with coordinates.

The output result is:

{u'_id': u'229136000', u'COORDINATES': [[160.6752, -27.80978]]} \{u'_id': u'567014500', u'COORDINATES': [[128.1873, -3.672835], [128.1876, -3.673245]]}
...

As I see the code groups all the id's in dataset and the performance is very bad.

Code:

getPositionsOfshipsgrouped = db.collection.aggregate([ { "$group" :
{"_id" :
  "$properties.MMSI",
  "total": {"$sum": 1},
  "COORDINATES": { "$push": "$geometry.coordinates"} }},
{"$match": {"total":{"$gte": 0}}} ])

If I add $limit in the end of this query, the group function executed and then the limit so there is no change in performance.

Is it possible to limit the results of a group function?



 Comments   
Comment by AntonisMak [ 23/May/18 ]

But the performance is very low as it must group the entire dataset first, right? Is there a more efficient solution? Thank you!

Comment by Ramon Fernandez Marina [ 23/May/18 ]

Thanks for your report. You can use $limit before $group to only group a subset of your data.

Please note that the SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag, where your question will reach a larger audience. A question like this involving more discussion would be best posted on the mongodb-user group. See also our Technical Support page for additional support resources.

Regards,
Ramón.

Generated at Thu Feb 08 04:39:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.