Skip to content

Commit 8a1cad6

Browse files
committed
support for aggregate that returns a Cursor
1 parent 2d48633 commit 8a1cad6

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/distinct_and_count.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ DBCollection.prototype.distinctAndCount = function(field, query) {
1414
{$project: {values: '$_id', count: 1, _id: 0}}
1515
)
1616

17-
if (it.ok === 1) {
18-
return _.reduce(it.result, function(all, r) {
19-
if (!_.any(r.values, isObject)) {
20-
all[_.values(r.values).join(',')] = r.count
21-
return all
22-
}
23-
throw 'distinctAndCount fields could not be objects: ' + tojson(r.values)
24-
}, {})
17+
var resultIsAnObject = (it.result !== undefined) && (it.ok !== undefined)
18+
if (resultIsAnObject && it.ok === 0) {
19+
return it
2520
}
26-
return it
21+
22+
var result = it.result || it.toArray()
23+
return _.reduce(result, function(all, r) {
24+
if (!_.any(r.values, isObject)) {
25+
all[_.values(r.values).join(',')] = r.count
26+
return all
27+
}
28+
throw 'distinctAndCount fields could not be objects: ' + tojson(r.values)
29+
}, {})
2730
}

0 commit comments

Comments
 (0)