new Cursor(collection, condition, projection)
This is a Cursor instance constructor. find() and other some methods use this internally.
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
collection |
KagoDB | source collection | |
condition |
Object | function |
<optional> |
query parameters or function |
projection |
Object | function |
<optional> |
mapping parameters or function |
- Source:
Methods
-
count(callback) → {Cursor}
-
This invokes a callback function with the number of items found
Parameters:
Name Type Description callback
function function(err, count) {} - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().count(function(err, count) { console.log(count); });
-
each(callback) → {Cursor}
-
This invokes a callback function with each items found.
Parameters:
Name Type Description callback
function function(err, item) {} - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().each(function(err, item) { if (err) { console.error(err); } else if (!item) { // EOF } else { console.error(item); } });
-
<private> index(callback) → {Cursor}
-
This invokes a callback function with an index for all items of the collection whether a condition is given or not.
Parameters:
Name Type Description callback
function function(err, list) {} - Source:
Returns:
instance itself for method chaining- Type
- Cursor
-
limit(limit) → {Cursor}
-
This specifies a limit parameters
Parameters:
Name Type Description limit
Number limit parameter - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().limit(100).toArray();
-
nextObject(callback) → {Cursor}
-
This returns the next object at the cursor.
Parameters:
Name Type Description callback
function function(err, item) {} - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
var cursor = collection.find(); cursor.nextObject(function(err, item){ console.log(item); });
-
offset(offset) → {Cursor}
-
This specifies a offset parameters
Parameters:
Name Type Description offset
Number offset parameter - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().offset(100).toArray();
-
rewind() → {Cursor}
-
This resets the cursor position to the original state.
- Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
var cursor = collection.find(); cursor.nextObject(function(err, item){ console.log(item); // first item cursor.rewind(); cursor.nextObject(function(err, item){ console.log(item); // first item again }); });
-
sort(order) → {Cursor}
-
This specifies a sort parameters
Parameters:
Name Type Description order
function | Object sort function or parameters - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().sort(function(a, b){ return a.price - b.price || b.stock - a.stock; }).toArray(); var sort = {price: 1, stock: -1}); collection.find().sort(sort).toArray(); // same order
-
toArray(callback) → {Cursor}
-
This invokes a callback function with a list of items found.
Parameters:
Name Type Description callback
function function(err, list) {} - Source:
Returns:
instance itself for method chaining- Type
- Cursor
Example
collection.find().toArray(function(err, list) { if (err) { console.error(err); } else { list.forEach(function(item) { console.log(item); }); } });