mongoose-csv
generate CSVs from a mongoose schema
usage
var mongoose_csv = ;var schema = name: String ;schema;
This adds a .csv()
function, which takes a write-stream, to the mongoose.Query
, utilizing mongoose QueryStream. This should be called after query calls, e.g. .find()
, .select()
, .limit()
. This facilitates responding to an HTTP request, writing to a file, etc.
For example, a simple Express/Restify handler:
app
This is faciliated by two lower-level methods, which are exposed for extensibility:
- a static function to the schema
.csv_header()
, returning property names - an instance method on documents
.toCSV()
, returning values of a document
configuration
By default, mongoose-csv
automatically includes any property with a type that is not Array
or Mixed
(this includes the nested properties of an Object
type). Properties are returned in the order they are defined. Properties which are nested have a column name that is prefixed by their parent name and a period, e.g. nested.foo
and nested.bar
below.
If you want to prevent a property from the CSV output, annotate it with { csv : false }
. For example:
var schema = 'name' : String 'secret' : type: String csv: false 'nested' : 'foo' : String 'bar' : Date ;