0

I have the next collection:

{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f901223e4b0c2899fb22da0"}, "description": "description 1", "number": "01"}
{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f90126fe4b0c2899fb22da1"}, "description": "description 1", "number": "02"}
{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f901276e4b0c2899fb22da2"}, "description": "description 1", "number": "03"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f901286e4b0c2899fb22da3"}, "description": "description 1", "number": "04"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f90128ee4b0c2899fb22da4"}, "description": "description 1", "number": "05"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f901292e4b0c2899fb22da5"}, "description": "description 1", "number": "06"}

I'm trying to group by 'status' column:

cursor = db.command('aggregate', table, pipeline=[
            {'$project': {u'date': 1, u'status': 1, u'number': 1, u'description': 1}}
            {'$group': {'_id': u'$status'}}])

But I get this:

"ok"
"result"

Why?

1
  • Ok I've found that the results are it cursor['result'] but I don't have the columns which I have pointed in $project {u'ok': 1.0, u'result': [{u'_id': u'new'}, {u'_id': u'blocked'}]} -- it shows only _id column, but I need date, status, number, description as well Commented Apr 30, 2012 at 8:13

1 Answer 1

1

You will need to add those extra fields to the "$group" command in your pipeline, but you need to specify how the group command should aggregate them as it can't just put them all in one array. Please provide what you expect as output as well.

Sign up to request clarification or add additional context in comments.

1 Comment

I have found $first and $last operators so if I sort the results and then group them by a cloumn I can use $first/$last operators to define the columns I want to have in the results. So it's solved, thanks for help anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.