mongodb怎么按照时间查询数据?

{

"_id" : ObjectId("599a97064c96a5a4d77ecc71"),
"createDate" : ISODate("2017-07-19T02:40:26.671Z")

}
{

"_id" : ObjectId("599a97064c96a5a4d77ecc71"),
"createDate" : ISODate("2017-07-20T02:40:26.671Z")

}
{

"_id" : ObjectId("599a97064c96a5a4d77ecc71"),
"createDate" : ISODate("2017-07-21T02:40:26.671Z")

}
{

"_id" : ObjectId("599a97064c96a5a4d77ecc71"),
"createDate" : ISODate("2017-07-22T02:40:26.671Z")

}
mongodb怎么按照createDate这个字段查2017-07-19日的数据?怎么查到2017-07-19 02:40的数据?怎么查到2017-07-19 至2017-07-21日的数据?

阅读 5.1k
2 个回答

给你个示例,你自己按你的需求改一下吧:
select * from table where user in ('','') and data between '' and '' order by user asc;
等价于
db.table.find({'user':{$in:["18186276128","18566165996","18358653590"]},
$and:[{"date":{$gt:ISODate("2016-01-11T15:00:00.000Z")}},
{"date":{$lt:ISODate("2016-01-14T16:00:00.000Z")}}]}).sort({"user":1});

mysql的做法
:select * from table where ISODate >'2017-07-20' and ISODate <'2017-07-22'
或者
:select * from table where ISODate between '2017-07-20' and <'2017-07-22'

如果是datetime格式的话,可以;

或者
select * from table where DATE_FORMAT(ISODate, '%Y-%m-%d') in ('2017-07-20','2017-07-21','2017-07-22')

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题