Skip to content

Commit

Permalink
support for aggregate that returns a Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielelana committed Nov 10, 2014
1 parent 2d48633 commit 8a1cad6
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/distinct_and_count.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ DBCollection.prototype.distinctAndCount = function(field, query) {
{$project: {values: '$_id', count: 1, _id: 0}}
)

if (it.ok === 1) {
return _.reduce(it.result, function(all, r) {
if (!_.any(r.values, isObject)) {
all[_.values(r.values).join(',')] = r.count
return all
}
throw 'distinctAndCount fields could not be objects: ' + tojson(r.values)
}, {})
var resultIsAnObject = (it.result !== undefined) && (it.ok !== undefined)
if (resultIsAnObject && it.ok === 0) {
return it
}
return it

var result = it.result || it.toArray()
return _.reduce(result, function(all, r) {
if (!_.any(r.values, isObject)) {
all[_.values(r.values).join(',')] = r.count
return all
}
throw 'distinctAndCount fields could not be objects: ' + tojson(r.values)
}, {})
}

0 comments on commit 8a1cad6

Please sign in to comment.