나는 가지가 있고 모든 가지에는 메뉴가 있고,이 메뉴에는 모든 가지에이 지점에 대한 가격이 지정된 항목이 있습니다. 몽구스의 복잡한 관계
내 스키마는 다음과 같습니다var mongoose = require('mongoose');
var eaterySchema = mongoose.Schema({
name: String,
type: Number,
branches: [{type: Schema.Types.ObjectId, ref: 'Branch' }]
});
var branchSchema = mongoose.Schema({
id: Number,
name: String,
eatery: {type: Schema.Types.ObjectId, ref: 'Eatery'},
menu: {type: Schema.Types.ObjectId, ref: 'Menu'}
});
var menuSchema = mongoose.Schema({
branch: {type: Schema.Types.ObjectId, ref: 'Branch'},
items: [{type: Schema.Types.ObjectId, ref: 'Item'}]
});
var itemSchema = mongoose.Schema({
name: String,
description: String
});
var itemMenuSchema = mongoose.Schema({ //Many-To-Many Table
menu: {type: Schema.Types.ObjectId, ref: 'Menu'},
item: {type: Schema.Types.ObjectId, ref: 'Item'},
price: Number
});
var Eatery = mongoose.model('Eatery', eaterySchema);
var Branch = mongoose.model('Branch', branchSchema);
var Menu = mongoose.model('Menu', menuSchema);
var Item = mongoose.model('Item', itemSchema);
var ItemMenu = mongoose.model('ItemMenu', itemMenuSchema);
합니까 항목 및 요청 지점 ID에 대한 항목 가격과 메뉴를 얻기 위해 서면 바로 아래의 쿼리?
Branch
.findOne({id: '@GivenId'})
.populate('eatery menu')
.exec(function(err, doc){
doc
.populate('menu.items')
.exec(function(err, doc){
ItemMenu
.find({})
.where('menu._id').equals(doc.menu._id)
.exec(function(err, items){
doc.push(items);
res.send(docs);
})
})
};)
});
그리고 mongoose는 마지막 콜백에서 작성한 마지막 쿼리를 처리하기 위해 채우기를 제공합니까? 그리고이 복잡한 관계를 다루는 더 좋은 해결책은 무엇입니까?
덕분에 많은
고마워요. mongoose가 mongoDB를 객체로 처리하기 위해 작성되었으므로 관계형으로 이것을 처리했습니다. 위에 나와있는이 예제의 스키마를 작성하는 방법은 무엇입니까? –
고마워, 너는 나의 하루를 구했다 :) 대답을하기 전에. 그리고 그 후 100 개의 감사;) –