Fred
unread,Oct 29, 2012, 11:21:44 AM10/29/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongod...@googlegroups.com
Hello,
I'm trying to use the new aggregation framework and have a problem with non-existing fileds in some documents.
db.test.insert({'user' : 'F42', 'group' : 'admin', 'age':42})
db.test.insert({'user' : 'Fred', 'group' : 'admin', 'age':40})
db.test.insert({'user' : 'Bob', 'group' : 'admin'})
db.test.insert({'user' : 'Foo', 'group' : 'bar'})
db.test.insert({'user' : 'Bar', 'group' : 'bar'})
db.test.insert({'user' : 'B50', 'group' : 'bar', 'age' : 20})
db.test.insert({'user' : 'B20', 'group' : 'bar', 'age' : 50})
as you can see not all users have the age field. I'm trying to get two oldest users from each group:
db.test.aggregate({$project:{user:'$user', group:'$group', age:{$ifNull:['$age', null]}},$group:{_id:'$group', age:'$age'}})
which gives me
{
"errmsg" : "exception: the group aggregate field 'age' must be defined as an expression inside an object",
"code" : 15951,
"ok" : 0
}
doesn't $project produces an output which should be consumed by $group? If so, why it is complaining about non existing field? $ifNull makes sure it is there.
Regards
Fred