File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed
Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -9834,19 +9834,29 @@ DBQuery.prototype.tojson = function() {
98349834 return tojson ( this . toArray ( ) )
98359835}
98369836
9837+ /* global isObject: false */
9838+
98379839DBCollection . prototype . distinctAndCount = function ( field , query ) {
9840+ field = [ ] . concat ( field )
98389841 query = query || { }
98399842
9843+ var groupById = _ ( [ ] . concat ( field ) ) . reduce ( function ( result , key ) {
9844+ result [ key . replace ( '.' , '_' ) ] = '$' + key ; return result
9845+ } , { } )
9846+
98409847 var it = this . aggregate (
98419848 { $match : query } ,
9842- { $group : { _id : '$' + field , count : { $sum : 1 } } } ,
9843- { $project : { name : '$_id' , count : 1 , _id : 0 } }
9849+ { $group : { _id : groupById , count : { $sum : 1 } } } ,
9850+ { $project : { values : '$_id' , count : 1 , _id : 0 } }
98449851 )
98459852
98469853 if ( it . ok === 1 ) {
98479854 return _ . reduce ( it . result , function ( all , r ) {
9848- all [ r . name ] = r . count
9849- return all
9855+ if ( ! _ . any ( r . values , isObject ) ) {
9856+ all [ _ . values ( r . values ) . join ( ',' ) ] = r . count
9857+ return all
9858+ }
9859+ throw 'distinctAndCount fields could not be objects: ' + tojson ( r . values )
98509860 } , { } )
98519861 }
98529862 return it
You can’t perform that action at this time.
0 commit comments