diff --git a/lib/mongodb/admin.js b/lib/mongodb/admin.js index 89ca706c24a..fb129d8a9a8 100644 --- a/lib/mongodb/admin.js +++ b/lib/mongodb/admin.js @@ -13,6 +13,8 @@ var Collection = require('./collection').Collection, * @return {Function} Constructor for Admin type. */ function Admin(db) { + if(!(this instanceof Admin)) return new Admin(db); + this.db = db; }; diff --git a/lib/mongodb/bson/binary.js b/lib/mongodb/bson/binary.js index 350be6b792d..c5d256e9570 100644 --- a/lib/mongodb/bson/binary.js +++ b/lib/mongodb/bson/binary.js @@ -20,7 +20,9 @@ var bson = require('./bson'); * @param {Number} [subType] the option binary type. * @return {Grid} */ -function Binary(buffer, subType) { +function Binary(buffer, subType) { + if(!(this instanceof Binary)) return new Binary(buffer, subType); + this._bsontype = 'Binary'; if(buffer instanceof Number) { diff --git a/lib/mongodb/bson/code.js b/lib/mongodb/bson/code.js index b2675385506..69b56a3ff51 100644 --- a/lib/mongodb/bson/code.js +++ b/lib/mongodb/bson/code.js @@ -7,6 +7,8 @@ * @return {Code} */ function Code(code, scope) { + if(!(this instanceof Code)) return new Code(code, scope); + this._bsontype = 'Code'; this.code = code; this.scope = scope == null ? {} : scope; diff --git a/lib/mongodb/bson/db_ref.js b/lib/mongodb/bson/db_ref.js index d44abb02423..56b651029bd 100644 --- a/lib/mongodb/bson/db_ref.js +++ b/lib/mongodb/bson/db_ref.js @@ -8,6 +8,8 @@ * @return {DBRef} */ function DBRef(namespace, oid, db) { + if(!(this instanceof DBRef)) return new DBRef(namespace, oid, db); + this._bsontype = 'DBRef'; this.namespace = namespace; this.oid = oid; diff --git a/lib/mongodb/bson/double.js b/lib/mongodb/bson/double.js index d77bc718670..ae5146378f0 100644 --- a/lib/mongodb/bson/double.js +++ b/lib/mongodb/bson/double.js @@ -6,6 +6,8 @@ * @return {Double} */ function Double(value) { + if(!(this instanceof Double)) return new Double(value); + this._bsontype = 'Double'; this.value = value; } diff --git a/lib/mongodb/bson/long.js b/lib/mongodb/bson/long.js index a3c7e0763ab..f8f37a6b865 100644 --- a/lib/mongodb/bson/long.js +++ b/lib/mongodb/bson/long.js @@ -40,6 +40,8 @@ * @param {Number} high the high (signed) 32 bits of the Long. */ function Long(low, high) { + if(!(this instanceof Long)) return new Long(low, high); + this._bsontype = 'Long'; /** * @type {number} diff --git a/lib/mongodb/bson/max_key.js b/lib/mongodb/bson/max_key.js index 20b4586cdd0..0825408d0c9 100644 --- a/lib/mongodb/bson/max_key.js +++ b/lib/mongodb/bson/max_key.js @@ -5,6 +5,8 @@ * @return {MaxKey} */ function MaxKey() { + if(!(this instanceof MaxKey)) return new MaxKey(); + this._bsontype = 'MaxKey'; } diff --git a/lib/mongodb/bson/min_key.js b/lib/mongodb/bson/min_key.js index 265e563acb9..230c2e64a1d 100644 --- a/lib/mongodb/bson/min_key.js +++ b/lib/mongodb/bson/min_key.js @@ -5,6 +5,8 @@ * @return {MinKey} */ function MinKey() { + if(!(this instanceof MinKey)) return new MinKey(); + this._bsontype = 'MinKey'; } diff --git a/lib/mongodb/bson/objectid.js b/lib/mongodb/bson/objectid.js index a78b2637dd1..298da6c020b 100644 --- a/lib/mongodb/bson/objectid.js +++ b/lib/mongodb/bson/objectid.js @@ -25,6 +25,8 @@ var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); * @return {Object} instance of ObjectID. */ function ObjectID(id) { + if(!(this instanceof ObjectID)) return new ObjectID(id); + this._bsontype = 'ObjectID'; var self = this; diff --git a/lib/mongodb/bson/symbol.js b/lib/mongodb/bson/symbol.js index 67174157617..f015dfdb2c2 100644 --- a/lib/mongodb/bson/symbol.js +++ b/lib/mongodb/bson/symbol.js @@ -6,6 +6,8 @@ * @return {Symbol} */ function Symbol(value) { + if(!(this instanceof Symbol)) return new Symbol(value); + this._bsontype = 'Symbol'; this.value = value; } diff --git a/lib/mongodb/bson/timestamp.js b/lib/mongodb/bson/timestamp.js index db6003c03e1..0432ba48142 100644 --- a/lib/mongodb/bson/timestamp.js +++ b/lib/mongodb/bson/timestamp.js @@ -40,6 +40,8 @@ * @param {Number} high the high (signed) 32 bits of the Timestamp. */ function Timestamp(low, high) { + if(!(this instanceof Timestamp)) return new Timestamp(low, high); + this._bsontype = 'Timestamp'; /** * @type {number} diff --git a/lib/mongodb/collection.js b/lib/mongodb/collection.js index 60b86b28943..139bd683000 100644 --- a/lib/mongodb/collection.js +++ b/lib/mongodb/collection.js @@ -44,6 +44,8 @@ var toString = Object.prototype.toString; * @return {Object} a collection instance. */ function Collection (db, collectionName, pkFactory, options) { + if(!(this instanceof Collection)) return new Collection(db, collectionName, pkFactory, options); + checkCollectionName(collectionName); this.db = db; diff --git a/lib/mongodb/connection/repl_set_servers.js b/lib/mongodb/connection/repl_set_servers.js index 28662821e94..12897b94e9d 100644 --- a/lib/mongodb/connection/repl_set_servers.js +++ b/lib/mongodb/connection/repl_set_servers.js @@ -29,6 +29,8 @@ const STATE_ROLLBACK = 9; * */ var ReplSetServers = exports.ReplSetServers = function(servers, options) { + if(!(this instanceof ReplSetServers)) return new ReplSetServers(server, options); + var self = this; // Contains the master server entry this.options = options == null ? {} : options; diff --git a/lib/mongodb/connection/server.js b/lib/mongodb/connection/server.js index 974750d00f5..4b4b1922245 100644 --- a/lib/mongodb/connection/server.js +++ b/lib/mongodb/connection/server.js @@ -7,6 +7,8 @@ var Connection = require('./connection').Connection, inherits = require('util').inherits; var Server = exports.Server = function(host, port, options) { + if(!(this instanceof Server)) return new Server(host, port, options); + var self = this; this.host = host; this.port = port; diff --git a/lib/mongodb/cursorstream.js b/lib/mongodb/cursorstream.js index cb3d2ff60d7..fd2ff657fc8 100644 --- a/lib/mongodb/cursorstream.js +++ b/lib/mongodb/cursorstream.js @@ -18,6 +18,8 @@ var Stream = require('stream').Stream; * @return {Stream} */ function CursorStream(cursor) { + if(!(this instanceof CursorStream)) return new CursorStream(cursor); + Stream.call(this); this.readable = true; diff --git a/lib/mongodb/db.js b/lib/mongodb/db.js index 68afe96ce2b..b8e9b7efeb2 100644 --- a/lib/mongodb/db.js +++ b/lib/mongodb/db.js @@ -58,6 +58,9 @@ inherits(CallbackStore, EventEmitter); * @param {Object} [options] additional options for the collection. */ function Db(databaseName, serverConfig, options) { + + if(!(this instanceof Db)) return new Db(databaseName, serverConfig, options); + EventEmitter.call(this); this.databaseName = databaseName; this.serverConfig = serverConfig; diff --git a/lib/mongodb/gridfs/chunk.js b/lib/mongodb/gridfs/chunk.js index bf48e19a162..365ee011dbc 100644 --- a/lib/mongodb/gridfs/chunk.js +++ b/lib/mongodb/gridfs/chunk.js @@ -21,6 +21,8 @@ var BinaryParser = require('../bson/binary_parser').BinaryParser, * @see Chunk#buildMongoObject */ var Chunk = exports.Chunk = function(file, mongoObject) { + if(!(this instanceof Chunk)) return new Chunk(file, mongoObject); + this.file = file; var self = this; var mongoObjectFinal = mongoObject == null ? {} : mongoObject; diff --git a/lib/mongodb/gridfs/grid.js b/lib/mongodb/gridfs/grid.js index f380392cedf..f22df9b77ff 100644 --- a/lib/mongodb/gridfs/grid.js +++ b/lib/mongodb/gridfs/grid.js @@ -10,6 +10,9 @@ var GridStore = require('./gridstore').GridStore, * @return {Grid} */ function Grid(db, fsName) { + + if(!(this instanceof Grid)) return new Grid(db, fsName); + this.db = db; this.fsName = fsName == null ? GridStore.DEFAULT_ROOT_COLLECTION : fsName; } diff --git a/lib/mongodb/gridfs/gridstore.js b/lib/mongodb/gridfs/gridstore.js index 6a83e79a072..985536fd57b 100644 --- a/lib/mongodb/gridfs/gridstore.js +++ b/lib/mongodb/gridfs/gridstore.js @@ -42,6 +42,8 @@ var REFERENCE_BY_FILENAME = 0, * @return {GridStore} */ function GridStore(db, fileIdObject, mode, options) { + if(!(this instanceof GridStore)) return new GridStore(db, fileIdObject, mode, options); + var self = this; this.db = db;