2009-01-26 22:19:15 -05:00
// collection.js
if ( ( typeof DBCollection ) == "undefined" ) {
DBCollection = function ( mongo , db , shortName , fullName ) {
this . _mongo = mongo ;
this . _db = db ;
this . _shortName = shortName ;
this . _fullName = fullName ;
2009-07-21 14:41:23 -04:00
2009-05-05 10:43:38 -04:00
this . verify ( ) ;
2009-01-26 22:19:15 -05:00
}
}
2009-05-05 10:43:38 -04:00
DBCollection . prototype . verify = function ( ) {
assert ( this . _fullName , "no fullName" ) ;
2009-05-05 17:28:24 -04:00
assert ( this . _shortName , "no shortName" ) ;
assert ( this . _db , "no db" ) ;
2009-07-21 14:41:23 -04:00
2009-05-05 10:43:38 -04:00
assert . eq ( this . _fullName , this . _db . _name + "." + this . _shortName , "name mismatch" ) ;
2009-05-05 17:28:24 -04:00
assert ( this . _mongo , "no mongo in DBCollection" ) ;
2009-05-05 10:43:38 -04:00
}
2009-01-26 22:19:15 -05:00
DBCollection . prototype . getName = function ( ) {
return this . _shortName ;
2010-02-09 18:28:36 -05:00
}
DBCollection . prototype . help = function ( ) {
2010-04-20 04:43:46 +08:00
var shortName = this . getName ( ) ;
2010-02-09 18:28:36 -05:00
print ( "DBCollection help" ) ;
2010-04-20 04:43:46 +08:00
print ( "\tdb." + shortName + ".count()" ) ;
print ( "\tdb." + shortName + ".dataSize()" ) ;
print ( "\tdb." + shortName + ".distinct( key ) - eg. db." + shortName + ".distinct( 'x' )" ) ;
print ( "\tdb." + shortName + ".drop() drop the collection" ) ;
print ( "\tdb." + shortName + ".dropIndex(name)" ) ;
print ( "\tdb." + shortName + ".dropIndexes()" ) ;
print ( "\tdb." + shortName + ".ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups" ) ;
print ( "\tdb." + shortName + ".reIndex()" ) ;
print ( "\tdb." + shortName + ".find( [query] , [fields]) - first parameter is an optional query filter. second parameter is optional set of fields to return." ) ;
print ( "\t e.g. db." + shortName + ".find( { x : 77 } , { name : 1 , x : 1 } )" ) ;
print ( "\tdb." + shortName + ".find(...).count()" ) ;
print ( "\tdb." + shortName + ".find(...).limit(n)" ) ;
print ( "\tdb." + shortName + ".find(...).skip(n)" ) ;
print ( "\tdb." + shortName + ".find(...).sort(...)" ) ;
print ( "\tdb." + shortName + ".findOne([query])" ) ;
print ( "\tdb." + shortName + ".findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )" ) ;
print ( "\tdb." + shortName + ".getDB() get DB object associated with collection" ) ;
print ( "\tdb." + shortName + ".getIndexes()" ) ;
print ( "\tdb." + shortName + ".group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )" ) ;
print ( "\tdb." + shortName + ".mapReduce( mapFunction , reduceFunction , <optional params> )" ) ;
print ( "\tdb." + shortName + ".remove(query)" ) ;
print ( "\tdb." + shortName + ".renameCollection( newName , <dropTarget> ) renames the collection." ) ;
print ( "\tdb." + shortName + ".runCommand( name , <options> ) runs a db command with the given name where the 1st param is the colleciton name" ) ;
print ( "\tdb." + shortName + ".save(obj)" ) ;
print ( "\tdb." + shortName + ".stats()" ) ;
print ( "\tdb." + shortName + ".storageSize() - includes free space allocated to this collection" ) ;
print ( "\tdb." + shortName + ".totalIndexSize() - size in bytes of all the indexes" ) ;
print ( "\tdb." + shortName + ".totalSize() - storage allocated for all data and indexes" ) ;
print ( "\tdb." + shortName + ".update(query, object[, upsert_bool, multi_bool])" ) ;
print ( "\tdb." + shortName + ".validate() - SLOW" ) ;
print ( "\tdb." + shortName + ".getShardVersion() - only for use with sharding" ) ;
2010-04-21 21:58:00 -04:00
return _ _magicNoPrint ;
2009-01-26 22:19:15 -05:00
}
DBCollection . prototype . getFullName = function ( ) {
return this . _fullName ;
}
DBCollection . prototype . getDB = function ( ) {
return this . _db ;
}
2010-02-24 13:12:37 -05:00
DBCollection . prototype . _dbCommand = function ( cmd , params ) {
if ( typeof ( cmd ) == "object" )
return this . _db . _dbCommand ( cmd ) ;
var c = { } ;
c [ cmd ] = this . getName ( ) ;
if ( params )
Object . extend ( c , params ) ;
return this . _db . _dbCommand ( c ) ;
2009-01-26 22:19:15 -05:00
}
2010-02-24 13:12:37 -05:00
DBCollection . prototype . runCommand = DBCollection . prototype . _dbCommand ;
2009-01-26 22:19:15 -05:00
DBCollection . prototype . _massageObject = function ( q ) {
if ( ! q )
return { } ;
var type = typeof q ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( type == "function" )
return { $where : q } ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( q . isObjectId )
return { _id : q } ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( type == "object" )
return q ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( type == "string" ) {
if ( q . length == 24 )
return { _id : q } ;
2009-07-21 14:41:23 -04:00
2009-01-29 15:58:54 -05:00
return { $where : q } ;
2009-01-26 22:19:15 -05:00
}
throw "don't know how to massage : " + type ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
}
2009-06-24 14:49:50 -04:00
DBCollection . prototype . _validateObject = function ( o ) {
if ( o . _ensureSpecial && o . _checkModify )
throw "can't save a DBQuery object" ;
}
2009-08-10 11:10:15 -04:00
DBCollection . _allowedFields = { $id : 1 , $ref : 1 } ;
2009-01-26 22:19:15 -05:00
DBCollection . prototype . _validateForStorage = function ( o ) {
2009-06-24 14:49:50 -04:00
this . _validateObject ( o ) ;
2009-03-27 16:12:06 -04:00
for ( var k in o ) {
2009-07-21 15:08:45 -04:00
if ( k . indexOf ( "." ) >= 0 ) {
2009-01-26 22:19:15 -05:00
throw "can't have . in field names [" + k + "]" ;
2009-07-21 15:08:45 -04:00
}
2009-08-10 11:10:15 -04:00
if ( k . indexOf ( "$" ) == 0 && ! DBCollection . _allowedFields [ k ] ) {
throw "field names cannot start with $ [" + k + "]" ;
2009-07-21 15:08:45 -04:00
}
if ( o [ k ] !== null && typeof ( o [ k ] ) === "object" ) {
this . _validateForStorage ( o [ k ] ) ;
}
2009-01-26 22:19:15 -05:00
}
2009-07-21 15:08:45 -04:00
} ;
2009-01-26 22:19:15 -05:00
2009-06-24 14:49:50 -04:00
2009-01-26 22:19:15 -05:00
DBCollection . prototype . find = function ( query , fields , limit , skip ) {
2009-07-21 14:41:23 -04:00
return new DBQuery ( this . _mongo , this . _db , this ,
2009-01-26 22:19:15 -05:00
this . _fullName , this . _massageObject ( query ) , fields , limit , skip ) ;
}
DBCollection . prototype . findOne = function ( query , fields ) {
2010-02-27 11:16:26 -05:00
var cursor = this . _mongo . find ( this . _fullName , this . _massageObject ( query ) || { } , fields , - 1 , 0 , 0 ) ;
2009-01-26 22:19:15 -05:00
if ( ! cursor . hasNext ( ) )
return null ;
var ret = cursor . next ( ) ;
2009-05-06 11:00:22 -04:00
if ( cursor . hasNext ( ) ) throw "findOne has more than 1 result!" ;
2009-01-26 22:19:15 -05:00
if ( ret . $err )
throw "error " + tojson ( ret ) ;
return ret ;
}
2009-07-21 15:08:45 -04:00
DBCollection . prototype . insert = function ( obj , _allow _dot ) {
2009-01-26 22:19:15 -05:00
if ( ! obj )
2009-11-19 21:40:11 -05:00
throw "no object passed to insert!" ;
2009-07-21 15:08:45 -04:00
if ( ! _allow _dot ) {
this . _validateForStorage ( obj ) ;
}
2010-01-27 15:52:28 -05:00
if ( typeof ( obj . _id ) == "undefined" ) {
var tmp = obj ; // don't want to modify input
obj = { _id : new ObjectId ( ) } ;
for ( var key in tmp ) {
obj [ key ] = tmp [ key ] ;
}
}
this . _mongo . insert ( this . _fullName , obj ) ;
2010-03-01 23:16:36 -05:00
this . _lastID = obj . _id ;
2009-01-26 22:19:15 -05:00
}
DBCollection . prototype . remove = function ( t ) {
this . _mongo . remove ( this . _fullName , this . _massageObject ( t ) ) ;
}
2009-10-22 09:43:31 -04:00
DBCollection . prototype . update = function ( query , obj , upsert , multi ) {
2009-01-26 22:19:15 -05:00
assert ( query , "need a query" ) ;
assert ( obj , "need an object" ) ;
2009-06-24 14:49:50 -04:00
this . _validateObject ( obj ) ;
2009-10-22 09:43:31 -04:00
this . _mongo . update ( this . _fullName , query , obj , upsert ? true : false , multi ? true : false ) ;
2009-01-26 22:19:15 -05:00
}
DBCollection . prototype . save = function ( obj ) {
2009-08-28 12:34:56 -04:00
if ( obj == null || typeof ( obj ) == "undefined" )
throw "can't save a null" ;
2009-05-14 16:39:10 -04:00
if ( typeof ( obj . _id ) == "undefined" ) {
2009-05-13 17:27:45 -04:00
obj . _id = new ObjectId ( ) ;
2009-02-06 12:31:24 -05:00
return this . insert ( obj ) ;
2009-01-26 22:19:15 -05:00
}
else {
2009-02-06 12:31:24 -05:00
return this . update ( { _id : obj . _id } , obj , true ) ;
2009-01-26 22:19:15 -05:00
}
}
DBCollection . prototype . _genIndexName = function ( keys ) {
var name = "" ;
2009-03-27 16:12:06 -04:00
for ( var k in keys ) {
2009-12-21 11:21:25 -08:00
var v = keys [ k ] ;
if ( typeof v == "function" )
continue ;
2009-01-26 22:19:15 -05:00
if ( name . length > 0 )
name += "_" ;
name += k + "_" ;
if ( typeof v == "number" )
name += v ;
}
return name ;
}
2009-04-20 13:51:54 -04:00
DBCollection . prototype . _indexSpec = function ( keys , options ) {
2009-08-26 10:22:55 -04:00
var ret = { ns : this . _fullName , key : keys , name : this . _genIndexName ( keys ) } ;
if ( ! options ) {
}
else if ( typeof ( options ) == "string" )
ret . name = options ;
else if ( typeof ( options ) == "boolean" )
ret . unique = true ;
else if ( typeof ( options ) == "object" ) {
if ( options . length ) {
var nb = 0 ;
for ( var i = 0 ; i < options . length ; i ++ ) {
if ( typeof ( options [ i ] ) == "string" )
ret . name = options [ i ] ;
else if ( typeof ( options [ i ] ) == "boolean" ) {
if ( options [ i ] ) {
if ( nb == 0 )
ret . unique = true ;
if ( nb == 1 )
ret . dropDups = true ;
}
nb ++ ;
}
}
}
else {
Object . extend ( ret , options ) ;
}
}
else {
throw "can't handle: " + typeof ( options ) ;
}
/ *
return ret ;
2009-04-20 13:51:54 -04:00
var name ;
2009-08-05 16:02:20 -04:00
var nTrue = 0 ;
2009-08-26 10:22:55 -04:00
if ( ! isObject ( options ) ) {
2009-04-20 13:51:54 -04:00
options = [ options ] ;
}
2009-08-26 10:22:55 -04:00
if ( options . length ) {
for ( var i = 0 ; i < options . length ; ++ i ) {
var o = options [ i ] ;
if ( isString ( o ) ) {
ret . name = o ;
} else if ( typeof ( o ) == "boolean" ) {
if ( o ) {
++ nTrue ;
}
}
}
if ( nTrue > 0 ) {
ret . unique = true ;
}
if ( nTrue > 1 ) {
ret . dropDups = true ;
2009-04-20 13:51:54 -04:00
}
}
2009-08-26 10:22:55 -04:00
* /
2009-04-20 13:51:54 -04:00
return ret ;
}
DBCollection . prototype . createIndex = function ( keys , options ) {
var o = this . _indexSpec ( keys , options ) ;
2009-07-21 15:08:45 -04:00
this . _db . getCollection ( "system.indexes" ) . insert ( o , true ) ;
2009-01-26 22:19:15 -05:00
}
2009-04-20 13:51:54 -04:00
DBCollection . prototype . ensureIndex = function ( keys , options ) {
var name = this . _indexSpec ( keys , options ) . name ;
2009-01-26 22:19:15 -05:00
this . _indexCache = this . _indexCache || { } ;
2009-05-13 23:15:59 -04:00
if ( this . _indexCache [ name ] ) {
2009-10-20 16:20:22 -04:00
return ;
2009-05-13 23:15:59 -04:00
}
2009-01-26 22:19:15 -05:00
2009-04-20 13:51:54 -04:00
this . createIndex ( keys , options ) ;
2009-08-05 16:02:20 -04:00
if ( this . getDB ( ) . getLastError ( ) == "" ) {
this . _indexCache [ name ] = true ;
}
2009-01-26 22:19:15 -05:00
}
DBCollection . prototype . resetIndexCache = function ( ) {
this . _indexCache = { } ;
2010-02-09 18:28:36 -05:00
}
DBCollection . prototype . reIndex = function ( ) {
return this . _db . runCommand ( { reIndex : this . getName ( ) } ) ;
2009-02-09 13:05:36 -05:00
}
DBCollection . prototype . dropIndexes = function ( ) {
2009-01-26 22:19:15 -05:00
this . resetIndexCache ( ) ;
var res = this . _db . runCommand ( { deleteIndexes : this . getName ( ) , index : "*" } ) ;
assert ( res , "no result from dropIndex result" ) ;
if ( res . ok )
return res ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( res . errmsg . match ( /not found/ ) )
return res ;
throw "error dropping indexes : " + tojson ( res ) ;
}
DBCollection . prototype . drop = function ( ) {
2009-04-16 10:18:09 -04:00
this . resetIndexCache ( ) ;
2009-11-09 12:42:20 -05:00
var ret = this . _db . runCommand ( { drop : this . getName ( ) } ) ;
2009-11-09 13:10:30 -05:00
if ( ! ret . ok ) {
if ( ret . errmsg == "ns not found" )
return false ;
2009-11-09 12:42:20 -05:00
throw "drop failed: " + tojson ( ret ) ;
2009-11-09 13:10:30 -05:00
}
return true ;
2009-01-26 22:19:15 -05:00
}
2009-12-30 20:08:43 -05:00
DBCollection . prototype . findAndModify = function ( args ) {
var cmd = { findandmodify : this . getName ( ) } ;
2010-01-27 15:52:28 -05:00
for ( var key in args ) {
2009-12-30 20:08:43 -05:00
cmd [ key ] = args [ key ] ;
}
var ret = this . _db . runCommand ( cmd ) ;
if ( ! ret . ok ) {
if ( ret . errmsg == "No matching object found" ) {
return { } ;
}
throw "findAndModifyFailed failed: " + tojson ( ret . errmsg ) ;
}
return ret . value ;
}
2009-12-28 13:09:44 -05:00
DBCollection . prototype . renameCollection = function ( newName , dropTarget ) {
return this . _db . _adminCommand ( { renameCollection : this . _fullName ,
to : this . _db . _name + "." + newName ,
dropTarget : dropTarget } )
2009-09-07 11:14:48 -04:00
}
2009-01-26 22:19:15 -05:00
DBCollection . prototype . validate = function ( ) {
var res = this . _db . runCommand ( { validate : this . getName ( ) } ) ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
res . valid = false ;
if ( res . result ) {
var str = "-" + tojson ( res . result ) ;
res . valid = ! ( str . match ( /exception/ ) || str . match ( /corrupt/ ) ) ;
var p = /lastExtentSize:(\d+)/ ;
var r = p . exec ( str ) ;
if ( r ) {
res . lastExtentSize = Number ( r [ 1 ] ) ;
}
}
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
return res ;
}
2009-11-24 15:24:41 -05:00
DBCollection . prototype . getShardVersion = function ( ) {
return this . _db . _adminCommand ( { getShardVersion : this . _fullName } ) ;
}
2009-01-26 22:19:15 -05:00
DBCollection . prototype . getIndexes = function ( ) {
2009-05-13 23:15:59 -04:00
return this . getDB ( ) . getCollection ( "system.indexes" ) . find ( { ns : this . getFullName ( ) } ) . toArray ( ) ;
2009-01-26 22:19:15 -05:00
}
2009-05-16 21:01:56 -04:00
DBCollection . prototype . getIndices = DBCollection . prototype . getIndexes ;
2009-05-19 10:54:06 -04:00
DBCollection . prototype . getIndexSpecs = DBCollection . prototype . getIndexes ;
2009-05-16 21:01:56 -04:00
2009-05-19 10:54:06 -04:00
DBCollection . prototype . getIndexKeys = function ( ) {
2009-07-21 14:41:23 -04:00
return this . getIndexes ( ) . map (
2009-01-26 22:19:15 -05:00
function ( i ) {
2009-05-19 10:54:06 -04:00
return i . key ;
2009-01-26 22:19:15 -05:00
}
) ;
}
2009-05-11 16:38:15 -04:00
DBCollection . prototype . count = function ( x ) {
return this . find ( x ) . count ( ) ;
2009-01-26 22:19:15 -05:00
}
/ * *
* Drop free lists . Normally not used .
* Note this only does the collection itself , not the namespaces of its indexes ( see cleanAll ) .
* /
DBCollection . prototype . clean = function ( ) {
return this . _dbCommand ( { clean : this . getName ( ) } ) ;
}
/ * *
* < p > Drop a specified index . < / p >
*
* < p >
* Name is the name of the index in the system . indexes name field . ( Run db . system . indexes . find ( ) to
* see example data . )
* < / p >
*
* < p > Note : alpha : space is not reclaimed < / p >
* @ param { String } name of index to delete .
* @ return A result object . result . ok will be true if successful .
* /
DBCollection . prototype . dropIndex = function ( index ) {
assert ( index , "need to specify index to dropIndex" ) ;
2009-07-21 14:41:23 -04:00
2009-01-26 22:19:15 -05:00
if ( ! isString ( index ) && isObject ( index ) )
index = this . _genIndexName ( index ) ;
2009-07-21 14:41:23 -04:00
2010-02-24 13:12:37 -05:00
var res = this . _dbCommand ( "deleteIndexes" , { index : index } ) ;
2009-01-26 22:19:15 -05:00
this . resetIndexCache ( ) ;
return res ;
}
2009-03-06 16:44:16 -05:00
DBCollection . prototype . copyTo = function ( newName ) {
2009-07-21 14:41:23 -04:00
return this . getDB ( ) . eval (
2009-03-06 16:44:16 -05:00
function ( collName , newName ) {
var from = db [ collName ] ;
var to = db [ newName ] ;
to . ensureIndex ( { _id : 1 } ) ;
var count = 0 ;
2009-07-21 14:41:23 -04:00
2009-03-06 16:44:16 -05:00
var cursor = from . find ( ) ;
while ( cursor . hasNext ( ) ) {
var o = cursor . next ( ) ;
count ++ ;
to . save ( o ) ;
}
2009-07-21 14:41:23 -04:00
2009-03-06 16:44:16 -05:00
return count ;
2009-07-21 14:41:23 -04:00
} , this . getName ( ) , newName
2009-03-06 16:44:16 -05:00
) ;
}
2009-01-26 22:19:15 -05:00
DBCollection . prototype . getCollection = function ( subName ) {
return this . _db . getCollection ( this . _shortName + "." + subName ) ;
}
2010-02-27 12:11:41 -05:00
DBCollection . prototype . stats = function ( scale ) {
return this . _db . runCommand ( { collstats : this . _shortName , scale : scale } ) ;
2009-05-16 21:01:56 -04:00
}
DBCollection . prototype . dataSize = function ( ) {
2009-08-12 13:53:52 -04:00
return this . stats ( ) . size ;
}
DBCollection . prototype . storageSize = function ( ) {
return this . stats ( ) . storageSize ;
2009-05-16 21:01:56 -04:00
}
2009-10-07 12:44:24 -04:00
DBCollection . prototype . totalIndexSize = function ( verbose ) {
2010-02-22 22:16:13 -05:00
var stats = this . stats ( ) ;
if ( verbose ) {
for ( var ns in stats . indexSizes ) {
print ( ns + "\t" + stats . indexSizes [ ns ] ) ;
2009-05-16 21:01:56 -04:00
}
2010-02-22 22:16:13 -05:00
}
return stats . totalIndexSize ;
2009-05-16 21:01:56 -04:00
}
2009-08-24 17:13:15 -04:00
DBCollection . prototype . totalSize = function ( ) {
var total = this . storageSize ( ) ;
var mydb = this . _db ;
var shortName = this . _shortName ;
this . getIndexes ( ) . forEach (
function ( spec ) {
var coll = mydb . getCollection ( shortName + ".$" + spec . name ) ;
var mysize = coll . storageSize ( ) ;
//print( coll + "\t" + mysize + "\t" + tojson( coll.validate() ) );
total += coll . dataSize ( ) ;
}
) ;
return total ;
}
2009-05-20 14:50:33 -04:00
DBCollection . prototype . convertToCapped = function ( bytes ) {
if ( ! bytes )
throw "have to specify # of bytes" ;
return this . _dbCommand ( { convertToCapped : this . _shortName , size : bytes } )
}
2009-05-20 16:43:43 -04:00
DBCollection . prototype . exists = function ( ) {
return this . _db . system . namespaces . findOne ( { name : this . _fullName } ) ;
}
DBCollection . prototype . isCapped = function ( ) {
var e = this . exists ( ) ;
return ( e && e . options && e . options . capped ) ? true : false ;
}
2009-11-24 09:41:33 -05:00
DBCollection . prototype . distinct = function ( keyString , query ) {
var res = this . _dbCommand ( { distinct : this . _shortName , key : keyString , query : query || { } } ) ;
2009-09-09 17:33:27 -04:00
if ( ! res . ok )
throw "distinct failed: " + tojson ( res ) ;
return res . values ;
}
2009-06-05 10:05:33 -04:00
DBCollection . prototype . group = function ( params ) {
params . ns = this . _shortName ;
return this . _db . group ( params ) ;
}
2009-05-20 16:43:43 -04:00
2009-08-19 11:51:16 -04:00
DBCollection . prototype . groupcmd = function ( params ) {
params . ns = this . _shortName ;
return this . _db . groupcmd ( params ) ;
}
2009-10-19 11:00:53 -04:00
MapReduceResult = function ( db , o ) {
Object . extend ( this , o ) ;
this . _o = o ;
2009-12-21 11:21:25 -08:00
this . _keys = Object . keySet ( o ) ;
2009-10-19 11:00:53 -04:00
this . _db = db ;
this . _coll = this . _db . getCollection ( this . result ) ;
}
MapReduceResult . prototype . _simpleKeys = function ( ) {
return this . _o ;
}
MapReduceResult . prototype . find = function ( ) {
return DBCollection . prototype . find . apply ( this . _coll , arguments ) ;
}
MapReduceResult . prototype . drop = function ( ) {
2009-10-22 23:47:52 -04:00
return this . _coll . drop ( ) ;
2009-10-19 11:00:53 -04:00
}
2009-11-10 15:43:51 -05:00
/ * *
* just for debugging really
* /
MapReduceResult . prototype . convertToSingleObject = function ( ) {
var z = { } ;
this . _coll . find ( ) . forEach ( function ( a ) { z [ a . _id ] = a . value ; } ) ;
return z ;
}
2009-10-19 11:00:53 -04:00
/ * *
* @ param optional object of optional fields ;
* /
DBCollection . prototype . mapReduce = function ( map , reduce , optional ) {
var c = { mapreduce : this . _shortName , map : map , reduce : reduce } ;
if ( optional )
Object . extend ( c , optional ) ;
2009-10-22 23:47:52 -04:00
var raw = this . _db . runCommand ( c ) ;
if ( ! raw . ok )
2010-01-16 08:01:38 -05:00
throw "map reduce failed: " + tojson ( raw ) ;
2009-10-22 23:47:52 -04:00
return new MapReduceResult ( this . _db , raw ) ;
2009-10-19 11:00:53 -04:00
}
DBCollection . prototype . toString = function ( ) {
return this . getFullName ( ) ;
}
2009-01-26 22:19:15 -05:00
DBCollection . prototype . toString = function ( ) {
return this . getFullName ( ) ;
}
2009-06-04 17:01:56 -04:00
2009-10-19 11:00:53 -04:00
DBCollection . prototype . tojson = DBCollection . prototype . toString ;
2009-06-04 17:01:56 -04:00
DBCollection . prototype . shellPrint = DBCollection . prototype . toString ;
2009-10-19 11:00:53 -04:00