2013-11-07 12:42:20 -05:00
|
|
|
// This tests that $setOnInsert works and allow setting the _id
|
2013-02-20 18:06:39 -05:00
|
|
|
t = db.update_setOnInsert;
|
2014-02-19 12:45:53 -05:00
|
|
|
var res;
|
2013-02-20 18:06:39 -05:00
|
|
|
|
|
|
|
|
function dotest( useIndex ) {
|
|
|
|
|
t.drop();
|
|
|
|
|
if ( useIndex ) {
|
|
|
|
|
t.ensureIndex( { a : 1 } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.update( { _id: 5 }, { $inc : { x: 2 }, $setOnInsert : { a : 3 } }, true );
|
2013-07-11 18:07:23 -04:00
|
|
|
assert.docEq( { _id : 5, a: 3, x : 2 }, t.findOne() );
|
2013-02-20 18:06:39 -05:00
|
|
|
|
|
|
|
|
t.update( { _id: 5 }, { $set : { a : 4 } }, true );
|
|
|
|
|
|
|
|
|
|
t.update( { _id: 5 }, { $inc : { x: 2 }, $setOnInsert : { a : 3 } }, true );
|
2013-07-11 18:07:23 -04:00
|
|
|
assert.docEq( { _id : 5, a: 4, x : 4 }, t.findOne() );
|
2013-02-20 18:06:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dotest( false );
|
|
|
|
|
dotest( true );
|
2013-11-07 12:42:20 -05:00
|
|
|
|
|
|
|
|
// Cases for SERVER-9958 -- Allow _id $setOnInsert during insert (if upsert:true, and not doc found)
|
|
|
|
|
t.drop();
|
|
|
|
|
|
2014-02-19 12:45:53 -05:00
|
|
|
res = t.update( {_id: 1} , { $setOnInsert: { "_id.a": new Date() } } , true );
|
2014-07-02 13:19:42 -04:00
|
|
|
assert.writeError(res, "$setOnInsert _id.a worked" );
|
2013-11-07 12:42:20 -05:00
|
|
|
|
2014-02-19 12:45:53 -05:00
|
|
|
res = t.update( {"_id.a": 4} , { $setOnInsert: { "_id.b": 1 } } , true );
|
2014-07-02 13:19:42 -04:00
|
|
|
assert.writeError(res, "$setOnInsert _id.a/b worked" );
|
2013-11-07 12:42:20 -05:00
|
|
|
|
2014-02-19 12:45:53 -05:00
|
|
|
res = t.update( {"_id.a": 4} , { $setOnInsert: { "_id": {a:4, b:1} } } , true );
|
2014-07-02 13:19:42 -04:00
|
|
|
assert.writeError(res, "$setOnInsert _id.a/a+b worked" );
|