【发布时间】:2016-03-22 17:01:11
【问题描述】:
假设我有以下两种模式:
var SchemaOne = new mongoose.Schema({
created_at: { type: Date },
schemaTwo: { type: mongoose.Schema.Types.ObjectId, ref: 'SchemaTwo' },
ancestor: { type: mongoose.Schema.Types.ObjectId, ref: 'SchemaOne' }
});
var SchemaTwo = new mongoose.Schema({
headline: { type: String, required: true }
});
我想做的是:对于每个与提供的具有相同祖先的SchemaOne文档,返回与它们相关联的SchemaTwo的标题(如果有的话),考虑到结果来自查询不应返回任何重复项,并且应限制为按SchemaOne 的created_at 字段的降序排序的15 个结果。
我开始做以下事情:
SchemaOne
.find({ 'ancestor': ancestor })
.sort({ 'created_at': -1 })
.populate({ path: 'schemaTwo', select: 'headline', options: { limit: 15 } })
.exec(function(err, docs) {
// do something with the results
});
但是这样做,我仍然会得到重复的结果,即,我将有多个 SchemaOne 文档与同一个 SchemaTwo 文档相关联。
你能帮我解决这个问题吗?
【问题讨论】: