Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
throw new MongoError('collection name must be a String');
}
if (!collectionName || collectionName.indexOf('..') !== -1) {
throw new MongoError('collection names cannot be empty');
}
if (
collectionName.indexOf('$') !== -1 &&
collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null
) {
throw new MongoError("collection names must not contain '$'");
}
if (collectionName.match(/^\.|\.$/) != null) {
throw new MongoError("collection names must not start or end with '.'");
}
// Validate that we are not passing 0x00 in the colletion name
if (collectionName.indexOf('\x00') !== -1) {
throw new MongoError('collection names cannot contain a null character');
}
};
function connectCallback(err, db) {
if(err && err.message == 'no mongos proxies found in seed list') {
if(logger.isWarn()) {
logger.warn(f('seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name'));
}
// Return a more specific error message for MongoClient.connect
return callback(new MongoError('seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name'));
}
// Return the error and db instance
callback(err, db);
}
// Pass to the raw bulk
bulk.raw(operations[i]);
}
} catch(err) {
return callback(err, null);
}
// Final options for write concern
var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
var writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
var capabilities = self.s.topology.capabilities();
// Did the user pass in a collation, check if our write server supports it
if(collation && capabilities && !capabilities.commandsTakeCollation) {
return callback(new MongoError(f('server/primary/mongos does not support collation')));
}
// Execute the bulk
bulk.execute(writeCon, function(err, r) {
// We have connection level error
if(!r && err) return callback(err, null);
// We have single error
if(r && r.hasWriteErrors() && r.getWriteErrorCount() == 1) {
return callback(toError(r.getWriteErrorAt(0)), r);
}
r.insertedCount = r.nInserted;
r.matchedCount = r.nMatched;
r.modifiedCount = r.nModified || 0;
r.deletedCount = r.nRemoved;
r.upsertedCount = r.getUpsertedIds().length;
}
// Remove any write concern operations
var removeKeys = ['w', 'wtimeout', 'j', 'fsync', 'readPreference'];
for(var i = 0; i < removeKeys.length; i++) {
delete indexes[0][removeKeys[i]];
}
}
// Get capabilities
var capabilities = self.s.topology.capabilities();
// Did the user pass in a collation, check if our write server supports it
if(indexes[0].collation && capabilities && !capabilities.commandsTakeCollation) {
// Create a new error
var error = new MongoError(f('server/primary/mongos does not support collation'));
error.code = 67;
// Return the error
return callback(error);
}
// Create command, apply write concern to command
var cmd = writeConcern({createIndexes: name, indexes: indexes}, self, options);
// Decorate command with writeConcern if supported
decorateWithWriteConcern(cmd, self, options);
// ReadPreference primary
options.readPreference = ReadPreference.PRIMARY;
// Build the command
self.command(cmd, options, function(err, result) {
if (callback) callback(null, collection);
return collection;
} catch (err) {
if (err instanceof MongoError && callback) return callback(err);
throw err;
}
}
// Strict mode
if (typeof callback !== 'function') {
throw toError(`A callback is required in strict mode. While getting collection ${name}`);
}
// Did the user destroy the topology
if (this.serverConfig && this.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
const listCollectionOptions = Object.assign({}, options, { nameOnly: true });
// Strict mode
this.listCollections({ name: name }, listCollectionOptions).toArray((err, collections) => {
if (err != null) return handleCallback(callback, err, null);
if (collections.length === 0)
return handleCallback(
callback,
toError(`Collection ${name} does not exist. Currently in strict mode.`),
null
);
try {
return handleCallback(
Collection.prototype.listIndexes = function(options) {
options = options || {};
// Clone the options
options = shallowClone(options);
// Set the CommandCursor constructor
options.cursorFactory = CommandCursor;
// Set the promiseLibrary
options.promiseLibrary = this.s.promiseLibrary;
if(!this.s.topology.capabilities()) {
throw new MongoError('cannot connect to server');
}
// We have a list collections command
if(this.s.topology.capabilities().hasListIndexesCommand) {
// Cursor options
var cursor = options.batchSize ? {batchSize: options.batchSize} : {}
// Build the command
var command = { listIndexes: this.s.name, cursor: cursor };
// Execute the cursor
var cursor = this.s.topology.cursor(f('%s.$cmd', this.s.dbName), command, options);
// Do we have a readPreference, apply it
if(options.readPreference) cursor.setReadPreference(options.readPreference);
// Return the cursor
return cursor;
}
var checkCollectionName = function checkCollectionName (collectionName) {
if('string' !== typeof collectionName) {
throw new MongoError("collection name must be a String");
}
if(!collectionName || collectionName.indexOf('..') != -1) {
throw new MongoError("collection names cannot be empty");
}
if(collectionName.indexOf('$') != -1 &&
collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) {
throw new MongoError("collection names must not contain '$'");
}
if(collectionName.match(/^\.|\.$/) != null) {
throw new MongoError("collection names must not start or end with '.'");
}
// Validate that we are not passing 0x00 in the collection name
if(!!~collectionName.indexOf("\x00")) {
throw new MongoError("collection names cannot contain a null character");
}
};
Cursor.prototype.toArray = function(callback) {
var self = this;
if(!callback) throw new MongoError('callback is mandatory');
if(self.s.options.tailable) return handleCallback(callback, new MongoError("Tailable cursor cannot be converted to array"), null);
var items = [];
// Reset cursor
this.rewind();
self.s.state = Cursor.INIT;
// Fetch all the documents
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);
}
// Add doc to items
function validOptions(options) {
var _validOptions = validOptionNames.concat(legacyOptionNames);
for(var name in options) {
if(ignoreOptionNames.indexOf(name) != -1) {
continue;
}
if(_validOptions.indexOf(name) == -1 && options.validateOptions) {
return new MongoError(f('option %s is not supported', name));
} else if(_validOptions.indexOf(name) == -1) {
console.warn(f('the options [%s] is not supported', name));
}
if(legacyOptionNames.indexOf(name) != -1) {
console.warn(f('the server/replset/mongos options are deprecated, '
+ 'all their options are supported at the top level of the options object [%s]', validOptionNames));
}
}
}
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;
}));
}