Socket
Socket
Sign inDemoInstall

k-mongoose-json-select

Package Overview
Dependencies
2
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    k-mongoose-json-select

A mongoose plugin to limit JSON properties and set this properties to null.


Version published
Weekly downloads
11
increased by120%
Maintainers
3
Created
Weekly downloads
 

Readme

Source

mongoose-json-select

A mongoose plugin to limit JSON properties and set the unselected fields to null.

var jsonSelect = require('k-mongoose-json-select');

var schema = Schema({
  name: String,
  email: String,
  created: {type: Date, default: Date.now}
});
schema.plugin(jsonSelect, 'name created');
var User = mongoose.model('User', schema);

var user = User({name: 'alice', email: 'alice@example.com'});
JSON.stringify(user);
// -> '{"name": "alice", "email": null , "created": "2013-03-16T16:08:38.065Z"}'

JSON.stringify(user.toJSON({select: 'name email'}));
// -> '{"name": "alice", "email": "alice@example.com", "created": null }'

Installation

$ npm install k-mongoose-json-select

Usage

Inclusion/Exclusion

// inclusion. these are equivalent
schema.plugin(jsonSelect, 'name.first');
schema.plugin(jsonSelect, {'name.first': 1});

// exclusion. these are equivalent
schema.plugin(jsonSelect, '-name.last');
schema.plugin(jsonSelect, {'name.last': 0});

Always exclude _id,id and __v field if the field is not included explicitly.

schema.plugin(jsonSelect, 'name');  // contains 'name' only

Configures default fields as a plugin option or schema option.

// these are equivalent
schema.plugin(jsonSelect, 'name');

schema.plugin(jsonSelect);
schema.set('toJSON', {select: 'name'});

Specifies fields when calling toJSON.

// this overrides a default configuration
JSON.stringify(doc.toJSON({select: 'name email'}));

The syntax for fields is the same with mongoose's Query#select.

http://mongoosejs.com/docs/api.html#query_Query-select

Support

This plugin is proudly supported by Kubide desarrollo@kubide.es

License

MIT

Keywords

FAQs

Last updated on 11 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc