"errmsg" : "exception: the group aggregate field '_state' must be defined as an expression inside an object",

6,503 views
Skip to first unread message

janwen lou

unread,
Nov 2, 2013, 11:15:12 PM11/2/13
to mongod...@googlegroups.com
I try to do the examples based on the dataset:http://media.mongodb.org/zips.json
and i see the  docs about two phase of mongodb aggregate:http://docs.mongodb.org/manual/core/aggregation-pipeline/#aggregation-pipeline
so i try to match the state equals "LA",and group the total pop within the state,the shell is:
db.zips.aggregate({$match:{state:"LA"}},{$group:{_state:"$state",total:{$sum:"$pop"}}})

like the doc says,phrase one:match the "LA" state docs,and the sum the pop,and the get the error like post title:
Error: Printing Stack Trace
    at printStackTrace (src/mongo/shell/utils.js:37:15)
    at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
    at (shell):1:9
Sun Nov  3 04:08:19.542 aggregate failed: {
"errmsg" : "exception: the group aggregate field '_state' must be defined as an expression inside an object",
"code" : 15951,
"ok" : 0
 

after that error,i remenber r that mysql group syntax, if i want to do that i need use similar sql:select * from zips group by state having state='LA'
but i am just wondering why i can use the db.zips.aggregate({$match:{state:"LA"}},{$group:{_state:"$state",total:{$sum:"$pop"}}})

to do my request like the example said  

Nasir Rasul

unread,
Nov 3, 2013, 8:42:13 AM11/3/13
to mongod...@googlegroups.com
just a thought: i think you are required to have _id which is a special field.

try: db.zips.aggregate({$match:{
state:"LA"}},{$group:{_id:"$state",total:{$sum:"$pop"}}})

- Nasir


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages