【发布时间】:2018-12-24 05:59:28
【问题描述】:
我在尝试填充标签和类别字段时遇到了这种有线行为。
这里是文章模型的一部分
tags: {
type: [mongoose.Schema.Types.ObjectId],
index: true,
ref: 'tag'
},
categories: {
type: [mongoose.Schema.Types.ObjectId],
index: true,
ref: 'category'
}
通过尝试填充这三个字段仅作者作品,因为它不是对象数组
.populate('author')
.populate('tags')
.populate('categgories')
查看 mongoose 调试结果,注意 mongoose 调用文章模型而不是标签来填充标签等类别,因此填充不起作用。
Mongoose: users.find({ _id: { '$in': [ ObjectId("5b83eacb51e2d33dd5c057ad") ] } }, { fields: { name: 1 } })
Mongoose: articles.find({ _id: { '$in': [ ObjectId("5b83eac851e2d33dd5c057ab") ] } }, { fields: { _id: 1 } })
Mongoose: articles.find({ _id: { '$in': [ ObjectId("5b83eac951e2d33dd5c057ac") ] } }, { fields: {} })
【问题讨论】:
-
为什么猫鼬叫articles.find而不是tags.find?