반응형
Mongoose에서 다른 스키마 참조
다음과 같은 두 개의 스키마가있는 경우 :
var userSchema = new Schema({
twittername: String,
twitterID: Number,
displayName: String,
profilePic: String,
});
var User = mongoose.model('User')
var postSchema = new Schema({
name: String,
postedBy: User, //User Model Type
dateCreated: Date,
comments: [{body:"string", by: mongoose.Schema.Types.ObjectId}],
});
위의 예처럼 연결하려고했지만 어떻게해야할지 모르겠습니다. 결국 내가 이런 일을 할 수 있다면 내 삶이 아주 편해질거야
var profilePic = Post.postedBy.profilePic
채우기 방법이 당신이 찾고있는 것 같습니다. 먼저 게시물 스키마를 약간 변경합니다.
var postSchema = new Schema({
name: String,
postedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
dateCreated: Date,
comments: [{body:"string", by: mongoose.Schema.Types.ObjectId}],
});
그런 다음 모델을 만드십시오.
var Post = mongoose.model('Post', postSchema);
그런 다음 쿼리를 만들 때 다음과 같이 참조를 채울 수 있습니다.
Post.findOne({_id: 123})
.populate('postedBy')
.exec(function(err, post) {
// do stuff with post
});
부록 : "Populate"를 언급 한 사람은 아무도 없습니다. --- Mongooses Populate Method를 살펴 보는 것은 시간과 돈의 가치가 매우 높습니다.
http://mongoosejs.com/docs/populate.html
참고 URL : https://stackoverflow.com/questions/18001478/referencing-another-schema-in-mongoose
반응형
'Programing' 카테고리의 다른 글
두 개의 열을 기반으로 두 개의 데이터 프레임을 결합하는 방법은 무엇입니까? (0) | 2020.08.28 |
---|---|
자바 스크립트에서 함수를 오버로드하는 방법은 무엇입니까? (0) | 2020.08.28 |
대규모 C ++ 프로젝트에서 불필요한 #include 파일을 어떻게 감지해야합니까? (0) | 2020.08.28 |
구아바 라이브러리는 Maven 저장소에서 사용할 수 있습니까? (0) | 2020.08.28 |
"!"의 목적은 무엇입니까? (0) | 2020.08.28 |