asyncfunctionhandleMovie(cursor) {
const movie = await cursor.next()
if (movie === undefined) {
return
}
console.log(movie)
// ... process and decide whether to keep iterating ...
handleMovie(cursor)
}
// Work with the movies collectionconst moviesCollection = db.collection('movieDetails')
moviesCollection
.find({}, {limit: 10})
.iterator()
.then(handleMovie)
RemoteMongoCursor
RemoteMongoCursor is an iterator over documents, which allows asynchronous traversal.
A RemoteMongoCursor can be obtained from the result of a
find
oraggregate
operation on a RemoteMongoCollection by calling RemoteMongoReadOperation.iterator.Example
async function handleMovie(cursor) { const movie = await cursor.next() if (movie === undefined) { return } console.log(movie) // ... process and decide whether to keep iterating ... handleMovie(cursor) } // Work with the movies collection const moviesCollection = db.collection('movieDetails') moviesCollection .find({}, {limit: 10}) .iterator() .then(handleMovie)
See Also