0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
This commit is contained in:
Eliot Horowitz 2009-08-25 18:07:30 -04:00
parent 358b5e638c
commit 99b20a0b6b
2 changed files with 52 additions and 3 deletions

View File

@ -236,7 +236,7 @@ namespace mongo {
/* Used for modifiers such as $inc, $set, ... */
struct Mod {
enum Op { INC, SET, PUSH, PUSH_ALL, PULL, PULL_ALL } op;
enum Op { INC, SET, PUSH, PUSH_ALL, PULL, PULL_ALL , POP } op;
const char *fieldName;
// kind of lame; fix one day?
@ -311,8 +311,8 @@ namespace mongo {
return true;
}
static Mod::Op opFromStr( const char *fn ) {
const char *valid[] = { "$inc", "$set", "$push", "$pushAll", "$pull", "$pullAll" };
for( int i = 0; i < 6; ++i )
const char *valid[] = { "$inc", "$set", "$push", "$pushAll", "$pull", "$pullAll" , "$pop" };
for( int i = 0; i < 7; ++i )
if ( strcmp( fn, valid[ i ] ) == 0 )
return Mod::Op( i );
uassert( "Invalid modifier specified " + string( fn ), false );
@ -438,6 +438,12 @@ namespace mongo {
}
break;
}
case Mod::POP: {
uassert( "Cannot apply $pop modifier to non-array", e.type() == Array || e.eoo() );
if ( ! e.embeddedObject().isEmpty() )
inPlacePossible = false;
break;
}
}
}
}
@ -577,6 +583,27 @@ namespace mongo {
}
arr.done();
}
else if ( m->op == Mod::POP ){
BSONObjBuilder arr( b2.subarrayStartAs( m->fieldName ) );
BSONObjIterator i( e.embeddedObject() );
if ( m->elt.isNumber() && m->elt.number() < 0 ){
if ( i.more() ) i.next();
int count = 0;
while( i.more() ) {
arr.appendAs( i.next() , arr.numStr( count++ ).c_str() );
}
}
else {
while( i.more() ) {
BSONElement arrI = i.next();
if ( i.more() ){
arr.append( arrI );
}
}
}
arr.done();
}
++m;
++p;
} else if ( cmp < 0 ) {

22
jstests/push.js Normal file
View File

@ -0,0 +1,22 @@
t = db.push
t.drop();
t.save( { _id : 2 , a : [ 1 ] } );
t.update( { _id : 2 } , { $push : { a : 2 } } );
assert.eq( "1,2" , t.findOne().a.toString() , "A" );
t.update( { _id : 2 } , { $push : { a : 3 } } );
assert.eq( "1,2,3" , t.findOne().a.toString() , "B" );
t.update( { _id : 2 } , { $pop : { a : 1 } } );
assert.eq( "1,2" , t.findOne().a.toString() , "C" );
t.update( { _id : 2 } , { $pop : { a : -1 } } );
assert.eq( "2" , t.findOne().a.toString() , "D" );
t.update( { _id : 2 } , { $push : { a : 3 } } );
t.update( { _id : 2 } , { $push : { a : 4 } } );
t.update( { _id : 2 } , { $push : { a : 5 } } );
assert.eq( "2,3,4,5" , t.findOne().a.toString() , "D" );
t.update( { _id : 2 } , { $pop : { a : -1 } } );
assert.eq( "3,4,5" , t.findOne().a.toString() , "D" );