From 461bf40fa45458df9de26ec35c519b4d101cb68e Mon Sep 17 00:00:00 2001 From: "gabriele.lana" Date: Mon, 10 Nov 2014 14:24:50 +0100 Subject: [PATCH] Release 0.2.3 --- package.json | 2 +- released/mongorc.js | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index fc2dafa..2e547ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongodb-shell-extensions", - "version": "0.2.2", + "version": "0.2.3", "description": "Useful MongoDB shell extensions", "files": [ "src", diff --git a/released/mongorc.js b/released/mongorc.js index 7e72e6d..2524476 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.2) by Gabriele Lana \033[0m') +chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.3) by Gabriele Lana \033[0m') DBCollection.prototype.last = function(n) { return this.find().sort({_id: -1}).limit(n || 1) @@ -10370,7 +10370,7 @@ DBCollection.prototype.distinctAndCount = function(field, query) { query = query || {} var groupById = _([].concat(field)).reduce(function(result, key) { - result[key.replace('.', '_')] = '$' + key; return result + result[key.replace(/\./g, '_')] = '$' + key; return result }, {}) var it = this.aggregate( @@ -10379,16 +10379,19 @@ 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) + }, {}) } DBQuery.prototype.select = function(expression) {