【发布时间】:2019-09-16 13:36:19
【问题描述】:
我有两个模式代表我的帖子和类别文档。我正在尝试使用 category.order 属性对我的帖子进行排序。 order 属性是一个数字字段。
const postSchema = mongoose.Schema({
title: {
type: String,
max: 200,
},
body: {
type: String,
max: 10000,
},
categories: {
type: mongoose.Schema.ObjectId,
ref: 'Category',
required: true
}
})
module.exports = mongoose.model('Post', postSchema);
const categorySchema = mongoose.Schema({
name: {
type: String,
max: 30,
required:true
},
order: {
type: Number,
unique: true
},
})
module.exports = mongoose.model('Category', categorySchema);
我知道排序只适用于数字字段。我搜索了很多关于 web 和 StackOverflow 中的类似问题,甚至是 mongoose 文档。但我的查询不起作用。它让我的帖子回来了,但排序顺序不起作用。 查询:
Post.find({}).populate({path:'categories', select:'order', options:{sort:{order:1}}})
【问题讨论】:
标签: node.js mongodb mongoose aggregation-framework