Skip to content
Browse files

fixed; subdoc.index() now supported

  • Loading branch information...
1 parent d96fbc6 commit fd9b333404ed37d61cc81b9d4abac0bf4203cf92 @aheckmann aheckmann committed
Showing with 34 additions and 9 deletions.
  1. +34 −9 lib/mongoose/schema.js
View
43 lib/mongoose/schema.js
@@ -365,17 +365,17 @@ Schema.prototype.__defineGetter__('indexes', function () {
, indexes = []
, seenSchemas = [];
- function collectIndexes (paths, prefix) {
+ function collectIndexes (schema, prefix) {
+ if (~seenSchemas.indexOf(schema)) return;
+ seenSchemas.push(schema);
+
+ var paths = schema.paths;
prefix = prefix || '';
- for (var i in paths){
+ for (var i in paths) {
if (paths[i]) {
if (paths[i] instanceof Types.DocumentArray) {
- // avoid recursion
- if (!(~seenSchemas.indexOf(paths[i].schema))) {
- seenSchemas.push(paths[i].schema);
- collectIndexes(paths[i].schema.paths, i + '.');
- }
+ collectIndexes(paths[i].schema, i + '.');
} else {
index = paths[i]._index;
@@ -387,11 +387,36 @@ Schema.prototype.__defineGetter__('indexes', function () {
}
}
}
+
+ if (!prefix) {
+ return indexes = indexes.concat(schema._indexes);
+ }
+
+ // Check for indexes added to subdocs using Schema.index().
+ // These indexes need their paths prefixed properly.
+ // schema._indexes = [ [indexObj, options], [indexObj, options] ..]
+ var subindexes = schema._indexes;
+ var len = subindexes.length;
+
+ for (i = 0; i < len; ++i) {
+ var indexObj = subindexes[i][0]
+ , keys = Object.keys(indexObj)
+ , klen = keys.length
+ , newindex = {}
+
+ // use forward iteration, order matters
+ for (var j = 0; j < klen; ++j) {
+ var key = keys[j];
+ newindex[prefix + key] = indexObj[key];
+ }
+
+ indexes.push([newindex, subindexes[i][1]]);
+ }
}
- collectIndexes(this.paths);
+ collectIndexes(this);
- return indexes.concat(this._indexes);
+ return indexes;
});
/**

0 comments on commit fd9b333

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