summaryrefslogtreecommitdiff
path: root/jstests/mr_index3.js
blob: 0607cc8aa84b09b496e57c420217dc2319072ac6 (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
40
41
42
43
44
45
46
47
48
49
50

t = db.mr_index3
t.drop(); 

t.insert( { _id : 1, name : 'name1', tags : ['dog', 'cat'] } ); 
t.insert( { _id : 2, name : 'name2', tags : ['cat'] } ); 
t.insert( { _id : 3, name : 'name3', tags : ['mouse', 'cat', 'dog'] } ); 
t.insert( { _id : 4, name : 'name4', tags : [] } ); 

m = function(){ 
    for ( var i=0; i<this.tags.length; i++ )
        emit( this.tags[i] , 1 )
}; 

r = function( key , values ){ 
    return Array.sum( values );
}; 

a1 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r , out : { inline : true } } ).results
a2 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r, query: {name : 'name1'} , out : { inline : true }}).results
a3 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r, query: {name : {$gt:'name'} } , out : { inline : true }}).results

assert.eq( [
    {
	"_id" : "cat",
	"value" : 3
    },
    {
	"_id" : "dog",
	"value" : 2
    },
    {
	"_id" : "mouse",
	"value" : 1
    }
] , a1 , "A1" );
assert.eq( [ { "_id" : "cat", "value" : 1 }, { "_id" : "dog", "value" : 1 } ] , a2 , "A2" )
assert.eq( a1 , a3 , "A3" )

t.ensureIndex({name:1, tags:1}); 

b1 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r , out : { inline : true } } ).results
b2 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r, query: {name : 'name1'} , out : { inline : true }}).results
b3 = db.runCommand({ mapreduce : 'mr_index3', map : m, reduce : r, query: {name : {$gt:'name'} } , out : { inline : true }}).results

assert.eq( a1 , b1 , "AB1" )
assert.eq( a2 , b2 , "AB2" )
assert.eq( a3 , b3 , "AB3" )