0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 09:06:21 +01:00
mongodb/shell/query.js

255 lines
5.8 KiB
JavaScript
Raw Normal View History

2009-01-27 04:19:15 +01:00
// query.js
if ( typeof DBQuery == "undefined" ){
2010-02-27 17:16:26 +01:00
DBQuery = function( mongo , db , collection , ns , query , fields , limit , skip , batchSize ){
2009-01-27 04:19:15 +01:00
this._mongo = mongo; // 0
this._db = db; // 1
this._collection = collection; // 2
this._ns = ns; // 3
this._query = query || {}; // 4
this._fields = fields; // 5
this._limit = limit || 0; // 6
this._skip = skip || 0; // 7
2010-02-27 17:16:26 +01:00
this._batchSize = batchSize || 0;
2009-01-27 04:19:15 +01:00
this._cursor = null;
this._numReturned = 0;
this._special = false;
}
2009-05-13 23:53:41 +02:00
print( "DBQuery probably won't have array access " );
2009-01-27 04:19:15 +01:00
}
2009-09-09 22:52:02 +02:00
DBQuery.prototype.help = function(){
print( "DBQuery help" );
print( "\t.sort( {...} )" )
print( "\t.limit( n )" )
print( "\t.skip( n )" )
print( "\t.count() - total # of objects matching query, ignores skip,limit" )
print( "\t.size() - total # of objects cursor would return skip,limit effect this" )
2009-09-09 22:52:02 +02:00
print( "\t.explain()" )
print( "\t.forEach( func )" )
print( "\t.map( func )" )
2009-09-09 22:52:02 +02:00
}
2009-01-27 04:19:15 +01:00
DBQuery.prototype.clone = function(){
var q = new DBQuery( this._mongo , this._db , this._collection , this._ns ,
this._query , this._fields ,
2010-02-27 17:16:26 +01:00
this._limit , this._skip , this._batchSize );
2009-01-27 04:19:15 +01:00
q._special = this._special;
return q;
}
DBQuery.prototype._ensureSpecial = function(){
if ( this._special )
return;
var n = { query : this._query };
this._query = n;
this._special = true;
}
DBQuery.prototype._checkModify = function(){
if ( this._cursor )
throw "query already executed";
}
DBQuery.prototype._exec = function(){
if ( ! this._cursor ){
assert.eq( 0 , this._numReturned );
2010-02-27 17:16:26 +01:00
this._cursor = this._mongo.find( this._ns , this._query , this._fields , this._limit , this._skip , this._batchSize );
2009-01-27 04:19:15 +01:00
this._cursorSeen = 0;
}
return this._cursor;
}
DBQuery.prototype.limit = function( limit ){
this._checkModify();
this._limit = limit;
return this;
}
2010-02-27 17:16:26 +01:00
DBQuery.prototype.batchSize = function( batchSize ){
this._checkModify();
this._batchSize = batchSize;
return this;
}
2009-01-27 04:19:15 +01:00
DBQuery.prototype.skip = function( skip ){
this._checkModify();
this._skip = skip;
return this;
}
DBQuery.prototype.hasNext = function(){
this._exec();
if ( this._limit > 0 && this._cursorSeen >= this._limit )
return false;
var o = this._cursor.hasNext();
return o;
}
DBQuery.prototype.next = function(){
this._exec();
var o = this._cursor.hasNext();
if ( o )
this._cursorSeen++;
else
throw "error hasNext: " + o;
2009-01-27 04:19:15 +01:00
var ret = this._cursor.next();
if ( ret.$err && this._numReturned == 0 && ! this.hasNext() )
throw "error: " + tojson( ret );
this._numReturned++;
return ret;
}
DBQuery.prototype.toArray = function(){
if ( this._arr )
return this._arr;
var a = [];
while ( this.hasNext() )
a.push( this.next() );
this._arr = a;
return a;
}
DBQuery.prototype.count = function( applySkipLimit ){
2009-01-27 04:19:15 +01:00
var cmd = { count: this._collection.getName() };
if ( this._query ){
if ( this._special )
cmd.query = this._query.query;
else
cmd.query = this._query;
}
cmd.fields = this._fields || {};
if ( applySkipLimit ){
if ( this._limit )
cmd.limit = this._limit;
if ( this._skip )
cmd.skip = this._skip;
}
2009-01-27 04:19:15 +01:00
var res = this._db.runCommand( cmd );
if( res && res.n != null ) return res.n;
2009-02-04 23:10:48 +01:00
throw "count failed: " + tojson( res );
2009-01-27 04:19:15 +01:00
}
DBQuery.prototype.size = function(){
return this.count( true );
}
DBQuery.prototype.countReturn = function(){
var c = this.count();
if ( this._skip )
c = c - this._skip;
if ( this._limit > 0 && this._limit < c )
return this._limit;
return c;
}
2009-04-20 23:41:27 +02:00
/**
* iterative count - only for testing
*/
DBQuery.prototype.itcount = function(){
var num = 0;
while ( this.hasNext() ){
num++;
this.next();
}
return num;
}
2009-01-27 04:19:15 +01:00
DBQuery.prototype.length = function(){
return this.toArray().length;
}
DBQuery.prototype._addSpecial = function( name , value ){
2009-01-27 04:19:15 +01:00
this._ensureSpecial();
this._query[name] = value;
2009-01-27 04:19:15 +01:00
return this;
}
DBQuery.prototype.sort = function( sortBy ){
return this._addSpecial( "orderby" , sortBy );
}
2009-01-30 16:05:07 +01:00
DBQuery.prototype.hint = function( hint ){
return this._addSpecial( "$hint" , hint );
2009-01-30 16:05:07 +01:00
}
DBQuery.prototype.min = function( min ) {
return this._addSpecial( "$min" , min );
}
DBQuery.prototype.max = function( max ) {
return this._addSpecial( "$max" , max );
}
2009-01-30 16:05:07 +01:00
2009-01-27 04:19:15 +01:00
DBQuery.prototype.forEach = function( func ){
while ( this.hasNext() )
func( this.next() );
}
2009-06-21 03:36:35 +02:00
DBQuery.prototype.map = function( func ){
var a = [];
while ( this.hasNext() )
a.push( func( this.next() ) );
return a;
}
2009-01-27 04:19:15 +01:00
DBQuery.prototype.arrayAccess = function( idx ){
return this.toArray()[idx];
}
DBQuery.prototype.explain = function(){
var n = this.clone();
n._ensureSpecial();
n._query.$explain = true;
n._limit = n._limit * -1;
2009-01-27 04:19:15 +01:00
return n.next();
}
2009-08-11 16:52:20 +02:00
DBQuery.prototype.snapshot = function(){
this._ensureSpecial();
this._query.$snapshot = true;
return this;
}
2009-08-11 16:52:20 +02:00
2009-01-27 04:19:15 +01:00
DBQuery.prototype.shellPrint = function(){
try {
var n = 0;
2009-01-27 17:41:23 +01:00
while ( this.hasNext() && n < 20 ){
2009-10-12 23:20:19 +02:00
var s = tojson( this.next() , "" , true );
2009-01-27 04:19:15 +01:00
print( s );
n++;
}
2009-06-08 17:20:30 +02:00
if ( this.hasNext() ){
2009-01-27 04:19:15 +01:00
print( "has more" );
2009-06-08 17:20:30 +02:00
___it___ = this;
}
else {
___it___ = null;
}
2009-01-27 04:19:15 +01:00
}
catch ( e ){
print( e );
}
}
DBQuery.prototype.toString = function(){
return "DBQuery: " + this._ns + " -> " + tojson( this.query );
}