0
내 node.js 표현형 앱에 스키마가 있어야합니다 (사용자 및 배달). 내가 배달 스키마의 address
필드에서 사용자의 addresses
하위 문서를 참조 할 수 없습니다몽구스 : 참조 된 하위 문서에 연결된 인구
var User = new Schema({
name: {type: String},
addresses: [
{
city: {type: String},
address: {type: String},
}
],
});
, 그래서 address
필드 저장 하위 문서의 _id
.
var Delivery = new Schema({
time: {type: String},
address: {type: String} // this field references to User schema's address subdocument
});
그래서 어떻게 Delivery
스키마에 address
필드를 채울 수 있습니까?
Delivery.findOne(/*here is condition*/).populate({path: 'address', model: 'User.addresses'});
그것은 작동하지 않습니다이 간단한 해결 오류없이, 널 (null)이된다. 각 스키마는'user.js'와'delivery.js'라는 별개의 파일에 저장됩니다. – user3215609
@ user3215609 MongoDB에는 조인이 없으므로 수동으로 다른 모음의 데이터를로드해야합니다. 또는 주소를 전용 컬렉션에 보관하여 참조 할 수 있습니다. – oaleynik