node.js - 如何将排序功能与 $or 和 $and 一起使用?
问题描述
我必须根据一些限制从 MongoDB 获取数据,并且它应该按日期和时间排序。我试过下面的代码没有运气。
var startDate = new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() - 365);
collections.find({
$and: [{
$or: [{
"times.0.date": {
$gte: endDate,
$lte: startDate
}
}, {
"times.date": {
$gte: endDate,
$lte: startDate
}
}]
}, {
"user_id": parseInt(user._id)
}],
$sort: [{startDate: -1}]
})
解决方案
你可以先找到你的结果,然后用
collections.find({
$and: [{
$or: [{
"times.0.date": {
$gte: endDate,
$lte: startDate
}
}, {
"times.date": {
$gte: endDate,
$lte: startDate
}
}]
}, {
"user_id": parseInt(user._id)
}]
}).sort({ "times.date": -1 })
推荐阅读
- mysql - 为什么以下互惠查询具有不同的性能?
- macos - 在 MAC OS 10.14.5 上修复 m4
- java - 无法使用 FUNC() 解决 JPQL :parameter 问题
- sql - 根据存储的值乘以可能的选项返回多行
- php - 使用 xPath 从 html 创建节标题元素的编号有序列表
- angular - Angular Routes 不适用于 cloudfront + s3
- python - Python Spark - 如何在数据帧上创建一个对现有列进行切片的新列?
- google-api - 使用适用于 .NET Core 的 Google.Apis.Core,当我指定空格 =“照片”时,为什么在我的 Google 照片中看不到照片列表?
- javascript - 每次 webpack 更新后必须重新连接游戏手柄
- javascript - 链表替换和删除