问题描述
我有这个模型:
const HistorySchema = new Schema({
belongs_to: {
type: Schema.Types.ObjectId,refPath: "onModel",},onModel: {
type: String,enum: ["videos","help_requests"],date: {
type: Date,default: Date.Now,});
我想使用聚合来填充 belongs_to
字段。问题是,对于 aggregate
,我必须指定 belongs_to
正在引用哪个模型:
const populate_belongs_to_field_option = {
$lookup: {
// Here I should specify the model
// But,it can be any of the model in the field
// onModel
// This returns an empty array
// Which makes sense since onModel is not a model
from: "onModel",localField: "belongs_to",foreignField: "_id",as: "belongs_to",};
const pipeline = [
populate_belongs_to_field_option,];
const history_events_with_aggregate = await History.aggregate(pipeline);
但 belongs_to
可以引用 "videos"
或 "help_requests"
。所以我不确定如何进行这项工作。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)