@@ -10244,7 +10244,7 @@ moment.fn.within = function(range) {
1024410244
1024510245/* global chatty */
1024610246
10247- chatty ( '\033[1;32m+ MongoDB Shell Extensions (0.2.4 ) by Gabriele Lana <gabriele.lana@gmail.com>\033[0m' )
10247+ chatty ( '\033[1;32m+ MongoDB Shell Extensions (0.2.5 ) by Gabriele Lana <gabriele.lana@gmail.com>\033[0m' )
1024810248
1024910249DBCollection . prototype . last = function ( n ) {
1025010250 return this . find ( ) . sort ( { _id : - 1 } ) . limit ( n || 1 )
@@ -10401,11 +10401,45 @@ DBCollection.prototype.distinctAndCount = function(field, query) {
1040110401
1040210402 var result = it . result || it . toArray ( )
1040310403 return _ . reduce ( result , function ( all , r ) {
10404- if ( ! _ . any ( r . values , isObject ) ) {
10405- all [ _ . values ( r . values ) . join ( ',' ) ] = r . count
10406- return all
10404+
10405+ var isValidValue =
10406+ _ ( r . values )
10407+ . chain ( )
10408+ . values ( )
10409+ . any ( function ( value ) {
10410+ if ( _ ( value ) . isArray ( ) ) {
10411+ return _ ( value ) . all ( function ( value ) {
10412+ return ! _ ( value ) . isObject ( )
10413+ } )
10414+ }
10415+ // we support values like Number or Date but not {}
10416+ return value . constructor . name !== ''
10417+ } )
10418+ . valueOf ( )
10419+
10420+ if ( ! isValidValue ) {
10421+ throw 'distinctAndCount could not work when one or more fields are objects: ' + tojson ( r . values )
1040710422 }
10408- throw 'distinctAndCount fields could not be objects: ' + tojson ( r . values )
10423+
10424+ var key =
10425+ _ ( r . values )
10426+ . chain ( )
10427+ . values ( )
10428+ . map ( function ( value ) {
10429+ if ( _ ( value . valueOf ) . isFunction ( ) ) {
10430+ value = value . valueOf ( )
10431+ }
10432+ if ( _ ( value ) . isArray ( ) ) {
10433+ value = _ ( value ) . sort ( ) . valueOf ( )
10434+ }
10435+ return value
10436+ } )
10437+ . valueOf ( )
10438+ . join ( ',' )
10439+
10440+ all [ key ] = ( all [ key ] || 0 ) + r . count
10441+
10442+ return all
1040910443 } , { } )
1041010444}
1041110445
0 commit comments