node.js - Mongoose,如何对集合中选定对象的日期进行排序
问题描述
我有这个猫鼬模式,我想根据它们的 createdAt 字段对菜单项进行排序
user:
...
menus: [
{
name: { type: String },
recipes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Recipe',
},
],
image: {
url: { type: String },
type: {
type: String,
enum: avatarTypes,
}
},
createdAt: {
type: Date,
required: true,
default: Date.now
}
},
],
在nodeJs中,当我获取它们时,我想根据菜单中的createdAt对它们进行排序(最新的优先)
这是我的代码,但它不起作用
const menus = await User.findOne({
_id: id,
}).sort('-menus.createdAt')
解决方案
推荐阅读
- python - 如何将包含在某些不可迭代对象中的列表中的列表展开为平面?
- c++ - C++11 的 std::thread 是否与 POSIX 信号量兼容?
- typescript - TypeError:v1.isEqual 不是函数
- apache-kafka - 抵消与运行消费者的保留
- python - Python 定义基于输入名称创建变量的函数
- go - 不能在赋值中使用 strings.NewReplacer("#", "o") (type *strings.Replacer) 作为类型 strings.Replacer
- python - 带有标头的 Python Url API 请求 - 不断获取会话过期无法查看 Json 数据
- php - 将js字符串化字符串转换为php中的多维数组
- android - 将应用发布到 Google Play 商店并且 API 调用不起作用
- python-3.x - 如何在多个类中实现行为?