Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Last active April 15, 2017 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oieduardorabelo/4fff511490c6be6ca0ae5bd51bc6ae9f to your computer and use it in GitHub Desktop.
Save oieduardorabelo/4fff511490c6be6ca0ae5bd51bc6ae9f to your computer and use it in GitHub Desktop.
const mongodb = require('mongodb');
test();
async function test() {
const db = await mongodb.MongoClient.connect('mongodb://localhost:27017/test');
await db.collection('Movies').drop();
await db.collection('Movies').insertMany([
{ name: 'Enter the Dragon' },
{ name: 'Ip Man' },
{ name: 'Kickboxer' }
]);
// Não usamos `await` aqui, ao invés disso, recebemos o Cursor
const cursor = db.collection('Movies').find();
// Usamos `next()` e `await` para iterar todos os valores do Cursor
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log(doc.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment