Skip to content

Commit

Permalink
Release 0.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielelana committed Dec 1, 2014
1 parent c934d78 commit 68fcc6e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 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.3",
"version": "0.2.4",
"description": "Useful MongoDB shell extensions",
"files": [
"src",
Expand Down
77 changes: 46 additions & 31 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.3) by Gabriele Lana <[email protected]>\033[0m')
chatty('\033[1;32m+ MongoDB Shell Extensions (0.2.4) 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 @@ -10303,38 +10303,53 @@ var printcsv = function(x) {
})
}

var tocsv = function(x) {
var lines = [],
fieldNames = {},
encodedDocuments = x.map(function(doc) {
return _.reduce(doc, function(values, value, field) {
fieldNames[field] = true
values[field] = tojson(value).replace(
/^(?:ISODate|ObjectId)\((.*)\)$/,
function(_, contentAsString) {
return contentAsString
}
)
return values
}, {})
})

fieldNames = _.keys(fieldNames)
var tocsv = (function() {
var flatten = function(o) {
return _.reduce(o, function(flattened, value, field) {
if (_.isPlainObject(value)) {
_.forEach(flatten(value), function(nestedValue, nestedField) {
flattened[[field, nestedField].join('.')] = nestedValue
})
} else {
flattened[field] = value
}
return flattened
}, {})
}

lines.push(fieldNames.join(','))
encodedDocuments.forEach(function(encodedDocument) {
lines.push(
fieldNames.map(function(fieldName) {
if (encodedDocument[fieldName] !== undefined) {
return encodedDocument[fieldName]
}
return '""'
}).join(',')
)
})
return function(x) {
var lines = [],
fieldNames = {},
encodedDocuments = x.map(function(doc) {
return _.reduce(flatten(doc), function(values, value, field) {
fieldNames[field] = true
values[field] = tojson(value).replace(
/^(?:ISODate|ObjectId)\((.*)\)$/,
function(_, contentAsString) {
return contentAsString
}
)
return values
}, {})
})

fieldNames = _.keys(fieldNames)

lines.push(fieldNames.join(','))
encodedDocuments.forEach(function(encodedDocument) {
lines.push(
fieldNames.map(function(fieldName) {
if (encodedDocument[fieldName] !== undefined) {
return encodedDocument[fieldName]
}
return ''
}).join(',')
)
})

return new CSV(lines)
}
return new CSV(lines)
}
})()

DBQuery.prototype.reverse = function() {
if (!this._query.query || _.isEmpty(this._query.query)) {
Expand Down

0 comments on commit 68fcc6e

Please sign in to comment.