npm install mongoose-schema-paths-getter --save
yarn add mongoose-schema-paths-getter
const schemaPathsGetter = require('mongoose-schema-paths-getter');
const BookSchema = new mongoose.Schema({
title : String,
author : String,
ISBN : String,
price : Number
});
BookSchema.plugin(schemaPathsGetter);
const Book = mongoose.model('Book', BookSchema);
const book = new Book({/* book data */});
book.getPaths(); // => ['title', 'author', 'ISBN', 'price'] *** order is not guaranteed ***
Exclude the unwanted paths
BookSchema.plugin(schemaPathsGetter, {
exclude : ['ISBN'] // or just 'ISBN'
});
book.getPaths(); // => ['title', 'author', 'price']
default : getPaths
Specify the method name
BookSchema.plugin(schemaPathsGetter, {
method : 'getProperties'
});
book.getProperties(); // => ['title', 'author', 'ISBN', 'price']
yarn install
yarn test