Skip to content

Commit

Permalink
Release 0.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielelana committed Dec 5, 2014
1 parent fd270c3 commit c515f26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongodb-shell-extensions",
"version": "0.2.4",
"version": "0.2.5",
"description": "Useful MongoDB shell extensions",
"files": [
"src",
Expand Down
44 changes: 39 additions & 5 deletions released/mongorc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10244,7 +10244,7 @@ moment.fn.within = function(range) {

/* global chatty */

chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.4) by Gabriele Lana <[email protected]>\033[0m')
chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.5) by Gabriele Lana <[email protected]>\033[0m')

DBCollection.prototype.last = function(n) {
return this.find().sort({_id: -1}).limit(n || 1)
Expand Down Expand Up @@ -10401,11 +10401,45 @@ DBCollection.prototype.distinctAndCount = function(field, query) {

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

var isValidValue =
_(r.values)
.chain()
.values()
.any(function(value) {
if (_(value).isArray()) {
return _(value).all(function(value) {
return !_(value).isObject()
})
}
// we support values like Number or Date but not {}
return value.constructor.name !== ''
})
.valueOf()

if (!isValidValue) {
throw 'distinctAndCount could not work when one or more fields are objects: ' + tojson(r.values)
}
throw 'distinctAndCount fields could not be objects: ' + tojson(r.values)

var key =
_(r.values)
.chain()
.values()
.map(function(value) {
if (_(value.valueOf).isFunction()) {
value = value.valueOf()
}
if (_(value).isArray()) {
value = _(value).sort().valueOf()
}
return value
})
.valueOf()
.join(',')

all[key] = (all[key] || 0) + r.count

return all
}, {})
}

Expand Down

0 comments on commit c515f26

Please sign in to comment.