Skip to content

Instantly share code, notes, and snippets.

@mcasimir
Created March 6, 2020 15:52
Show Gist options
  • Save mcasimir/229d8445c7a34e16610883929b714b90 to your computer and use it in GitHub Desktop.
Save mcasimir/229d8445c7a34e16610883929b714b90 to your computer and use it in GitHub Desktop.
Test cursor hasNext
const { MongoClient } = require('mongodb');
const assert = require('assert');
describe('mongodb cursor issue', function() {
this.timeout(2000);
let client;
let dbName;
let db;
let collection;
beforeEach(async () => {
dbName = `test-${Date.now()}`;
client = await MongoClient.connect(
'mongodb://localhost:27017',
{ useUnifiedTopology: true }
);
db = client.db(dbName);
collection = db.collection('coll1');
});
afterEach(async () => {
await db.dropDatabase();
client.close(true);
});
it('should fetch the right document range', async() => {
await collection.insertMany([
{doc: 1},
{doc: 2},
{doc: 3}
]);
const cursor = collection
.find({}, { projection: { _id: 0 }})
.skip(1)
.limit(1);
const results = [];
while(await cursor.hasNext()) {
results.push(await cursor.next());
}
assert.deepEqual(results, [{doc: 2}]);
});
});
{
"name": "test-cursor-hasnext",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"mongodb": "3.5.4"
},
"scripts": {
"test": "mocha index.spec.js"
},
"devDependencies": {
"mocha": "^7.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment