summaryrefslogtreecommitdiff
path: root/jstests/core/regex_options.js
blob: 120a3a9c9a59a03b9c1e05714fd2b06d70d4b9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Test regex options in a find context.
 * @tags: [requires_fcv_51]
 */
(function() {
'use strict';
const coll = db.jstests_regex_options;

coll.drop();
assert.commandWorked(coll.insert({a: 'foo'}));
assert.eq(1, coll.count({a: {$regex: /O/i}}));
assert.eq(1, coll.count({a: /O/i}));
assert.eq(1, coll.count({a: {$regex: 'O', $options: 'i'}}));

// Test with various invalid options and make sure they aren't ignored.
// After SERVER-26991 the u flag should no longer be considered invalid.
coll.drop();
assert.commandWorked(coll.insert({txt: 'hello_test'}));

// Test valid/invalid options.
assert.eq(1, coll.count({txt: {$regex: '^hello.*'}}));
assert.eq(1, coll.count({txt: {$regex: '^hello.*', $options: 'u'}}));
assert.commandFailedWithCode(
    assert.throws(() => coll.count({txt: {$regex: '^hello.*', $options: 'g'}})), 51108);

// Test using regex object.
assert.eq(1, coll.count({txt: {$regex: /^hello.*/}}));
assert.eq(1, coll.count({txt: {$regex: /^hello.*/u}}));
assert.commandFailedWithCode(assert.throws(() => coll.count({txt: {$regex: /^hello.*/g}})), 51108);

// Test in projection.
assert.eq(1, coll.find({}, {p: {$regexFind: {input: '$txt', regex: /^hello.*/}}}).toArray().length);
assert.eq(1,
          coll.find({}, {p: {$regexFind: {input: '$txt', regex: /^hello.*/u}}}).toArray().length);
assert.commandFailedWithCode(
    assert.throws(() => coll.find({}, {p: {$regexFind: {input: '$txt', regex: /^hello.*/g}}})
                            .itcount()),
                 51108);
})();