Skip to content

Instantly share code, notes, and snippets.

@cometofsky
Created December 23, 2024 16:03
Show Gist options
  • Save cometofsky/eb89eed46d650ca0ec747c39ccc27612 to your computer and use it in GitHub Desktop.
Save cometofsky/eb89eed46d650ca0ec747c39ccc27612 to your computer and use it in GitHub Desktop.
Commands inside mongodb shell
Episode 99::
db.COLLECTION.find() //find returns a cursor
db.COLLECTION.find().count()
db.COLLECTION.next() //gives next one document
const dataCursor = db.COLLECTION.find() //store cursor in a variable
dataCursor.next() //gives next one document
datacursor.forEach(doc => {printjson(doc)}) //prints all documents one by one (printjson is provided by mongo shell)
//after the above forEach the dataCursor.next() will give error, because the cursor is empty
dataCursor.hasNext() //gives if there is any next document available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment