Skip to content

Commit

Permalink
docs(schematypes): add note about what values are converted to booleans
Browse files Browse the repository at this point in the history
Re: #6758
  • Loading branch information
vkarpov15 committed Jul 23, 2018
1 parent a37fd2f commit 95e553f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion docs/schematypes.jade
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ block content
- [Number](./api.html#schema-number-js)
- [Date](./api.html#schema-date-js)
- [Buffer](./api.html#schema-buffer-js)
- Boolean
- [Boolean](#booleans)
- Mixed
- [ObjectId](./api.html#schema-objectid-js)
- [Array](#arrays)
Expand Down Expand Up @@ -235,6 +235,40 @@ block content
// or just Schema.ObjectId for backwards compatibility with v2
```

<h4 id="booleans">Boolean</h4>

Booleans in Mongoose are [plain JavaScript booleans](https://www.w3schools.com/js/js_booleans.asp).
By default, Mongoose casts the below values to `true`:

* `true`
* `'true'`
* `1`
* `'1'`
* `'yes'`

Mongoose casts the below values to `false`:

* `false`
* `'false'`
* `0`
* `'0'`
* `'no'`

Any other value causes a [CastError](/docs/api.html#mongooseerror_MongooseError.CastError).
You can modify what values Mongoose converts to true or false using the
`convertToTrue` and `convertToFalse` properties, which are [JavaScript sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).

```javascript
const M = mongoose.model('Test', new Schema({ b: Boolean }));
console.log(new M({ b: 'nay' }).b); // undefined

// Set { false, 'false', 0, '0', 'no' }
console.log(mongoose.Schema.Types.Boolean.convertToFalse);

mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
console.log(new M({ b: 'nay' }).b); // true
```

<h4 id="arrays">Arrays</h4>

Mongoose supports arrays of [SchemaTypes](./api.html#schema_Schema.Types)
Expand Down

0 comments on commit 95e553f

Please sign in to comment.