summaryrefslogtreecommitdiff
path: root/jstests/updatel.js
diff options
context:
space:
mode:
authoraaron <aaron@10gen.com>2012-12-22 13:34:06 -0800
committeraaron <aaron@10gen.com>2012-12-27 12:20:18 -0800
commitf498a5c235e4da255d9cfea336b66b9c30403706 (patch)
tree169f3cccc648f1e29ca740a1781cb1b75831221c /jstests/updatel.js
parent5b24436ec22e8925d921a5f070e5c46132915f6c (diff)
downloadmongo-f498a5c235e4da255d9cfea336b66b9c30403706.tar.gz
SERVER-6669 SERVER-4713 Validate that a positional match is found before applying an update mod with the positional operator.
Diffstat (limited to 'jstests/updatel.js')
-rw-r--r--jstests/updatel.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/jstests/updatel.js b/jstests/updatel.js
new file mode 100644
index 00000000000..b4a80061a2b
--- /dev/null
+++ b/jstests/updatel.js
@@ -0,0 +1,48 @@
+// The positional operator allows an update modifier field path to contain a sentinel ('$') path
+// part that is replaced with the numeric position of an array element matched by the update's query
+// spec. <http://docs.mongodb.org/manual/reference/operators/#_S_>
+
+// If no array element position from a query is available to substitute for the positional operator
+// setinel ('$'), the update fails with an error. SERVER-6669 SERVER-4713
+
+t = db.jstests_updatel;
+t.drop();
+
+
+
+// The collection is empty, forcing an upsert. In this case the query has no array position match
+// to substiture for the positional operator. SERVER-4713
+t.update( {}, { $set:{ 'a.$.b':1 } }, true );
+assert( db.getLastError(), "An error is reported." );
+assert.eq( 0, t.count(), "No upsert occurred." );
+
+
+
+// Save a document to the collection so it is no longer empty.
+t.save( { _id:0 } );
+
+// Now, with an existing document, trigger an update rather than an upsert. The query has no array
+// position match to substiture for the positional operator. SERVER-6669
+t.update( {}, { $set:{ 'a.$.b':1 } } );
+assert( db.getLastError(), "An error is reported." );
+assert.eq( [ { _id:0 } ], t.find().toArray(), "No update occurred." );
+
+
+
+// Now, try with an update by _id (without a query array match).
+t.update( { _id:0 }, { $set:{ 'a.$.b':1 } } );
+assert( db.getLastError(), "An error is reported." );
+assert.eq( [ { _id:0 } ], t.find().toArray(), "No update occurred." );
+
+
+
+// Seed the collection with a document suitable for the following check.
+t.remove();
+t.save( { _id:0, a:[ { b:{ c:1 } } ] } );
+
+// Now, attempt to apply an update with two nested positional operators. There is a positional
+// query match for the first positional operator but not the second. Note that dollar sign
+// substitution for multiple positional opertors is not implemented (SERVER-831).
+t.update( { 'a.b.c':1 }, { $set:{ 'a.$.b.$.c':2 } } );
+assert( db.getLastError(), "An error is reported" );
+assert.eq( [ { _id:0, a:[ { b:{ c:1 } } ] } ], t.find().toArray(), "No update occurred." );