From c515f2608b17182c4af22e74cb9cebbb0900bcd6 Mon Sep 17 00:00:00 2001 From: "gabriele.lana" Date: Fri, 5 Dec 2014 17:20:55 +0100 Subject: [PATCH] Release 0.2.5 --- package.json | 2 +- released/mongorc.js | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 949f847..617f2f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongodb-shell-extensions", - "version": "0.2.4", + "version": "0.2.5", "description": "Useful MongoDB shell extensions", "files": [ "src", diff --git a/released/mongorc.js b/released/mongorc.js index 51eee60..878d5d2 100644 --- a/released/mongorc.js +++ b/released/mongorc.js @@ -10244,7 +10244,7 @@ moment.fn.within = function(range) { /* global chatty */ -chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.4) by Gabriele Lana \033[0m') +chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.5) by Gabriele Lana \033[0m') DBCollection.prototype.last = function(n) { return this.find().sort({_id: -1}).limit(n || 1) @@ -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 }, {}) }