몽구스 스키마의 특정 필드를 항상 populate()
여기서 문서를 읽으십시오.
참고 : 인구는 강력한 기능이지만 한계가 있으며 스키마 설계가 열악한 상태에서 벗어나도록 도와줍니다. 특히, 문서에 묶이지 않고 성장하는 배열을 포함하는 것은 일반적으로 잘못된 MongoDB 스키마 설계입니다. 스키마에 끊임없이 성장하는 객체 배열을 포함하지 마십시오. 배열이 커지면서 데이터가 다루기 어려워지고 결국 16MB 문서 크기 제한에 도달합니다. 일반적으로 스키마를 설계 할 때 신중하게 생각하십시오.
mongoose-autopopulate
모듈은 Mongoose Schema의 plugin()
함수로 전달할 수있는 단일 함수를 노출시킵니다.
const schema = new mongoose . Schema ( {
populatedField : {
type : mongoose . Schema . Types . ObjectId ,
ref : 'ForeignModel' ,
// The below option tells this plugin to always call `populate()` on
// `populatedField`
autopopulate : true
}
} ) ;
schema . plugin ( require ( 'mongoose-autopopulate' ) ) ;
이 플러그인을 최상위 스키마에만 적용하십시오. 이 플러그인을 하위 스키마에 적용하지 마십시오.
// Don't do `nestedSchema.plugin(require('mongoose-autopopulate'))`.
// You only need to add mongoose-autopopulate to top-level schemas.
const nestedSchema = mongoose . Schema ( {
child : { type : Number , ref : 'Child' , autopopulate : true }
} ) ;
const topSchema = mongoose . Schema ( { nested : nestedSchema } ) ;
topSchema . plugin ( require ( 'mongoose-autopopulate' ) ) ;