Using the aggregate command to return a cursor

456 views
Skip to first unread message

John Lynn

unread,
May 26, 2014, 4:56:46 PM5/26/14
to mongod...@googlegroups.com
The docs say "Using the aggregate command to return a cursor is a low-level operation, intended for authors of drivers."

Does that mean that I won't be able to get a cursor from something like:

db.runCommand(   {aggregate : "zipcodes", pipeline : [   {$group: {_id: "$state", totalPop : { $sum: "$pop"}}} ] },{cursor: { batchSize: 10 }})

It seems to return a document, just like pre-2.6 

John Lynn

unread,
May 26, 2014, 4:57:43 PM5/26/14
to mongod...@googlegroups.com
Sorry - I meant to specify: this is in the mongo shell. Thanks

Rob Moore

unread,
May 26, 2014, 9:37:56 PM5/26/14
to mongod...@googlegroups.com

John -

The 2.6 version of the shell always uses a cursor when you use the aggregate() helper there is no reason to manually construct the command.

Having said that, if you really want to do this manually. The document your are getting back will have the cursor information in it if the server started a cursor.  The document should look like:

> db.runCommand( { aggregate : 'usertable', pipeline : [ { '$match' : { "_id" : 1 } } ] , cursor : {} } )
{
"cursor" : {
"id" : NumberLong(0),
"ns" : "ycsb.usertable",
"firstBatch" : [ ]
},
"ok" : 1
}

If there had been results the first few documents would have been in the 'firstBatch' array.  You can then use getMore requests with the cursor's 'id' (if it is non-zero) to get more documents from the aggregation. I am not sure that the driver has a generic "getMore" request capability but you can probably wrap the cursor's id in the internal iterator class and it will "just work".

I'll state again that all of this is handled by the aggregate() shell helper.  I strongly encourage you to just use it.


Rob.

John Lynn

unread,
May 26, 2014, 10:12:42 PM5/26/14
to mongod...@googlegroups.com
Hmm, I don't get results such as yours at all. I just get back what look like pre-2.6 results, with the "results" array, and the "ok" at the end of the document. 

Yep, I am aware of the aggregate() helper, I'm just trying to explore the behavior of 2.6 aggregate pipeline command, with and without cursor. 


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/hDElCyZvHgE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1732f06c-e193-41ec-939b-cdac59e44b6d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rob Moore

unread,
May 26, 2014, 10:27:17 PM5/26/14
to mongod...@googlegroups.com


On Monday, May 26, 2014 10:12:42 PM UTC-4, John Lynn wrote:
Hmm, I don't get results such as yours at all. I just get back what look like pre-2.6 results, with the "results" array, and the "ok" at the end of the document. 

Yep, I am aware of the aggregate() helper, I'm just trying to explore the behavior of 2.6 aggregate pipeline command, with and without cursor. 


I think there might be a typo in your example:

   db.runCommand(   {aggregate : "zipcodes", pipeline : [   {$group: {_id: "$state", totalPop : { $sum: "$pop"}}} ] },{cursor: { batchSize: 10 }})

should be: 

  db.runCommand(   {aggregate : "zipcodes", pipeline : [   {$group: {_id: "$state", totalPop : { $sum: "$pop"}}} ], cursor: { batchSize: 10 }} ) 

i.e., Move the cursor field into the 'aggregate' document. I honestly thought the runCommand did that automatically with the second document but it is the only difference between your version and mine.

Rob.


John Lynn

unread,
May 26, 2014, 11:30:41 PM5/26/14
to mongod...@googlegroups.com
Ah - right you are! Good eye! Thanks

John Lynn

unread,
May 27, 2014, 7:33:39 AM5/27/14
to mongod...@googlegroups.com
So is it correct to say: if you for some reason need to execute a pipeline and get results back as a document instead of a cursor (pre-2.6 behavior), then executing the aggregate database command using runCommand *without* specifying the "cursor" option, is the way to do it?

Rob Moore

unread,
May 27, 2014, 3:28:29 PM5/27/14
to mongod...@googlegroups.com

> So is it correct to say: if you for some reason need to execute a pipeline and get results back as a document instead of a cursor (pre-2.6 behavior),
> then executing the aggregate database command using runCommand *without* specifying the "cursor" option, is the way to do it?

Yes - If for some reason you need the results as a document then sending a command manually without the cursor options will work.

Promise me you will first look long and hard at the reason to make sure it is really valid.

Rob.

Reply all
Reply to author
Forward
0 new messages