summaryrefslogtreecommitdiff
path: root/jstests/update_arraymatch8.js
blob: 1e8ce377862e5513024ba1a148c677af0e52c029 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Checking for positional array updates with either .$ or .0 at the end
// SERVER-7511

// array.$.name
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'name': 'old'}]} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {'array.name': 'old'}, {$set: {'array.$.name': 'new'}} );
assert( t.findOne({'array.name': 'new'}) );
assert( !t.findOne({'array.name': 'old'}) );

// array.$   (failed in 2.2.2)
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'name': 'old'}]} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {'array.name': 'old'}, {$set: {'array.$': {'name':'new'}}} );
assert( t.findOne({'array.name': 'new'}) );
assert( !t.findOne({'array.name': 'old'}) );

// array.0.name
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'name': 'old'}]} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {'array.name': 'old'}, {$set: {'array.0.name': 'new'}} );
assert( t.findOne({'array.name': 'new'}) );
assert( !t.findOne({'array.name': 'old'}) );

// array.0   (failed in 2.2.2)
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'name': 'old'}]} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {'array.name': 'old'}, {$set: {'array.0': {'name':'new'}}} );
assert( t.findOne({'array.name': 'new'}) );
assert( !t.findOne({'array.name': 'old'}) );

// // array.12.name
t = db.jstests_update_arraymatch8;
t.drop();
arr = new Array();
for (var i=0; i<20; i++) {
    arr.push({'name': 'old'});
}
t.ensureIndex( {'array.name': 1} );
t.insert( {_id:0, 'array': arr} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {_id:0}, {$set: {'array.12.name': 'new'}} );
// note: both documents now have to be in the array
assert( t.findOne({'array.name': 'new'}) );
assert( t.findOne({'array.name': 'old'}) );

// array.12   (failed in 2.2.2)
t = db.jstests_update_arraymatch8;
t.drop();
arr = new Array();
for (var i=0; i<20; i++) {
    arr.push({'name': 'old'});
}
t.ensureIndex( {'array.name': 1} );
t.insert( {_id:0, 'array': arr} );
assert( t.findOne({'array.name': 'old'}) );
t.update( {_id:0}, {$set: {'array.12': {'name':'new'}}} );
// note: both documents now have to be in the array
assert( t.findOne({'array.name': 'new'}) );
assert( t.findOne({'array.name': 'old'}) );

// array.$.123a.name
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.123a.name': 1} );
t.insert( {'array': [{'123a':{'name': 'old'}}]} );
assert( t.findOne({'array.123a.name': 'old'}) );
t.update( {'array.123a.name': 'old'}, {$set: {'array.$.123a.name': 'new'}} );
assert( t.findOne({'array.123a.name': 'new'}) );
assert( !t.findOne({'array.123a.name': 'old'}) );

// array.$.123a
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'123a':{'name': 'old'}}]} );
assert( t.findOne({'array.123a.name': 'old'}) );
t.update( {'array.123a.name': 'old'}, {$set: {'array.$.123a': {'name': 'new'}}} );
assert( t.findOne({'array.123a.name': 'new'}) );
assert( !t.findOne({'array.123a.name': 'old'}) );

// array.0.123a.name
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.123a.name': 1} );
t.insert( {'array': [{'123a':{'name': 'old'}}]} );
assert( t.findOne({'array.123a.name': 'old'}) );
t.update( {'array.123a.name': 'old'}, {$set: {'array.0.123a.name': 'new'}} );
assert( t.findOne({'array.123a.name': 'new'}) );
assert( !t.findOne({'array.123a.name': 'old'}) );

// array.0.123a
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'array.name': 1} );
t.insert( {'array': [{'123a':{'name': 'old'}}]} );
assert( t.findOne({'array.123a.name': 'old'}) );
t.update( {'array.123a.name': 'old'}, {$set: {'array.0.123a': {'name': 'new'}}} );
assert( t.findOne({'array.123a.name': 'new'}) );
assert( !t.findOne({'array.123a.name': 'old'}) );

// a.0.b
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'a.0.b': 1} );
t.insert( {'a': [ [ { b:'old' } ] ] } );
assert( t.findOne({'a.0.0.b': 'old'}) );
assert( t.findOne({'a.0.b': 'old'}) );
t.update( {}, {$set: {'a.0.0.b': 'new'}} );
assert( t.findOne({'a.0.b': 'new'}) );
assert( !t.findOne({'a.0.b': 'old'}) );

// a.0.b.c
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'a.0.b.c': 1} );
t.insert( {'a': [ { b:[ { c:'old' } ] } ] } );
assert( t.findOne({'a.0.b.0.c': 'old'}) );
assert( t.findOne({'a.b.0.c': 'old'}) );
assert( t.findOne({'a.0.b.c': 'old'}) );
assert( t.findOne({'a.b.c': 'old'}) );
t.update( {}, {$set: {'a.0.b.0.c': 'new'}} );
assert( t.findOne({'a.0.b.c': 'new'}) );
assert( !t.findOne({'a.0.b.c': 'old'}) );

// a.b.$ref
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'a.b.$ref': 1} );
t.insert( {'a': [ { 'b':{ '$ref':'old', '$id':0 } } ] } );
assert( t.findOne({'a.b.$ref': 'old'}) );
assert( t.findOne({'a.0.b.$ref': 'old'}) );
t.update( {}, {$set: {'a.0.b.$ref': 'new'}} );
assert( t.findOne({'a.b.$ref': 'new'}) );
assert( !t.findOne({'a.b.$ref': 'old'}) );

// a.b and a-b
t = db.jstests_update_arraymatch8;
t.drop();
t.ensureIndex( {'a.b': 1} );
t.ensureIndex( {'a-b': 1} );
t.insert( {'a':{'b':'old'}} );
assert( t.findOne({'a.b': 'old'}) );
t.update( {}, {$set: {'a': {'b': 'new'}}} );
assert( t.findOne({'a.b': 'new'}) );
assert( !t.findOne({'a.b': 'old'}) );