Skip to content
Browse files

fixed; index declaration using objects/arrays from other contexts

closes #754
  • Loading branch information...
1 parent 786c188 commit 6d18c1945eebe6d28ec6ff02481a7ee11881622a @aheckmann committed
Showing with 22 additions and 10 deletions.
  1. +8 −7 lib/mongodb/commands/db_command.js
  2. +1 −1 test/contexts/other.js
  3. +13 −2 test/index_test.js
View
15 lib/mongodb/commands/db_command.js
@@ -1,6 +1,7 @@
var QueryCommand = require('./query_command').QueryCommand,
InsertCommand = require('./insert_command').InsertCommand,
inherits = require('util').inherits,
+ utils = require('../utils'),
crypto = require('crypto');
/**
@@ -127,18 +128,18 @@ DbCommand.createCreateIndexCommand = function(db, collectionName, fieldOrSpec, o
var keys;
// Get all the fields accordingly
- if (fieldOrSpec.constructor === String) { // 'type'
+ if ('string' == typeof fieldOrSpec) { // 'type'
indexes.push(fieldOrSpec + '_' + 1);
fieldHash[fieldOrSpec] = 1;
- } else if (fieldOrSpec.constructor === Array) { // [{location:'2d'}, ...]
+ } else if (utils.isArray(fieldOrSpec)) { // [{location:'2d'}, ...]
fieldOrSpec.forEach(function(f) {
- if (f.constructor === String) { // [{location:'2d'}, 'type']
+ if ('string' == typeof f) { // [{location:'2d'}, 'type']
indexes.push(f + '_' + 1);
fieldHash[f] = 1;
- } else if (f.constructor === Array) { // [['location', '2d'],['type', 1]]
+ } else if (utils.isArray(f)) { // [['location', '2d'],['type', 1]]
indexes.push(f[0] + '_' + (f[1] || 1));
fieldHash[f[0]] = f[1] || 1;
- } else if (f.constructor === Object) { // [{location:'2d'}, {type:1}]
+ } else if (utils.isObject(f)) { // [{location:'2d'}, {type:1}]
keys = Object.keys(f);
keys.forEach(function(k) {
indexes.push(k + '_' + f[k]);
@@ -148,7 +149,7 @@ DbCommand.createCreateIndexCommand = function(db, collectionName, fieldOrSpec, o
// undefined
}
});
- } else if (fieldOrSpec.constructor === Object) { // {location:'2d', type:1}
+ } else if (utils.isObject(fieldOrSpec)) { // {location:'2d', type:1}
keys = Object.keys(fieldOrSpec);
keys.forEach(function(key) {
indexes.push(key + '_' + fieldOrSpec[key]);
@@ -211,4 +212,4 @@ DbCommand.createAdminDbCommandSlaveOk = function(db, command_hash) {
DbCommand.createDbSlaveOkCommand = function(db, command_hash, options) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 0, -1, command_hash, null, options);
-};
+};
View
2 test/contexts/other.js
@@ -1,7 +1,7 @@
var shared = require('./shared');
shared.object = { name: 1, age: -1 };
-shared.array = [1,2]
+shared.array = ['name']
shared.string = 'woot'
shared.string2 = String('woot')
shared.number = 3
View
15 test/index_test.js
@@ -9,7 +9,8 @@ var testCase = require('nodeunit').testCase,
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
- Server = mongodb.Server;
+ Server = mongodb.Server,
+ shared = require('./contexts')
var MONGODB = 'integration_tests';
var native_parser = (process.env['TEST_NATIVE'] != null);
@@ -792,6 +793,16 @@ exports['Should correctly create an index with overriden name'] = function(test)
});
}
+exports['should handle index declarations using objects from other contexts'] = function (test) {
+ client.collection('indexcontext').ensureIndex(shared.object, { safe: true, background: true }, function (err) {
+ test.equal(null, err);
+ client.collection('indexcontext').ensureIndex(shared.array, { safe: true, background: true }, function (err) {
+ test.equal(null, err);
+ test.done();
+ })
+ })
+}
+
/**
* Retrieve the server information for the current
* instance of the db client
@@ -810,4 +821,4 @@ exports.noGlobalsLeaked = function(test) {
*
* @ignore
*/
-var numberOfTestsRun = Object.keys(this).length - 2;
+var numberOfTestsRun = Object.keys(this).length - 2;

0 comments on commit 6d18c19

Please sign in to comment.
Something went wrong with that request. Please try again.