Skip to content

Commit 35dede2

Browse files
committed
Added cursor iteration example using generators
1 parent 371b516 commit 35dede2

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/functional/operation_generators_example_tests.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,61 @@ exports.shouldCorrectlyPeformNextObjectOnCursorWithGenerators = {
43444344
}
43454345
}
43464346

4347+
/**
4348+
* A simple example showing the use of next and co module to iterate over cursor
4349+
*
4350+
* @example-class Cursor
4351+
* @example-method nextObject
4352+
* @ignore
4353+
*/
4354+
exports.shouldCorrectlyPeformNextOnCursorWithGenerators = {
4355+
// Add a tag that our runner can trigger on
4356+
// in this case we are setting that node needs to be higher than 0.10.X to run
4357+
metadata: { requires: { generators:true, topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
4358+
4359+
// The actual test we wish to run
4360+
test: function(configuration, test) {
4361+
var co = require('co');
4362+
4363+
co(function*() {
4364+
// Connect
4365+
var db = yield configuration.newDbInstance({w:1}, {poolSize:1}).open();
4366+
// LINE var MongoClient = require('mongodb').MongoClient,
4367+
// LINE co = require('co');
4368+
// LINE test = require('assert');
4369+
// LINE
4370+
// LINE co(function*() {
4371+
// LINE var db = yield MongoClient.connect('mongodb://localhost:27017/test');
4372+
// REPLACE configuration.writeConcernMax() WITH {w:1}
4373+
// REMOVE-LINE test.done();
4374+
// BEGIN
4375+
4376+
// Create a collection
4377+
var collection = db.collection('simple_next_object_collection_next_with_generators');
4378+
4379+
// Insert some documents we can sort on
4380+
yield collection.insertMany([{a:1}, {a:2}, {a:3}], configuration.writeConcernMax());
4381+
4382+
// Get a cursor
4383+
var cursor = collection.find({});
4384+
4385+
// Get the document
4386+
var doc = null;
4387+
var docs = [];
4388+
4389+
// Iterate over all the cursor items
4390+
while((doc = yield cursor.next()) != null) {
4391+
docs.push(doc);
4392+
}
4393+
4394+
test.equal(3, docs.length);
4395+
db.close();
4396+
test.done();
4397+
});
4398+
// END
4399+
}
4400+
}
4401+
43474402
/**
43484403
* A simple example showing the use of the cursor explain function using a Generator and the co module.
43494404
*

0 commit comments

Comments
 (0)