Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 6a6ac60

Browse files
authored
Wildcard indexes (#61)
1 parent 43c8784 commit 6a6ac60

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

lib/model.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,22 @@ var IndexModel = Model.extend({
8989
return !!this.extra.textIndexVersion;
9090
}
9191
},
92+
wildcard: {
93+
deps: ['extra', 'key'],
94+
fn: function() {
95+
return _.keys(this.key).some(function(k) {
96+
return k === '$**' || k.indexOf('.$**') > -1;
97+
});
98+
}
99+
},
92100
collation: {
93101
deps: ['extra'],
94102
fn: function() {
95103
return !!this.extra.collation;
96104
}
97105
},
98106
type: {
99-
deps: ['geo', 'hashed', 'text'],
107+
deps: ['geo', 'hashed', 'text', 'wildcard'],
100108
fn: function() {
101109
if (this.geo) {
102110
return 'geospatial';
@@ -107,6 +115,9 @@ var IndexModel = Model.extend({
107115
if (this.text) {
108116
return 'text';
109117
}
118+
if (this.wildcard) {
119+
return 'wildcard';
120+
}
110121
return 'regular';
111122
}
112123
},

lib/warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var WARNINGS = {
2020
*
2121
* @type {string[]}
2222
*/
23-
var VALID_INDEX_TYPE_VALUES = [1, -1, '2dsphere', '2d', 'geoHaystack', 'text', 'hashed'];
23+
var VALID_INDEX_TYPE_VALUES = [1, -1, '2dsphere', '2d', 'geoHaystack', 'text', 'hashed', 'wildcard'];
2424

2525
var WarningModel = Model.extend({
2626
idAttribute: 'code',

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixture.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,33 @@ module.exports = [
9090
},
9191
'name': 'big-index',
9292
'ns': 'mongodb.fanclub'
93+
},
94+
{
95+
'v': 1,
96+
'key': {
97+
'address.$**': 1
98+
},
99+
'name': 'wildcard_single_subtree',
100+
'ns': 'mongodb.fanclub'
101+
},
102+
{
103+
'v': 1,
104+
'key': {
105+
'$**': 1
106+
},
107+
'name': 'wildcard_multi_subtree',
108+
'wildcardProjection': {
109+
'borough': 1,
110+
'cuisine': 1
111+
},
112+
'ns': 'mongodb.fanclub'
113+
},
114+
{
115+
'v': 1,
116+
'key': {
117+
'name$**': 1
118+
},
119+
'name': 'not_wildcard',
120+
'ns': 'mongodb.fanclub'
93121
}
94122
];

test/index.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('mongodb-index-model', function() {
1515

1616
context('IndexModel', function() {
1717
it('should have all indexes in the collection', function() {
18-
assert.equal(indexes.length, 9);
18+
assert.equal(indexes.length, 12);
1919
});
2020

2121
it('should get the names right', function() {
@@ -27,8 +27,12 @@ describe('mongodb-index-model', function() {
2727
'email_1_favorite_features_1',
2828
'last_login_-1',
2929
'last_position_2dsphere',
30+
'not_wildcard',
3031
'seniors',
31-
'seniors-inverse']);
32+
'seniors-inverse',
33+
'wildcard_multi_subtree',
34+
'wildcard_single_subtree'
35+
]);
3236
});
3337

3438
it('should have the correct namespace', function() {
@@ -51,7 +55,7 @@ describe('mongodb-index-model', function() {
5155
assert.equal(index.hashed, false);
5256
assert.equal(index.geo, false);
5357
assert.equal(index.compound, false);
54-
assert.equal(index.geo, false);
58+
assert.equal(index.wildcard, false);
5559
assert.equal(index.partial, false);
5660
assert.equal(index.collation, false);
5761
});
@@ -60,6 +64,18 @@ describe('mongodb-index-model', function() {
6064
assert.equal(indexes.get('last_position_2dsphere', 'name').geo, true);
6165
});
6266

67+
it('should recognize single wildcard indexes', function() {
68+
assert.equal(indexes.get('wildcard_single_subtree', 'name').wildcard, true);
69+
});
70+
71+
it('should recognize multi subtree wildcard indexes', function() {
72+
assert.equal(indexes.get('wildcard_multi_subtree', 'name').wildcard, true);
73+
});
74+
75+
it('should not recognize indexes with $** as wildcard', function() {
76+
assert.equal(indexes.get('not_wildcard', 'name').wildcard, false);
77+
});
78+
6379
it('should recognize compound indexes', function() {
6480
assert.equal(indexes.get('email_1_favorite_features_1', 'name').compound, true);
6581
});
@@ -107,6 +123,7 @@ describe('mongodb-index-model', function() {
107123
assert.ok('ttl' in index);
108124
assert.ok('hashed' in index);
109125
assert.ok('geo' in index);
126+
assert.ok('wildcard' in index);
110127
assert.ok('compound' in index);
111128
assert.ok('partial' in index);
112129
assert.ok('text' in index);

0 commit comments

Comments
 (0)