summaryrefslogtreecommitdiff
path: root/jstests/indexu.js
blob: c7fa8ed3365067a269e92283d0b4e2b9da1fac04 (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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Test index key generation with duplicate values addressed by array index and
// object field.  SERVER-2902

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

var dupDoc = {a:[{'0':1}]}; // There are two 'a.0' fields in this doc.
var dupDoc2 = {a:[{'1':1},'c']};
var noDupDoc = {a:[{'1':1}]};

// Test that we can't index dupDoc.
t.save( dupDoc );
assert( !db.getLastError() );
t.ensureIndex( {'a.0':1} );
assert( db.getLastError() );

t.remove();
t.ensureIndex( {'a.0':1} );
assert( !db.getLastError() );
t.save( dupDoc );
assert( db.getLastError() );

// Test that we can't index dupDoc2.
t.drop();
t.save( dupDoc2 );
assert( !db.getLastError() );
t.ensureIndex( {'a.1':1} );
assert( db.getLastError() );

t.remove();
t.ensureIndex( {'a.1':1} );
assert( !db.getLastError() );
t.save( dupDoc2 );
assert( db.getLastError() );

// Test that we can index dupDoc with a different index.
t.drop();
t.ensureIndex( {'a.b':1} );
t.save( dupDoc );
assert( !db.getLastError() );

// Test number field starting with hyphen.
t.drop();
t.ensureIndex( {'a.-1':1} );
t.save( {a:[{'-1':1}]} );
assert( !db.getLastError() );

// Test number field starting with zero.
t.drop();
t.ensureIndex( {'a.00':1} );
t.save( {a:[{'00':1}]} );
assert( !db.getLastError() );

// Test multiple array indexes
t.drop();
t.ensureIndex( {'a.0':1,'a.1':1} );
t.save( {a:[{'1':1}]} );
assert( !db.getLastError() );
t.save( {a:[{'1':1},4]} );
assert( db.getLastError() );

// Test that we can index noDupDoc.
t.drop();
t.save( noDupDoc );
t.ensureIndex( {'a.0':1} );
assert( !db.getLastError() );
t.ensureIndex( {'a.1':1} );
assert( !db.getLastError() );

t.drop();
t.ensureIndex( {'a.0':1} );
t.ensureIndex( {'a.1':1} );
t.save( noDupDoc );
assert( !db.getLastError() );

// Test that we can query noDupDoc.
assert.eq( 1, t.find( {'a.1':1} ).hint( {'a.1':1} ).itcount() );
assert.eq( 1, t.find( {'a.1':1} ).hint( {$natural:1} ).itcount() );
assert.eq( 1, t.find( {'a.0':{'1':1}} ).hint( {'a.0':1} ).itcount() );
assert.eq( 1, t.find( {'a.0':{'1':1}} ).hint( {$natural:1} ).itcount() );

// Check multiple nested array fields.
t.drop();
t.save( {a:[[1]]} );
t.ensureIndex( {'a.0.0':1} );
assert( !db.getLastError() );
assert.eq( 1, t.find( {'a.0.0':1} ).hint( {$natural:1} ).itcount() );
assert.eq( 1, t.find( {'a.0.0':1} ).hint( {'a.0.0':1} ).itcount() );

// Check where there is a duplicate for a partially addressed field but not for a fully addressed field.
t.drop();
t.save( {a:[[1],{'0':1}]} );
t.ensureIndex( {'a.0.0':1} );
assert( db.getLastError() );

// Check where there is a duplicate for a fully addressed field.
t.drop();
t.save( {a:[[1],{'0':[1]}]} );
assert( !db.getLastError() );
t.ensureIndex( {'a.0.0':1} );
assert( db.getLastError() );

// Two ways of addressing parse to an array.
t.drop();
t.save( {a:[{'0':1}]} );
t.ensureIndex( {'a.0.0':1} );
assert( db.getLastError() );

// Test several key depths - with same arrays being found.
t.drop();
t.save( {a:[{'0':[{'0':1}]}]} );
t.ensureIndex( {'a.0.0.0.0.0.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a.0.0.0.0.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a.0.0.0.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a.0.0.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a.0.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a.0':1} );
assert( db.getLastError() );
t.ensureIndex( {'a':1} );
assert( !db.getLastError() );

// Two prefixes extract docs, but one terminates extraction before array.
t.drop();
t.save( {a:[{'0':{'c':[]}}]} );
t.ensureIndex( {'a.0.c':1} );
assert( db.getLastError() );

t.drop();
t.save( {a:[[{'b':1}]]} );
assert.eq( 1, t.find( {'a.0.b':1} ).itcount() );
t.ensureIndex( {'a.0.b':1} );
assert.eq( 1, t.find( {'a.0.b':1} ).itcount() );