group by - Mongodb limit result number with something like groupby -


i have mongodb collection multiple hierarchy, example use collection countries, contain cities, each document particular city , contain population value country , city (country_pop , city_pop in example) simplified, have in reality 6 hierarchies , big amount of data.

[   {     "country": "france",     "city": "paris",     "country_pop": 63000000,     "city_pop": 2200000,     "year": 2015   },   {     "country": "france",     "city": "marseille",     "country_pop": 63000000,     "city_pop": 850726,     "year": 2015   },   {     "country": "france",     "city": "toulouse",     "country_pop": 63000000,     "city_pop": 441802,     "year": 2015   },   {     "country": "france",     "city": "paris",     "country_pop": 63500000,     "city_pop": 2350000,     "year": 2016   },   {     "country": "france",     "city": "marseille",     "country_pop": 63500000,     "city_pop": 880726,     "year": 2016   },   {     "country": "france",     "city": "toulouse",     "country_pop": 63500000,     "city_pop": 445802,     "year": 2016   } ] 

i using doctrine mongo odm hydrate documents php object not requirement. want achieve in php script values display :

  • france :
    • 2015: 63000000
    • 2016: 63500000

currently, all documents match {"country": "france"} , in example 6 entries. in reality, big amount of data, it's kinda bad 6 entries two, 1 of year 2015 , 1 of year 2016 (because value of country_pop same in entries match {"year": "2016", "country": "france"}

during test php script use 100mo in order generate timeline of values on years , not acceptable. agree documents structure not have no control on it.

is there solution select country_pop ... groupby("country", "year") in order minimum results needed ?

i found group query in doctrine mongodb odm documentation: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html#group-queries there no real explanations.

also mongo documentation "group" method https://docs.mongodb.com/v3.2/reference/method/db.collection.group/ seems used aggregation sum or count , not looking for.

try starters , let me know if want additional data:

db.collectionname.aggregate([     {          $group: {              "_id": {                  "country": "$country",                  "year": "$year",                  "countrypop": "$country_pop"              }          }      } ]) 

this group results country, year, , country population , result in following data set:

{ "_id" : { "country" : "france", "year" : 2016, "countrypop" : 63500000 } } { "_id" : { "country" : "france", "year" : 2015, "countrypop" : 63000000 } } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -