mongoose populate 似乎不起作用, 另人疑惑的是, 我在项目另一个地方使用,它是正常的
mongodb: 4.0.1
mongoose: 5.2.9
// dinner.model.jsconst mongoose = require('mongoose')const Schema = mongoose.Schemaconst DinnerSchema = new Schema({ name: { type: String }, content: { type: [{ type: Schema.Types.ObjectId, ref: 'Food' }] } })const Dinner = mongoose.model('Dinner', DinnerSchema, 'dinners')module.exports = Dinner
// food.model.jsconst mongoose = require('mongoose')const Schema = mongoose.Schemaconst FoodSchema = new Schema({ name: { type: String } })const Food = mongoose.model('Food', FoodSchema, 'foods')module.exports = Food
router.get('/test', async (ctx, next) => { const result = await DinnerModel.find().populate({ path: "foods" }) ctx.result = { code: 200, data: { list: result, msg: 'success' } } await next() })
明月笑刀无情
相关分类