Skip to content

Commit

Permalink
SERVER-3192 require atomic specifier to be top level
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Jun 16, 2011
1 parent d7908f6 commit fc3f834
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions db/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ namespace mongo {
while( j.more() ) {
BSONElement f = j.next();
uassert( 13087, "$and/$or/$nor match element must be an object", f.type() == Object );
matchers.push_back( shared_ptr< Matcher >( new Matcher( f.embeddedObject() ) ) );
matchers.push_back( shared_ptr< Matcher >( new Matcher( f.embeddedObject(), true ) ) );
}
}

Expand All @@ -309,7 +309,7 @@ namespace mongo {
return true;
}

void Matcher::parseMatchExpressionElement( const BSONElement &e ) {
void Matcher::parseMatchExpressionElement( const BSONElement &e, bool nested ) {

uassert( 13629 , "can't have undefined in a query expression" , e.type() != Undefined );

Expand Down Expand Up @@ -405,6 +405,7 @@ namespace mongo {
_hasArray = true;
}
else if( strcmp(e.fieldName(), "$atomic") == 0 ) {
uassert( 14844, "$atomic specifier must be a top level field", !nested );
_atomic = e.trueValue();
return;
}
Expand All @@ -415,12 +416,12 @@ namespace mongo {

/* _jsobj - the query pattern
*/
Matcher::Matcher(const BSONObj &jsobj) :
Matcher::Matcher(const BSONObj &jsobj, bool nested) :
_where(0), _jsobj(jsobj), _haveSize(), _all(), _hasArray(0), _haveNeg(), _atomic(false), _nRegex(0) {

BSONObjIterator i(_jsobj);
while ( i.more() ) {
parseMatchExpressionElement( i.next() );
parseMatchExpressionElement( i.next(), nested );
}
}

Expand Down
4 changes: 2 additions & 2 deletions db/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace mongo {
return op <= BSONObj::LTE ? -1 : 1;
}

Matcher(const BSONObj &pattern);
Matcher(const BSONObj &pattern, bool nested=false);

~Matcher();

Expand Down Expand Up @@ -182,7 +182,7 @@ namespace mongo {
bool parseClause( const BSONElement &e );
void parseExtractedClause( const BSONElement &e, list< shared_ptr< Matcher > > &matchers );

void parseMatchExpressionElement( const BSONElement &e );
void parseMatchExpressionElement( const BSONElement &e, bool nested );

Where *_where; // set if query uses $where
BSONObj _jsobj; // the query pattern. e.g., { name: "joe" }
Expand Down
5 changes: 5 additions & 0 deletions jstests/remove2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ function g() {
t.save( { x:[7,8,9], z:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" } );

t.remove( {x : {$gte:3}, $atomic:x++ } );

assert( !db.getLastError() );
// $atomic within $and is not allowed.
t.remove( {x : {$gte:3}, $and:[{$atomic:true}] } );
assert( db.getLastError() );

assert( t.findOne({x:3}) == null );
assert( t.findOne({x:8}) == null );
Expand Down

0 comments on commit fc3f834

Please sign in to comment.