如何使用日期进行查询 [英] How to make a query using dates

查看:53
本文介绍了如何使用日期进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一天内从数据库中提取所有行

I need to pull from the DB all rows in a day

var i_sDate = "2014-06-21"; // (user input)
var startDate = new Date();
var month = parseInt(i_sDate.substr(5,2)) - 1;
var day = i_sDate.substr(8,2);
startDate.setFullYear(i_sDate.substr(0,4), month, day);
startDate.setHours(0, 0, 0, 0);

var endDate = new Date();
endDate.setFullYear(i_sDate.substr(0,4), month, day);
endDate.setHours(23, 59, 59, 0);

var query = {start_time:{"$gte": "ISODate('" + startDate.toISOString() + "')", "$lt": "ISODate('" + endDate.toISOString() + "')"}};
var tableInfo = Users_Collection.find(query).fetch();

console.log(query);

当我打印查询"时,它看起来不错,但我根本没有得到任何结果,我将相同的信息直接放在数据库上并得到了预期的结果.似乎我以错误的方式构建查询,有什么建议吗????

when I print "query" it looks OK, but I don't get any result at all, I put the same information directly on the DB and I get the expected result. It seems like I'm building the query in the wrong way, any suggestion?????

提前致谢!

推荐答案

您应该在查询中直接使用 Date 对象.试试这个:

You should directly use Date objects in your query. Try this:

var query = {start_time: {$gte: startDate, $lt: endDate}};

看起来您还缺少一个结束的 }.

It also looks like you were missing a closing }.

这篇关于如何使用日期进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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