12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- "use strict";
- var inherits = require('util').inherits
- , f = require('util').format
- , formattedOrderClause = require('./utils').formattedOrderClause
- , handleCallback = require('./utils').handleCallback
- , ReadPreference = require('./read_preference')
- , MongoError = require('mongodb-core').MongoError
- , Readable = require('stream').Readable || require('readable-stream').Readable
- , Define = require('./metadata')
- , CoreCursor = require('mongodb-core').Cursor
- , Map = require('mongodb-core').BSON.Map
- , CoreReadPreference = require('mongodb-core').ReadPreference;
- var flags = ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'exhaust', 'partial'];
- var fields = ['numberOfRetries', 'tailableRetryInterval'];
- var push = Array.prototype.push;
- var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
- CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
- var self = this;
- var state = Cursor.INIT;
- var streamOptions = {};
-
- var numberOfRetries = options.numberOfRetries || 5;
- var tailableRetryInterval = options.tailableRetryInterval || 500;
- var currentNumberOfRetries = numberOfRetries;
-
- var promiseLibrary = options.promiseLibrary;
-
- if(!promiseLibrary) {
- promiseLibrary = typeof global.Promise == 'function' ?
- global.Promise : require('es6-promise').Promise;
- }
-
- Readable.call(this, {objectMode: true});
-
- this.s = {
-
- numberOfRetries: numberOfRetries
- , tailableRetryInterval: tailableRetryInterval
- , currentNumberOfRetries: currentNumberOfRetries
-
- , state: state
-
- , streamOptions: streamOptions
-
- , bson: bson
-
- , ns: ns
-
- , cmd: cmd
-
- , options: options
-
- , topology: topology
-
- , topologyOptions: topologyOptions
-
- , promiseLibrary: promiseLibrary
-
- , currentDoc: null
- }
-
- if(self.s.options.noCursorTimeout == true) {
- self.addCursorFlag('noCursorTimeout', true);
- }
-
- this.sortValue = self.s.cmd.sort;
- }
- inherits(Cursor, Readable);
- CoreCursor.prototype._next = CoreCursor.prototype.next;
- for(var name in CoreCursor.prototype) {
- Cursor.prototype[name] = CoreCursor.prototype[name];
- }
- var define = Cursor.define = new Define('Cursor', Cursor, true);
- Cursor.prototype.hasNext = function(callback) {
- var self = this;
-
- if(typeof callback == 'function') {
- if(self.s.currentDoc){
- return callback(null, true);
- } else {
- return nextObject(self, function(err, doc) {
- if(!doc) return callback(null, false);
- self.s.currentDoc = doc;
- callback(null, true);
- });
- }
- }
-
- return new this.s.promiseLibrary(function(resolve, reject) {
- if(self.s.currentDoc){
- resolve(true);
- } else {
- nextObject(self, function(err, doc) {
- if(self.s.state == Cursor.CLOSED || self.isDead()) return resolve(false);
- if(err) return reject(err);
- if(!doc) return resolve(false);
- self.s.currentDoc = doc;
- resolve(true);
- });
- }
- });
- }
- define.classMethod('hasNext', {callback: true, promise:true});
- Cursor.prototype.next = function(callback) {
- var self = this;
-
- if(typeof callback == 'function') {
-
- if(self.s.currentDoc) {
- var doc = self.s.currentDoc;
- self.s.currentDoc = null;
- return callback(null, doc);
- }
-
- return nextObject(self, callback)
- }
-
- return new this.s.promiseLibrary(function(resolve, reject) {
-
- if(self.s.currentDoc) {
- var doc = self.s.currentDoc;
- self.s.currentDoc = null;
- return resolve(doc);
- }
- nextObject(self, function(err, r) {
- if(err) return reject(err);
- resolve(r);
- });
- });
- }
- define.classMethod('next', {callback: true, promise:true});
- Cursor.prototype.filter = function(filter) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.query = filter;
- return this;
- }
- define.classMethod('filter', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.maxScan = function(maxScan) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.maxScan = maxScan;
- return this;
- }
- define.classMethod('maxScan', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.hint = function(hint) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.hint = hint;
- return this;
- }
- define.classMethod('hint', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.min = function(min) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.min = min;
- return this;
- }
- define.classMethod('min', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.max = function(max) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.max = max;
- return this;
- }
- define.classMethod('max', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.returnKey = function(value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.returnKey = value;
- return this;
- }
- define.classMethod('returnKey', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.showRecordId = function(value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.showDiskLoc = value;
- return this;
- }
- define.classMethod('showRecordId', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.snapshot = function(value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.snapshot = value;
- return this;
- }
- define.classMethod('snapshot', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.setCursorOption = function(field, value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(fields.indexOf(field) == -1) throw MongoError.create({message: f("option %s not a supported option %s", field, fields), driver:true });
- this.s[field] = value;
- if(field == 'numberOfRetries')
- this.s.currentNumberOfRetries = value;
- return this;
- }
- define.classMethod('setCursorOption', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.addCursorFlag = function(flag, value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(flags.indexOf(flag) == -1) throw MongoError.create({message: f("flag %s not a supported flag %s", flag, flags), driver:true });
- if(typeof value != 'boolean') throw MongoError.create({message: f("flag %s must be a boolean value", flag), driver:true});
- this.s.cmd[flag] = value;
- return this;
- }
- define.classMethod('addCursorFlag', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.addQueryModifier = function(name, value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(name[0] != '$') throw MongoError.create({message: f("%s is not a valid query modifier"), driver:true});
-
- var field = name.substr(1);
-
- this.s.cmd[field] = value;
-
- if(field == 'orderby') this.s.cmd.sort = this.s.cmd[field];
- return this;
- }
- define.classMethod('addQueryModifier', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.comment = function(value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.comment = value;
- return this;
- }
- define.classMethod('comment', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.maxAwaitTimeMS = function(value) {
- if(typeof value != 'number') throw MongoError.create({message: "maxAwaitTimeMS must be a number", driver:true});
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.maxAwaitTimeMS = value;
- return this;
- }
- define.classMethod('maxAwaitTimeMS', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.maxTimeMS = function(value) {
- if(typeof value != 'number') throw MongoError.create({message: "maxTimeMS must be a number", driver:true});
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.maxTimeMS = value;
- return this;
- }
- define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.maxTimeMs = Cursor.prototype.maxTimeMS;
- define.classMethod('maxTimeMs', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.project = function(value) {
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- this.s.cmd.fields = value;
- return this;
- }
- define.classMethod('project', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.sort = function(keyOrList, direction) {
- if(this.s.options.tailable) throw MongoError.create({message: "Tailable cursor doesn't support sorting", driver:true});
- if(this.s.state == Cursor.CLOSED || this.s.state == Cursor.OPEN || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- var order = keyOrList;
-
-
- if(Array.isArray(order) && Array.isArray(order[0])) {
- order = new Map(order.map(function(x) {
- var value = [x[0], null];
- if(x[1] == 'asc') {
- value[1] = 1;
- } else if(x[1] == 'desc') {
- value[1] = -1;
- } else if(x[1] == 1 || x[1] == -1) {
- value[1] = x[1];
- } else {
- throw new MongoError("Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
- }
- return value;
- }));
- }
- if(direction != null) {
- order = [[keyOrList, direction]];
- }
- this.s.cmd.sort = order;
- this.sortValue = order;
- return this;
- }
- define.classMethod('sort', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.batchSize = function(value) {
- if(this.s.options.tailable) throw MongoError.create({message: "Tailable cursor doesn't support batchSize", driver:true});
- if(this.s.state == Cursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", driver:true});
- this.s.cmd.batchSize = value;
- this.setCursorBatchSize(value);
- return this;
- }
- define.classMethod('batchSize', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.collation = function(value) {
- this.s.cmd.collation = value;
- return this;
- }
- define.classMethod('collation', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.limit = function(value) {
- if(this.s.options.tailable) throw MongoError.create({message: "Tailable cursor doesn't support limit", driver:true});
- if(this.s.state == Cursor.OPEN || this.s.state == Cursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(typeof value != 'number') throw MongoError.create({message: "limit requires an integer", driver:true});
- this.s.cmd.limit = value;
-
- this.setCursorLimit(value);
- return this;
- }
- define.classMethod('limit', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.skip = function(value) {
- if(this.s.options.tailable) throw MongoError.create({message: "Tailable cursor doesn't support skip", driver:true});
- if(this.s.state == Cursor.OPEN || this.s.state == Cursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
- if(typeof value != 'number') throw MongoError.create({message: "skip requires an integer", driver:true});
- this.s.cmd.skip = value;
- this.setCursorSkip(value);
- return this;
- }
- define.classMethod('skip', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.nextObject = Cursor.prototype.next;
- var nextObject = function(self, callback) {
- if(self.s.state == Cursor.CLOSED || self.isDead && self.isDead()) return handleCallback(callback, MongoError.create({message: "Cursor is closed", driver:true}));
- if(self.s.state == Cursor.INIT && self.s.cmd.sort) {
- try {
- self.s.cmd.sort = formattedOrderClause(self.s.cmd.sort);
- } catch(err) {
- return handleCallback(callback, err);
- }
- }
-
- self._next(function(err, doc) {
- self.s.state = Cursor.OPEN;
- if(err) return handleCallback(callback, err);
- handleCallback(callback, null, doc);
- });
- }
- define.classMethod('nextObject', {callback: true, promise:true});
- var loop = function(self, callback) {
-
- if(self.bufferedCount() == 0) return;
-
- self._next(callback);
-
- return loop;
- }
- Cursor.prototype.next = Cursor.prototype.nextObject;
- define.classMethod('next', {callback: true, promise:true});
- Cursor.prototype.each = function(callback) {
-
- this.rewind();
-
- this.s.state = Cursor.INIT;
-
- _each(this, callback);
- };
- define.classMethod('each', {callback: true, promise:false});
- var _each = function(self, callback) {
- if(!callback) throw MongoError.create({message: 'callback is mandatory', driver:true});
- if(self.isNotified()) return;
- if(self.s.state == Cursor.CLOSED || self.isDead()) {
- return handleCallback(callback, MongoError.create({message: "Cursor is closed", driver:true}));
- }
- if(self.s.state == Cursor.INIT) self.s.state = Cursor.OPEN;
-
- var fn = null;
-
- if(self.bufferedCount() > 0) {
- while(fn = loop(self, callback)) fn(self, callback);
- _each(self, callback);
- } else {
- self.next(function(err, item) {
- if(err) return handleCallback(callback, err);
- if(item == null) {
- self.s.state = Cursor.CLOSED;
- return handleCallback(callback, null, null);
- }
- if(handleCallback(callback, null, item) == false) return;
- _each(self, callback);
- })
- }
- }
- Cursor.prototype.forEach = function(iterator, callback) {
- this.each(function(err, doc){
- if(err) { callback(err); return false; }
- if(doc != null) { iterator(doc); return true; }
- if(doc == null && callback) {
- var internalCallback = callback;
- callback = null;
- internalCallback(null);
- return false;
- }
- });
- }
- define.classMethod('forEach', {callback: true, promise:false});
- Cursor.prototype.setReadPreference = function(r) {
- if(this.s.state != Cursor.INIT) throw MongoError.create({message: 'cannot change cursor readPreference after cursor has been accessed', driver:true});
- if(r instanceof ReadPreference) {
- this.s.options.readPreference = new CoreReadPreference(r.mode, r.tags, {maxStalenessSeconds: r.maxStalenessSeconds});
- } else if(typeof r == 'string'){
- this.s.options.readPreference = new CoreReadPreference(r);
- } else if(r instanceof CoreReadPreference) {
- this.s.options.readPreference = r;
- }
- return this;
- }
- define.classMethod('setReadPreference', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.toArray = function(callback) {
- var self = this;
- if(self.s.options.tailable) throw MongoError.create({message: 'Tailable cursor cannot be converted to array', driver:true});
-
- if(typeof callback == 'function') return toArray(self, callback);
-
- return new this.s.promiseLibrary(function(resolve, reject) {
- toArray(self, function(err, r) {
- if(err) return reject(err);
- resolve(r);
- });
- });
- }
- var toArray = function(self, callback) {
- var items = [];
-
- self.rewind();
- self.s.state = Cursor.INIT;
-
- var fetchDocs = function() {
- self._next(function(err, doc) {
- if(err) return handleCallback(callback, err);
- if(doc == null) {
- self.s.state = Cursor.CLOSED;
- return handleCallback(callback, null, items);
- }
-
- items.push(doc)
-
- if(self.bufferedCount() > 0) {
- var docs = self.readBufferedDocuments(self.bufferedCount())
-
- if(self.s.transforms && typeof self.s.transforms.doc == 'function') {
- docs = docs.map(self.s.transforms.doc);
- }
- push.apply(items, docs);
- }
-
- fetchDocs();
- })
- }
- fetchDocs();
- }
- define.classMethod('toArray', {callback: true, promise:true});
- Cursor.prototype.count = function(applySkipLimit, opts, callback) {
- var self = this;
- if(self.s.cmd.query == null) throw MongoError.create({message: "count can only be used with find command", driver:true});
- if(typeof opts == 'function') callback = opts, opts = {};
- opts = opts || {};
-
- if(typeof callback == 'function') return count(self, applySkipLimit, opts, callback);
-
- return new this.s.promiseLibrary(function(resolve, reject) {
- count(self, applySkipLimit, opts, function(err, r) {
- if(err) return reject(err);
- resolve(r);
- });
- });
- };
- var count = function(self, applySkipLimit, opts, callback) {
- if(typeof applySkipLimit == 'function') {
- callback = applySkipLimit;
- applySkipLimit = true;
- }
- if(applySkipLimit) {
- if(typeof self.cursorSkip() == 'number') opts.skip = self.cursorSkip();
- if(typeof self.cursorLimit() == 'number') opts.limit = self.cursorLimit();
- }
-
- var delimiter = self.s.ns.indexOf('.');
- var command = {
- 'count': self.s.ns.substr(delimiter+1), 'query': self.s.cmd.query
- }
- if(typeof opts.maxTimeMS == 'number') {
- command.maxTimeMS = opts.maxTimeMS;
- } else if(self.s.cmd && typeof self.s.cmd.maxTimeMS == 'number') {
- command.maxTimeMS = self.s.cmd.maxTimeMS;
- }
-
- if(opts.skip) command.skip = opts.skip;
- if(opts.limit) command.limit = opts.limit;
- if(self.s.options.hint) command.hint = self.s.options.hint;
-
- self.topology.command(f("%s.$cmd", self.s.ns.substr(0, delimiter))
- , command, function(err, result) {
- callback(err, result ? result.result.n : null)
- });
- }
- define.classMethod('count', {callback: true, promise:true});
- Cursor.prototype.close = function(callback) {
- this.s.state = Cursor.CLOSED;
-
- this.kill();
-
- this.emit('close');
-
- if(typeof callback == 'function') return handleCallback(callback, null, this);
-
- return new this.s.promiseLibrary(function(resolve) {
- resolve();
- });
- }
- define.classMethod('close', {callback: true, promise:true});
- Cursor.prototype.map = function(transform) {
- this.cursorState.transforms = { doc: transform };
- return this;
- }
- define.classMethod('map', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.isClosed = function() {
- return this.isDead();
- }
- define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
- Cursor.prototype.destroy = function(err) {
- if(err) this.emit('error', err);
- this.pause();
- this.close();
- }
- define.classMethod('destroy', {callback: false, promise:false});
- Cursor.prototype.stream = function(options) {
- this.s.streamOptions = options || {};
- return this;
- }
- define.classMethod('stream', {callback: false, promise:false, returns: [Cursor]});
- Cursor.prototype.explain = function(callback) {
- var self = this;
- this.s.cmd.explain = true;
-
- if(this.s.cmd.readConcern) {
- delete this.s.cmd['readConcern'];
- }
-
- if(typeof callback == 'function') return this._next(callback);
-
- return new this.s.promiseLibrary(function(resolve, reject) {
- self._next(function(err, r) {
- if(err) return reject(err);
- resolve(r);
- });
- });
- }
- define.classMethod('explain', {callback: true, promise:true});
- Cursor.prototype._read = function() {
- var self = this;
- if(self.s.state == Cursor.CLOSED || self.isDead()) {
- return self.push(null);
- }
-
- self.nextObject(function(err, result) {
- if(err) {
- if(self.listeners('error') && self.listeners('error').length > 0) {
- self.emit('error', err);
- }
- if(!self.isDead()) self.close();
-
- self.emit('end');
- return self.emit('finish');
- }
-
- if(typeof self.s.streamOptions.transform == 'function' && result != null) {
- return self.push(self.s.streamOptions.transform(result));
- }
-
- if(self.cursorState.transforms && typeof self.cursorState.transforms.doc == 'function' && result != null) {
- return self.push(self.cursorState.transforms.doc(result));
- }
-
- self.push(result);
- });
- }
- Object.defineProperty(Cursor.prototype, 'readPreference', {
- enumerable:true,
- get: function() {
- if (!this || !this.s) {
- return null;
- }
- return this.s.options.readPreference;
- }
- });
- Object.defineProperty(Cursor.prototype, 'namespace', {
- enumerable: true,
- get: function() {
- if (!this || !this.s) {
- return null;
- }
-
- var ns = this.s.ns || '';
- var firstDot = ns.indexOf('.');
- if (firstDot < 0) {
- return {
- database: this.s.ns,
- collection: ''
- };
- }
- return {
- database: ns.substr(0, firstDot),
- collection: ns.substr(firstDot + 1)
- };
- }
- });
- Cursor.INIT = 0;
- Cursor.OPEN = 1;
- Cursor.CLOSED = 2;
- Cursor.GET_MORE = 3;
- module.exports = Cursor;
|