Constructor for a cursor object that handles all the operations on query result using find. This cursor object is unidirectional and cannot traverse backwards. Clients should not be creating a cursor directly, but use find to acquire a cursor. (INTERNAL TYPE)
- class Cursor()¶
Arguments:
- db (db) – the database object to work with.
- collection (collection) – the collection to query.
- selector (object) – the query selector.
- fields (object) – an object containing what fields to include or exclude from objects returned.
- [options] (object) – additional options for the collection.
Constant Name | Value | Description |
---|---|---|
Cursor.INIT | 0 | Init state |
Cursor.OPEN | 1 | Cursor open |
Cursor.CLOSED | 2 | Cursor closed |
Cursor.GET_MORE | 3 | Cursor performing a get more |
Clones a given cursor but uses new options
Arguments: |
|
---|---|
Returns: | object [options] additional options for the collection when cloning. |
Resets this cursor to its initial state. All settings like the query string, tailable, batchSizeValue, skipValue and limits are preserved.
Returns: | cursor returns itself with rewind applied. |
---|
Examples
An example showing the information returned by indexInformation
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { var docs = []; // Insert 100 documents with some data for(var i = 0; i < 100; i++) { var d = new Date().getTime() + i*1000; docs[i] = {'a':i, createdAt:new Date(d)}; } // Create collection db.createCollection('Should_correctly_rewind_and_restart_cursor', function(err, collection) { assert.equal(null, err); // insert all docs collection.insert(docs, {w:1}, function(err, result) { assert.equal(null, err); // Grab a cursor using the find var cursor = collection.find({}); // Fetch the first object off the cursor cursor.nextObject(function(err, item) { assert.equal(0, item.a) // Rewind the cursor, resetting it to point to the start of the query cursor.rewind(); // Grab the first object again cursor.nextObject(function(err, item) { assert.equal(0, item.a) db.close(); }) }) }) }); });
Returns an array of documents. The caller is responsible for making sure that there is enough memory to store the results. Note that the array only contain partial results when this cursor had been previouly accessed. In that case, cursor.rewind() can be used to reset the cursor.
Arguments: |
|
---|---|
Returns: | null |
Examples
An example showing the information returned by indexInformation
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection to hold our documents db.createCollection('test_array', function(err, collection) { // Insert a test document collection.insert({'b':[1, 2, 3]}, {w:1}, function(err, ids) { // Retrieve all the documents in the collection collection.find().toArray(function(err, documents) { assert.equal(1, documents.length); assert.deepEqual([1, 2, 3], documents[0].b); db.close(); }); }); }); });
Iterates over all the documents for this cursor. As with {cursor.toArray}, not all of the elements will be iterated if this cursor had been previouly accessed. In that case, {cursor.rewind} can be used to reset the cursor. However, unlike {cursor.toArray}, the cursor will only hold a maximum of batch size elements at any given time if batch size is specified. Otherwise, the caller is responsible for making sure that the entire result can fit the memory.
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example iterating over a query using the each function of the cursor.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('test_to_a_after_each', function(err, collection) { assert.equal(null, err); // Insert a document in the collection collection.insert({'a':1}, {w:1}, function(err, ids) { // Grab a cursor var cursor = collection.find(); // Execute the each command, triggers for each document cursor.each(function(err, item) { // If the item is null then the cursor is exhausted/empty and closed if(item == null) { // Show that the cursor is closed cursor.toArray(function(err, items) { assert.ok(err != null); // Let's close the db db.close(); }); }; }); }); }); });
Determines how many result the query for this cursor will return
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example showing the count function of the cursor.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Creat collection db.createCollection('cursor_count_collection', function(err, collection) { assert.equal(null, err); // Insert some docs collection.insert([{a:1}, {a:2}], {w:1}, function(err, docs) { assert.equal(null, err); // Do a find and get the cursor count collection.find().count(function(err, count) { assert.equal(null, err); assert.equal(2, count); db.close(); }) }); }); });
Sets the sort parameter of this cursor to the given value.
This method has the following method signatures: (keyOrList, callback) (keyOrList, direction, callback)
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Examples
A simple example showing the use of sort on the cursor.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_sort_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Do normal ascending sort collection.find().sort([['a', 1]]).nextObject(function(err, item) { assert.equal(null, err); assert.equal(1, item.a); // Do normal descending sort collection.find().sort([['a', -1]]).nextObject(function(err, item) { assert.equal(null, err); assert.equal(3, item.a); db.close(); }); }); }); }); });
Sets the limit parameter of this cursor to the given value.
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Examples
A simple example showing the use of limit on the cursor
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_limit_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Limit to only one document returned collection.find().limit(1).toArray(function(err, items) { assert.equal(null, err); assert.equal(1, items.length); db.close(); }); }); }); });
Specifies a time limit for a query operation. After the specified time is exceeded, the operation will be aborted and an error will be returned to the client. If maxTimeMS is null, no limit is applied.
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Sets the read preference for the cursor
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Sets the skip parameter of this cursor to the given value.
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Examples
A simple example showing the use of skip on the cursor
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_skip_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Skip one document collection.find().skip(1).nextObject(function(err, item) { assert.equal(null, err); assert.equal(2, item.a); db.close(); }); }); }); });
Sets the batch size parameter of this cursor to the given value.
Arguments: |
|
---|---|
Returns: | cursor an instance of this object. |
Examples
A simple example showing the use of batchSize on the cursor, batchSize only regulates how many documents are returned for each batch using the getMoreCommand against the MongoDB server
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_batch_size_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Do normal ascending sort collection.find().batchSize(1).nextObject(function(err, item) { assert.equal(null, err); assert.equal(1, item.a); db.close(); }); }); }); });
Gets the next document from the cursor.
Arguments: |
|
---|
Examples
A simple example showing the use of nextObject.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_next_object_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Do normal ascending sort collection.find().nextObject(function(err, item) { assert.equal(null, err); assert.equal(1, item.a); db.close(); }); }); }); });
Gets a detailed information about how the query is performed on this cursor and how long it took the database to process it.
Arguments: |
|
---|
Examples
A simple example showing the use of the cursor explain function.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a collection db.createCollection('simple_explain_collection', function(err, collection) { assert.equal(null, err); // Insert some documents we can sort on collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) { assert.equal(null, err); // Do normal ascending sort collection.find().explain(function(err, explaination) { assert.equal(null, err); db.close(); }); }); }); });
Returns a Node Transform Stream interface for this cursor.
Returns: | cursorstream returns a stream object. |
---|
Examples
A simple example showing the use of the cursor stream function.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a lot of documents to insert var docs = [] for(var i = 0; i < 100; i++) { docs.push({'a':i}) } // Create a collection db.createCollection('test_stream_function', function(err, collection) { assert.equal(null, err); // Insert documents into collection collection.insert(docs, {w:1}, function(err, ids) { // Peform a find to get a cursor var stream = collection.find().stream(); // Execute find on all the documents stream.on('close', function() { db.close(); }); stream.on('data', function(data) { assert.ok(data != null); }); }); }); });
Close the cursor.
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example showing the use of the cursor close function.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a lot of documents to insert var docs = [] for(var i = 0; i < 100; i++) { docs.push({'a':i}) } // Create a collection db.createCollection('test_close_function_on_cursor', function(err, collection) { assert.equal(null, err); // Insert documents into collection collection.insert(docs, {w:1}, function(err, ids) { // Peform a find to get a cursor var cursor = collection.find(); // Fetch the first object cursor.nextObject(function(err, object) { assert.equal(null, err); // Close the cursor, this is the same as reseting the query cursor.close(function(err, result) { assert.equal(null, err); db.close(); }); }); }); }); });
Check if the cursor is closed or open.
Returns: | boolean returns the state of the cursor. |
---|
Examples
A simple example showing the use of the cursor close function.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a lot of documents to insert var docs = [] for(var i = 0; i < 100; i++) { docs.push({'a':i}) } // Create a collection db.createCollection('test_is_close_function_on_cursor', function(err, collection) { assert.equal(null, err); // Insert documents into collection collection.insert(docs, {w:1}, function(err, ids) { // Peform a find to get a cursor var cursor = collection.find(); // Fetch the first object cursor.nextObject(function(err, object) { assert.equal(null, err); // Close the cursor, this is the same as reseting the query cursor.close(function(err, result) { assert.equal(null, err); assert.equal(true, cursor.isClosed()); db.close(); }); }); }); }); });