参考https://mongoosejs.com/docs/populate.html#checking-populated给出的 Mongoose url populate 示例,两个 Schema 之间似乎存在双向关系。例如,如果我只有一种方式的关系怎么办(使用相同的模式示例,但 Person 模式没有Story ref )
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const personSchema = Schema({
name: String,
age: Number
});
const storySchema = Schema({
author: { type: Schema.Types.ObjectId, ref: 'Person' },
title: String
});
const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);
如何返回如下所示的 GET Story 输出:
{
author :{
name: "Bla bla bla",
age: 30
}
title : "ABC Story"
}
我现在总是得到这个:
{
author :34235245453
title : "ABC Story"
}