![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/cgZeLE/btqCyl8ZjrY/4E6zMYruu3wJdccvH9PCOk/img.png)
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 =..
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/dmp30L/btqCxPbqKjX/fsB08UX59FgpOT5zcGa7rk/img.png)
virtuals는 MongoDB에 실재하지 않는 access property(getter나 setter)다. getter는 formatting이나 combining 하는데 유용하고 setter는 하나의 값을 여러개의 값으로 분해하는데 유용하다. Axl Rose라는 사람이 있다고 하면 // define a schema var personSchema = new Schema({ name: { first: String, last: String } }); // compile our model var Person = mongoose.model('Person', personSchema); // create a document var axl = new Person({ name: { first: 'Axl', last: ..
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/nbgms/btqCDlF5s9s/61ABzKlqH7fLOjBIODUV7K/img.png)
Mongoose에서 인덱스를 선언하는 방법은 2가지가 있다. path level(또는 field level)에서 정의하는 법과 schema level에서 정의하는 법. 스키마를 정의하면서 field level에서 정의할 수 있고, schema정의가 끝나고 index() 메서드를 통해서 정의할 수도 있다. compound index를 만드려면 index() 메서드를 사용해야 한다. const animalSchema = new Schema({ name: String, type: String, tags: { type: [String], index: true } // field level }); animalSchema.index({ name: 1, type: -1 }); // schema level const ..
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/bRtlCP/btqCBxNJ7V3/QU1qL0Y9YgxaL07CoYk2Pk/img.png)
Query Helper는 instance method와 유사하지만 mongoose queries에만 쓰이는 helper function이다. Query Helper는 말 그대로 mongoose의 chainable 한 mongoose의 query builder API를 확장시켜주는 method다. 스키마를 정의. const animalSchema = new Schema({ name: String, breed: String }); query property에 query helper를 정의후 컴파일. animalSchema.query.byName = function (name) { return this.where({ name: new RegExp(name, 'i') }); } const Animal = mon..
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/bmzNYD/btqCAO92hm2/88WD5CT5NWyIG7Cu2Rj2r0/img.png)
Model에는 instance method 말고도 static functions가 있다. 잠깐 설명을 하자면 javascript 클래스에는 static이라는 개념이 있다. 일반적으로 method를 클래스에 선언하면 그 method의 this는 인스턴스에 바인딩된다. static method에서 this는 클래스 그 자체에 바인딩된다. schema 정의. const animalSchema = new Schema({ name: String, breed: String }) static method 정의. // Assign a function to the "statics" object of our animalSchema animalSchema.statics.findByName = function (name) {..
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/brkEGg/btqCym0WMRc/MibrOVScztd0IizR1Q6NIk/img.png)
documents는 Model 클래스에 instance들이다. 저장한 data 이외에도 많은 built-in-instance methods를 가지고 있다. 물론 우리가 custom methods를 만들 수도 있다. schema 인스턴스에 methods 프로퍼티에 findSimilarTypes라는 instance method를 정의해 준다. const animalSchema = new Schema({ name: String, type: String, breed: String }); // overwritting a default mongoose doucment method may lead to unpredictable results. animalSchema.methods.findSimilarTypes = f..
- Total
- Today
- Yesterday
- 자료구조
- Node.js
- Iterator
- pm2 #cluster #Javascript
- index
- 이중 연결리스트
- resave
- query helper
- set
- instance method
- saveUnitialized
- Generator
- MongoDB
- mixin
- virtuals
- express-session
- 자료구조 #딕셔너리 #해시
- mongoose
- 맵
- javascript
- alias
- 선형리스트
- map
- 다형성
- static method
- 집합
- 다중상속
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |