From 4a01d610adee3412673a8326c4fbe5261e85db2b Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Thu, 29 Jan 2009 15:58:54 -0500 Subject: [PATCH] $where fix --- jstests/where1.js | 7 +++++-- shell/MongoJS.cpp | 5 ++++- shell/collection.js | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/jstests/where1.js b/jstests/where1.js index 701d6588dc2..831e9f59e22 100644 --- a/jstests/where1.js +++ b/jstests/where1.js @@ -1,5 +1,4 @@ -db = connect("test"); t = db.getCollection( "where1" ); t.drop(); @@ -7,4 +6,8 @@ t.save( { a : 1 } ); t.save( { a : 2 } ); t.save( { a : 3 } ); -assert.eq( 1 , t.find( function(){ return this.a == 2; } ).length() ); +assert.eq( 1 , t.find( function(){ return this.a == 2; } ).length() , "A" ); + +assert.eq( 1 , t.find( { $where : "this.a == 2" } ).toArray().length , "B" ); + +assert.eq( 1 , t.find( "this.a == 2" ).toArray().length , "C" ); diff --git a/shell/MongoJS.cpp b/shell/MongoJS.cpp index 0747bb999ad..422d8634b10 100644 --- a/shell/MongoJS.cpp +++ b/shell/MongoJS.cpp @@ -167,7 +167,10 @@ Local mongoToV8( BSONObj & m , bool array ){ void v8ToMongoElement( BSONObjBuilder & b , v8::Handle name , const string sname , v8::Handle value ){ if ( value->IsString() ){ - b.append( sname.c_str() , toSTLString( value ).c_str() ); + if ( sname == "$where" ) + b.appendCode( sname.c_str() , toSTLString( value ).c_str() ); + else + b.append( sname.c_str() , toSTLString( value ).c_str() ); return; } diff --git a/shell/collection.js b/shell/collection.js index e679bad4d47..515950f178b 100644 --- a/shell/collection.js +++ b/shell/collection.js @@ -71,7 +71,7 @@ DBCollection.prototype._massageObject = function( q ){ if ( q.length == 24 ) return { _id : q }; - throw "don't know how to handle string [" + q + "]"; + return { $where : q }; } throw "don't know how to massage : " + type;