File: and.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (86 lines) | stat: -rw-r--r-- 3,206 bytes parent folder | download | duplicates (4)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Some tests for $and SERVER-1089

t = db.jstests_and;
t.drop();

t.save( {a:[1,2]} );
t.save( {a:'foo'} );

function check() {
    // $and must be an array
    assert.throws( function() { t.find( {$and:4} ).toArray() } );
    // $and array must not be empty
    assert.throws( function() { t.find( {$and:[]} ).toArray() } );
    // $and elements must be objects
    assert.throws( function() { t.find( {$and:[4]} ).toArray() } );
    
    // Check equality matching
    assert.eq( 1, t.count( {$and:[{a:1}]} ) );
    assert.eq( 1, t.count( {$and:[{a:1},{a:2}]} ) );
    assert.eq( 0, t.count( {$and:[{a:1},{a:3}]} ) );
    assert.eq( 0, t.count( {$and:[{a:1},{a:2},{a:3}]} ) );
    assert.eq( 1, t.count( {$and:[{a:'foo'}]} ) );
    assert.eq( 0, t.count( {$and:[{a:'foo'},{a:'g'}]} ) );
    
    // Check $and with other fields
    assert.eq( 1, t.count( {a:2,$and:[{a:1}]} ) );
    assert.eq( 0, t.count( {a:0,$and:[{a:1}]} ) );
    assert.eq( 0, t.count( {a:2,$and:[{a:0}]} ) );
    assert.eq( 1, t.count( {a:1,$and:[{a:1}]} ) );
    
    // Check recursive $and
    assert.eq( 1, t.count( {a:2,$and:[{$and:[{a:1}]}]} ) );
    assert.eq( 0, t.count( {a:0,$and:[{$and:[{a:1}]}]} ) );
    assert.eq( 0, t.count( {a:2,$and:[{$and:[{a:0}]}]} ) );
    assert.eq( 1, t.count( {a:1,$and:[{$and:[{a:1}]}]} ) );    

    assert.eq( 1, t.count( {$and:[{a:2},{$and:[{a:1}]}]} ) );
    assert.eq( 0, t.count( {$and:[{a:0},{$and:[{a:1}]}]} ) );
    assert.eq( 0, t.count( {$and:[{a:2},{$and:[{a:0}]}]} ) );
    assert.eq( 1, t.count( {$and:[{a:1},{$and:[{a:1}]}]} ) );    
    
    // Some of these cases were more important with an alternative $and syntax
    // that was rejected, but they're still valid checks.
    
    // Check simple regex
    assert.eq( 1, t.count( {$and:[{a:/foo/}]} ) );
    // Check multiple regexes
    assert.eq( 1, t.count( {$and:[{a:/foo/},{a:/^f/},{a:/o/}]} ) );
    assert.eq( 0, t.count( {$and:[{a:/foo/},{a:/^g/}]} ) );
    assert.eq( 1, t.count( {$and:[{a:/^f/},{a:'foo'}]} ) );
    // Check regex flags
    assert.eq( 0, t.count( {$and:[{a:/^F/},{a:'foo'}]} ) );
    assert.eq( 1, t.count( {$and:[{a:/^F/i},{a:'foo'}]} ) );
    
    
    
    // Check operator
    assert.eq( 1, t.count( {$and:[{a:{$gt:0}}]} ) );
    
    // Check where
    assert.eq( 1, t.count( {a:'foo',$where:'this.a=="foo"'} ) );
    assert.eq( 1, t.count( {$and:[{a:'foo'}],$where:'this.a=="foo"'} ) );
    assert.eq( 1, t.count( {$and:[{a:'foo'}],$where:'this.a=="foo"'} ) );
    
    // Nested where ok
    assert.eq( 1, t.count({$and:[{$where:'this.a=="foo"'}]}) );
    assert.eq( 1, t.count({$and:[{a:'foo'},{$where:'this.a=="foo"'}]}) );
    assert.eq( 1, t.count({$and:[{$where:'this.a=="foo"'}],$where:'this.a=="foo"'}) );
}

check();
t.ensureIndex( {a:1} );
check();
var e = t.find( {$and:[{a:1}]} ).explain();
assert.eq( 'BtreeCursor a_1', e.cursor );
assert.eq( [[1,1]], e.indexBounds.a );

function checkBounds( query ) {
    var e = t.find( query ).explain();
    assert.eq( 1, e.n );
    assert.eq( [[1,1]], e.indexBounds.a );
}

// Since this is a multikey index, we get the bounds from the first constraint scanned.
checkBounds( {a:1,$and:[{a:2}]} );
checkBounds( {$and:[{a:1},{a:2}]} );