Node.js 如何从mongoDb中筛选最近10天的记录?

Node.js 如何从mongoDb中筛选最近10天的记录?,node.js,mongodb,Node.js,Mongodb,您可以在过去10天内执行此操作 "date": "2017/03/10", "is_deleted": 0, "filemimeType": "audio/mp3", "emotionCount": 0, "originalname": "1489042167918-1489040146195-SampleAudio_0.5mb (1).mp3", "updatedAt": "2017-03-10T05:54:34.032Z", "isUserLiked": 0, "country": "Ind

您可以在过去10天内执行此操作

"date": "2017/03/10",
"is_deleted": 0,
"filemimeType": "audio/mp3",
"emotionCount": 0,
"originalname": "1489042167918-1489040146195-SampleAudio_0.5mb (1).mp3",
"updatedAt": "2017-03-10T05:54:34.032Z",
"isUserLiked": 0,
"country": "India",
"fileuploadFilename": "1489125273974-1489042167918-1489040146195-SampleAudio_0.5mb (1).mp3",
"_id": "58c23f9a180ac54d758b5fc0",
"likeCount": 0,
"viewed": 0
添加此代码:

today.setDate(today.getDate()-10)

在这一行之后:

var today=新日期()

然后替换代码行:

videofiles.find({date:{$lte:'today'}}

这一行:

db.videofiles.find({“date”:{“$gte”:“today”})
一行解决方案,包括:

使用
find()
方法和
createAt
属性查找过去10天的记录

const moment = require("moment")

const docs = await Doc.find({
    createdAt: {
        $gte: moment().add(-10, "days"),
    }
})

你为什么在字符串
''
中使用“今天”按哪个字段过滤?你将日期存储为字符串,这样你的概念就不起作用了。虽然有一种方法可以将日期也作为字符串进行过滤,但首选的方法是将日期作为时间戳。你能提到在字符串中进行过滤的代码以及时间戳吗?好的。但我需要过滤过去10天记录的数量,而不是最后10条记录。你能建议一个代码吗?
videofiles.find(
{
    "date": 
    {
        $gte: (new Date((new Date()).getTime() - (10 * 24 * 60 * 60 * 1000)))
    }
}
).sort({ "date": -1 })
const moment = require("moment")

const docs = await Doc.find({
    createdAt: {
        $gte: moment().add(-10, "days"),
    }
})
const moment = require("moment")
const docs = await Doc.find({
    createdAt: {
        $gte: moment().add(-10, "days"),
    }
})