0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

Merge branch 'master' of ssh://git.10gen.com/data/gitroot/p

This commit is contained in:
Eliot Horowitz 2009-01-30 16:08:23 -05:00
commit 7a4d0b89cf
5 changed files with 70 additions and 17 deletions

View File

@ -28,6 +28,16 @@
namespace mongo {
Query& Query::where(const char *jscode, BSONObj scope) {
/* use where() before sort() and hint() and explain(), else this will assert. */
assert( !obj.hasField("query") );
BSONObjBuilder b;
b.appendElements(obj);
b.appendWhere(jscode, scope);
obj = b.doneAndDecouple();
return *this;
}
Query& Query::sort(const BSONObj& s) {
BSONObjBuilder b;
if( obj.hasElement("query") )

View File

@ -112,6 +112,22 @@ namespace mongo {
Normally it is easier to use the mongo shell to run db.find(...).explain().
*/
Query& explain();
/** Queries to the Mongo database support a $where parameter option which contains
a javascript function that is evaluated to see whether objects being queried match
its criteria. Use this helper to append such a function to a query object.
Your query may also contain other traditional Mongo query terms.
@param jscode The javascript code to execute.
@param scope Context for the javascript object. List in a BSON object any
variables you would like defined when the jscode executes. One can think
of these as "bind variables".
Example:
conn.findOne("test.coll", Query("a==3").where("this.b == 2 || this.c == 3"));
*/
Query& where(const char *jscode, BSONObj scope);
Query& where(const char *jscode) { return where(jscode, BSONObj()); }
};
#define QUERY(x) Query( BSON(x) )

View File

@ -32,11 +32,9 @@ int main() {
cout << "now using $where" << endl;
BSONObjBuilder query;
Query q = Query("{}").where("this.name == name" , BSON( "name" << "sara" ));
query.appendWhere( "this.name == name" , BSON( "name" << "sara" ) );
cursor = conn.query( ns , query.done() );
cursor = conn.query( ns , q );
int num = 0;
while ( cursor->more() ) {

View File

@ -518,17 +518,12 @@ namespace mongo {
/** @return true if field exists in the object */
bool hasElement(const char *name) const;
/** get the _id field from the object. assumes _id is the first
element of the object -- this is done for performance. drivers should
honor this convention.
/** Get the _id field from the object. For good performance drivers should
assure that _id is the first element of the object; however, correct operation
is assured regardless.
@return true if found
*/
bool getObjectID(BSONElement& e) {
BSONElement f = firstElement();
if( strcmp(f.fieldName(), "_id") )
return false;
e = f;
return true;
}
bool getObjectID(BSONElement& e);
OID* __getOID() {
BSONElement e = firstElement();
@ -732,10 +727,13 @@ namespace mongo {
b.append((int) strlen(symbol)+1);
b.append(symbol);
}
/** Add Null element to the object */
void appendNull( const char *fieldName ) {
b.append( (char) jstNULL );
b.append( fieldName );
}
// Append an element that is less than all other keys.
void appendMinKey( const char *fieldName ) {
b.append( (char) MinKey );
@ -746,6 +744,8 @@ namespace mongo {
b.append( (char) MaxKey );
b.append( fieldName );
}
/* Deprecated (but supported) */
void appendDBRef( const char *fieldName, const char *ns, const OID &oid ) {
b.append( (char) DBRef );
b.append( fieldName );
@ -753,6 +753,7 @@ namespace mongo {
b.append( ns );
b.append( (void *) &oid, 12 );
}
void appendBinData( const char *fieldName, int len, BinDataType type, const char *data ) {
b.append( (char) BinData );
b.append( fieldName );
@ -761,6 +762,9 @@ namespace mongo {
b.append( (void *) data, len );
}
/** Append to the BSON object a field of type CodeWScope. This is a javascript code
fragment accompanied by some scope that goes with it.
*/
void appendCodeWScope( const char *fieldName, const char *code, const BSONObj &scope ) {
b.append( (char) CodeWScope );
b.append( fieldName );
@ -770,6 +774,7 @@ namespace mongo {
b.append( ( void * )scope.objdata(), scope.objsize() );
}
/* helper function -- see Query::where() for primary way to do this. */
void appendWhere( const char *code, const BSONObj &scope ){
appendCodeWScope( "$where" , code , scope );
}
@ -1007,5 +1012,13 @@ namespace mongo {
*this = emptyObj;
}
inline bool BSONObj::getObjectID(BSONElement& e) {
BSONElement f = findElement("_id");
if( !f.eoo() ) {
e = f;
return true;
}
return false;
}
} // namespace mongo

View File

@ -299,11 +299,27 @@ v8::Handle< v8::Value > ResetDbpath( const v8::Arguments &a ) {
}
void killDb( int port ) {
assert( dbs.count( port ) == 1 );
if( dbs.count( port ) != 1 ) {
cout << "No db started on port: " << port << endl;
return;
}
pid_t pid = dbs[ port ];
kill( pid, SIGTERM );
int temp;
waitpid( pid, &temp, 0 );
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
++xt.sec;
int i = 0;
for( ; i < 5; ++i, ++xt.sec ) {
int temp;
if( waitpid( pid, &temp, WNOHANG ) != 0 )
break;
boost::thread::sleep( xt );
}
if ( i == 5 )
kill( pid, SIGKILL );
dbs.erase( port );
}