Permalink
Browse files

SERVER-3245: $addToSet $each filter dups of new field

  • Loading branch information...
1 parent 84816a6 commit 6a5bed92f5ba424b965f4c3552cd5dc2b0dc28ce Tony Hannan committed Sep 22, 2011
Showing with 34 additions and 2 deletions.
  1. +16 −1 db/ops/update.h
  2. +18 −1 jstests/update_addToSet.js
View
@@ -578,9 +578,24 @@ namespace mongo {
switch ( m.op ) {
case Mod::PUSH:
+ b.appendArray( m.shortFieldName, m.getEach() );
+ break;
case Mod::ADDTOSET: {
if ( m.isEach() ) {
- b.appendArray( m.shortFieldName , m.getEach() );
+ // Remove any duplicates in given array
+ BSONObjBuilder arr( b.subarrayStart( m.shortFieldName ) );
+ BSONElementSet toadd;
+ m.parseEach( toadd );
+ BSONObjIterator i( m.getEach() );
+ int n = 0;
+ while ( i.more() ) {
+ BSONElement e = i.next();
+ if ( toadd.count(e) ) {
+ arr.appendAs( e , BSONObjBuilder::numStr( n++ ) );
+ toadd.erase( e );
+ }
+ }
+ arr.done();
}
else {
BSONObjBuilder arr( b.subarrayStart( m.shortFieldName ) );
View
@@ -32,10 +32,27 @@ assert.eq( o , t.findOne() , "B3" );
t.update( { _id : 1 } , { $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } } , true );
assert.eq( o , t.findOne() , "B4" );
-
// SERVER-630
t.drop();
t.update( { _id : 2 } , { $addToSet : { a : 3 } } , true );
assert.eq( 1 , t.count() , "C1" );
assert.eq( { _id : 2 , a : [ 3 ] } , t.findOne() , "C2" );
+// SERVER-3245
+o = {_id: 1, a: [1,2]};
+t.drop();
+t.update( {_id: 1}, {$addToSet: {a: {$each: [1,2]}}}, true );
+assert.eq( o, t.findOne(), "D1" );
+
+t.drop();
+t.update( {_id: 1}, {$addToSet: {a: {$each: [1,2,1,2]}}}, true );
+assert.eq( o, t.findOne(), "D2" );
+
+t.drop();
+t.insert( {_id: 1} );
+t.update( {_id: 1}, {$addToSet: {a: {$each: [1,2,2,1]}}} );
+assert.eq( o, t.findOne(), "D3" );
+
+t.update( {_id: 1}, {$addToSet: {a: {$each: [3,2,2,3,3]}}} );
+o.a.push( 3 );
+assert.eq( o, t.findOne(), "D4" );

0 comments on commit 6a5bed9

Please sign in to comment.