3

I am using db.runCommand(document) of Java Mongo driver api.

Sample code I am using

Document resultDocument = db.runCommand({
    find: 'collectionName',
    filter: { startDate:{$gte:'#startDate',$lte:'#endDate'}},
    projection: { _id:0}});

I am using find command. My query is returning 101 records only as default batch size is 101. I want to create a cursor as mentioned in api below.

Snippet in mongo documentation: https://docs.mongodb.org/manual/reference/command/find/#dbcmd.find

Executes a query and returns the first batch of results and the cursor id, from which the client can construct a cursor.

I don't want to give batchSize as I am not sure how many records my query will return. So I want to create a cursor and iterate over it.

Can any one help how to create a cursor from id returned by db.runCommand in mongo java driver to iterate over all the records.

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
PSS
  • 51
  • 4
  • Why would you want to do this? Unless you are writing your own higher level abstraction from the driver (and probably not even then) you likely really should be using the collection methods instead. I think you really should have a good look at [Find or Query Data with the Java Driver](https://docs.mongodb.org/getting-started/java/query/) as a basic example of working with the driver. – Blakes Seven Mar 03 '16 at 12:01
  • 2
    I know about Find method in java driver. I want to create a generic method which will execute any command you give in the form of JSON(I can give find , update etc to same method and it will give me result) . So I am using runcommand – PSS Mar 03 '16 at 12:44
  • Any solution for it? – Horcrux7 Jan 08 '21 at 09:12

1 Answers1

0

You may get the next batches using getMore

Use in conjunction with commands that return a cursor, e.g. find and aggregate, to return subsequent batches of documents currently pointed to by the cursor.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
mse
  • 11