queries in mongodb command line -
i want query list cities in specific state populations between 6000 , 8000. in big data. :
{ "_id" : "01564", "city" : "sterling", "loc" : [ -71.775192, 42.435351 ], "pop" : 6481, "state" : "ma" } { "_id" : "01566", "city" : "sturbridge", "loc" : [ -72.084233, 42.112619 ], "pop" : 7001, "state" : "ma" } { "_id" : "01568", "city" : "west upton", "loc" : [ -71.608014, 42.173275 ], "pop" : 4682, "state" : "ma" } { "_id" : "01569", "city" : "uxbridge", "loc" : [ -71.632869, 42.074426 ], "pop" : 10364, "state" : "ma" }
i want return cities in massachusetts let above example. how can ? t tried :
>db.zipcodes.aggregate( [ { $group: { _id: "$city",statename:"$state", totalpop: { $sum: "$pop" } } }, { $match: { totalpop: { $gte: 1000 , $lte : 2000 },statename:"massachusetts" } } ] );
and got error below:
aggregate failed @ error (<anonymous>) @ doassert (src/mongo/shell/assert.js:11:14) @ function.assert.commandworked (src/mongo/shell/assert.js:254:5) @ dbcollection.aggregate (src/mongo/shell/collection.js:1278:12) @ (shell):1:13 @ src/mongo/shell/assert.js:13
you not need use aggregation framework.
db.zipcodes.find({ state: 'ma', pop: { $gte: 6000, $lte: 8000 } }, {city: 1})
if need name of cities (unique) can use distinct
db.zipcodes.distinct('city', { state: 'ma', pop: { $gte: 6000, $lte: 8000 } })
or use foreach
Comments
Post a Comment