Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-json-select

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-json-select

a mongoose plugin to limit json fields

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

mongoose-json-select

Build Status

A mongoose plugin to limit JSON properties.

var jsonSelect = require('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);
// -> '{"_id": "xxxxxxxxxx", "name": "alice", "created": "2013-03-16T16:08:38.065Z"}'

Installation

$ npm install mongoose-json-select

Documentation

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 includes _id field if the field is not excluded explicitly.

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

Configures default fields as a plugin option or schema option.

// these are equivalent
schema.plugin(jsonSelect, 'name');
schema.options.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 almost same with mongoose's Query#select.

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

Utils

select(obj, fields)

Creates a deep clone of 'obj' filtered by 'fields'.

var jsonSelect = require('mongoose-json-select');
jsonSelect.select({a: {b: 'foo'}, c: 'bar'}, 'a.b');
// -> {a: {b: 'foo'}}

License

MIT

Keywords

FAQs

Package last updated on 16 Mar 2013

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc