Skip to content

Commit b01d955

Browse files
committed
docs: add description of alias option
Fix #5287, re: #5184
1 parent efb017f commit b01d955

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

docs/guide.jade

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,29 @@ block content
250250
:markdown
251251
Virtual property setters are applied before other validation. So the example above would still work even if the `first` and `last` name fields were required.
252252

253-
Only non-virtual properties work as part of queries and for field selection.
253+
Only non-virtual properties work as part of queries and for field selection. Since virtuals are not stored in MongoDB, you can't query with them.
254+
255+
h5#aliases Aliases
256+
:markdown
257+
Aliases are a particular type of virtual where the getter and setter seamlessly get and set another property. This is handy for saving network bandwidth, so you can convert a short property name stored in the database into a longer name for code readability.
258+
259+
:js
260+
var personSchema = new Schema({
261+
n: {
262+
type: String,
263+
// Now accessing `name` will get you the value of `n`, and setting `n` will set the value of `name`
264+
alias: 'name'
265+
}
266+
});
267+
268+
// Setting `name` will propagate to `n`
269+
var person = new Person({ name: 'Val' });
270+
console.log(person); // { n: 'Val' }
271+
console.log(person.toObject({ virtuals: true })); // { n: 'Val', name: 'Val' }
272+
console.log(person.name); // "Val"
273+
274+
person.name = 'Not Val';
275+
console.log(person); // { n: 'Not Val' }
254276

255277
h3#options Options
256278
:markdown

docs/schematypes.jade

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ block content
112112
* `validate: function, adds a [validator function](http://mongoosejs.com/docs/validation.html#built-in-validators) for this property
113113
* `get`: function, defines a custom getter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
114114
* `set`: function, defines a custom setter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
115+
* `alias`: string, mongoose >= 4.10.0 only. Defines a [virtual](http://mongoosejs.com/docs/guide.html#virtuals) with the given name that gets/sets this path.
115116

116117
:js
117118
var numberSchema = new Schema({
118119
integerOnly: {
119120
type: Number,
120121
get: v => Math.round(v),
121-
set: v => Math.round(v)
122+
set: v => Math.round(v),
123+
alias: 'i'
122124
}
123125
});
124126

@@ -127,6 +129,10 @@ block content
127129
var doc = new Number();
128130
doc.integerOnly = 2.001;
129131
doc.integerOnly; // 2
132+
doc.i; // 2
133+
doc.i = 3.001;
134+
doc.integerOnly; // 3
135+
doc.i; // 3
130136

131137
h5 Indexes
132138
p

0 commit comments

Comments
 (0)