我想找到昨天为止创建的所有用户。这是我制作查询字符串的代码:
var today = new Date();
var a = today.getDate();
a--;
today.setDate(a);
var yesterday = today.toDateString();
它返回类似:Sun Jan 17 2016...这是昨天的日期,但数据库以 iso 格式存储日期,例如: "date": ISODate("2016-01-13T13:23: 08.419Z")
所以我得到的是 2016-01-13T13:23:08.419Z
。我现在的问题是,我无法使用昨天的变量来查询数据库中我需要的用户,即使可以,我也不知道如何找到每个注册(不包括今天发生的注册)。
有什么帮助吗?非常感谢!
最佳答案
您在前端生成一个日期,然后将其推迟一天,这在很多情况下都完全没问题 -
为此,由于您正在尝试查找发生的数据库条目,因此可以尝试使用从数据库中每个文档的 ID 中提取的时间戳范围来查询数据库。
这里有一些关于如何从 mongoDB 执行此操作的文档。 https://docs.mongodb.org/v3.0/reference/method/ObjectId.getTimestamp/
我还提供了一些额外的资源,可以帮助您准确了解与此方法相关的查询内容:
https://steveridout.github.io/mongo-object-time/
https://gist.github.com/tebemis/0e55aa0089e928f362d9
一些伪代码:
1. Query documents in the database
2. Get a timestamp from the ID's of the documents in the database
3. Set a range of the timestamps
4. Compare returned timestamps vs a timestamp range variable (yesterdays date in this case)
5. Have the DB return only documents that are within the range
希望这会有所帮助!
关于javascript - Mongoose.js 在数据库中查找昨天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861189/