티스토리 뷰
alias는 특별한 virtual 타입이다.
alias는 network 대역폭을 절약하는데 도움이 된다.
실제 DB에는 짧은 프로퍼티 이름을 저장하고 실제 코드에서는 긴 프로퍼티 이름을 사용할 수 있다.
const personSchema = new Schema({
n: {
type: String,
// Now accessing 'name' will get you the value of 'n',
// and setting 'n' will set the value of 'name'
alias: 'name'
}
})
const Person = mongoose.model('Person', personSchema);
// Setting 'name' will propagate to 'n'
const person = new Person({ name: 'Jack' });
console.log(person.name); // 'Jack'
virtuals: true로 하면 볼 수 있다.
person.name = 'Darius'
console.log(person.toObject({ virtuals: true }));
// {
// _id: 5e661c4e6f72000c481cf5d1,
// n: 'Darius',
// name: 'Darius',
// id: '5e661c4e6f72000c481cf5d1'
// }
nested path alias는 nested.myProp꼴로 alias를 설정해주면 된다.
// You can also declare on nested paths.
const childSchema = new Schema({
n: {
type: String,
alias: 'name'
}
}, { _id: false });
const parentSchema = new Schema({
// If in a child schema, alias doesn't need to include the full nested path
c: childSchema,
name: {
f: {
type: String,
// Alias needs to include the full nested path if declared inline
alias: 'name.first'
}
}
})
const Parent = mongoose.model('Parent', parentSchema);
"name.first"처럼 완벽한 nested path를 가져야 한다. child schema로 schema를 구성하면 "c.name"같은 nested path로는 못 쓰고 {c:{ name: value }}형태로 사용할 수 있다.
const parent = new Parent({ name: { first: 'Ashe' } });
const parent2 = new Parent({ "c.name": "trindamier" });
const parent3 = new Parent({ c: { name: "trindamier" } });
console.log(parent);
// { name: { f: 'Ashe' }, _id: 5e65d66d26b6df3154762b88 }
console.log(parent2);
// { _id: 5e65d66d26b6df3154762b89, c: { n: 'trindamier' } }
console.log(parent3);
// { _id: 5e661ff8a16f203a4856345b, c: { n: 'trindamier' } }
'Mongoose > schemas' 카테고리의 다른 글
Virtuals (0) | 2020.03.09 |
---|---|
Indexes (0) | 2020.03.09 |
Query Helpers (0) | 2020.03.09 |
Statics (0) | 2020.03.09 |
Instance method (0) | 2020.03.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 선형리스트
- 자료구조
- virtuals
- instance method
- 이중 연결리스트
- alias
- javascript
- static method
- query helper
- Generator
- 다형성
- saveUnitialized
- Iterator
- 자료구조 #딕셔너리 #해시
- 집합
- map
- Node.js
- mixin
- express-session
- 다중상속
- resave
- pm2 #cluster #Javascript
- 맵
- MongoDB
- mongoose
- index
- set
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함