Skip to content

Commit ea4b717

Browse files
committed
NODE-266 support listIndexes on 2.7 and higher
1 parent c5fea15 commit ea4b717

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

lib/db.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,8 @@ var Db = function(databaseName, topology, options) {
438438
if(err) return fallbackListCollections();
439439
// List of result documents that have been filtered
440440
var filtered_documents = result.collections.filter(function(document) {
441-
if(name && document.name != name) {
442-
return false;
443-
} else if(document.name.indexOf('$') != -1) {
444-
return false;
445-
}
446-
441+
if(name && document.name != name) return false;
442+
if(document.name.indexOf('$') != -1) return false;
447443
return true;
448444
});
449445

@@ -1127,23 +1123,11 @@ var Db = function(databaseName, topology, options) {
11271123

11281124
// If we specified full information
11291125
var full = options['full'] == null ? false : options['full'];
1130-
// Build selector for the indexes
1131-
var selector = name != null ? {ns: (databaseName + "." + name)} : {};
11321126

1133-
// Get read preference if we set one
1134-
readPreference = ReadPreference.PRIMARY;
1135-
1136-
// Iterate through all the fields of the index
1137-
var collection = this.collection(Db.SYSTEM_INDEX_COLLECTION);
1138-
// Perform the find for the collection
1139-
collection.find(selector).setReadPreference(readPreference).toArray(function(err, indexes) {
1140-
if(err != null) return handleCallback(callback, err, null);
1127+
// Process all the results from the index command and collection
1128+
var processResults = function(indexes) {
11411129
// Contains all the information
11421130
var info = {};
1143-
1144-
// if full defined just return all the indexes directly
1145-
if(full) return handleCallback(callback, null, indexes);
1146-
11471131
// Process all the indexes
11481132
for(var i = 0; i < indexes.length; i++) {
11491133
var index = indexes[i];
@@ -1154,8 +1138,36 @@ var Db = function(databaseName, topology, options) {
11541138
}
11551139
}
11561140

1141+
return info;
1142+
}
1143+
1144+
// Fallback to pre 2.8 getting the index information
1145+
var fallbackListIndexes = function() {
1146+
// Build selector for the indexes
1147+
var selector = name != null ? {ns: (databaseName + "." + name)} : {};
1148+
1149+
// Get read preference if we set one
1150+
readPreference = ReadPreference.PRIMARY;
1151+
1152+
// Iterate through all the fields of the index
1153+
var collection = self.collection(Db.SYSTEM_INDEX_COLLECTION);
1154+
// Perform the find for the collection
1155+
collection.find(selector).setReadPreference(readPreference).toArray(function(err, indexes) {
1156+
if(err != null) return handleCallback(callback, err, null);
1157+
// if full defined just return all the indexes directly
1158+
if(full) return handleCallback(callback, null, indexes);
1159+
// Return all the indexes
1160+
handleCallback(callback, null, processResults(indexes));
1161+
});
1162+
}
1163+
1164+
// Attempt to execute the listIndexes command
1165+
self.command({listIndexes: name}, function(err, result) {
1166+
if(err) return fallbackListIndexes();
1167+
// if full defined just return all the indexes directly
1168+
if(full) return handleCallback(callback, null, result.indexes);
11571169
// Return all the indexes
1158-
handleCallback(callback, null, info);
1170+
handleCallback(callback, null, processResults(result.indexes));
11591171
});
11601172
};
11611173

test/functional/scram_tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* rm -rf data; mkdir data; mongod --dbpath=./data --setParameter authenticationMechanisms=SCRAM-SHA-1 --auth
23
* @ignore
34
*/
45
exports['should correctly create a new user and authenticate using scram'] = {

0 commit comments

Comments
 (0)