-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c934d78
commit 68fcc6e
Showing
2 changed files
with
47 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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)) { | ||
|