Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cursor In MongoDB

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 230
    Comment on it

    In mongo shell when find() query executed it return cursor with all document in the collection. However, if the returned cursor is not assigned to any variable then first 20 documents iterated automatically.

    In other word when a query run to mongoDB whatever it returns is cursor.

    Create a cursor in mongo shell

    var cursorVar = db.collection.find();
    

    Cursor Iteration

    The cursor.next() function used to retrieve the next batch and display it to shell. For confirmation that cursor has next element cursor.hasNext() function used. Following example used cursor to get data and iterate through loop. Run these series of command into mongo shell.

    var cursor = db.accounts.find();
    while(cursor.hasNext()) printjson(cursor.next());
    

    By default, the server will close the cursor after if found the inactivity of 10 minutes, or if client has exhausted the cursor this process is done automatically by the server.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: