MongoDB:仅提取过去24小时内创建的文档吗? [英] MongoDB: Only fetch documents created in the last 24hrs?

查看:52
本文介绍了MongoDB:仅提取过去24小时内创建的文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将查询范围限制为仅查看过去24小时内创建的文档.

I want to limit a query I'm making to only look in documents that were created in the past 24 hrs.

构造此查询的最佳方法是什么?如何根据日期进行限制?

What is the best way to structure this query? How do I go about limiting based on date?

推荐答案

添加createdAt字段,对其进行索引,然后查询

Add createdAt field, index it, then query

db.getCollection("COLLECTION_NAME").find({"createdAt":{$gt:new Date(Date.now() - 24*60*60 * 1000)}})

这将返回所有早于86400秒的记录.

This will return all records older then 86400 seconds.

这篇关于MongoDB:仅提取过去24小时内创建的文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆