【发布时间】:2016-05-25 21:43:58
【问题描述】:
我的文档具有数组属性timestamps,如下面的文档所示。我希望找到两个日期之间时间戳的所有文档。我的查找查询如下所示:
$subset = $db->col->find(
array(
'timestamps'=> array(
'$elemMatch'=> array(
'$gte'=> new MongoDate(strtotime('2016-05-25 13:00:00')),
'$lte'=> new MongoDate(strtotime('2016-05-25 14:00:00'))
)
)
)
);
但它没有返回预期的文档。我尝试在 Google 和 StackOverflow 上搜索答案,但找不到任何解释如何在两个日期之间搜索数组中的时间戳的内容。
编辑:
问题似乎在于 PHP,因为相同的 Python 查询有效:
import datetime
f = '%Y-%m-%d %H:%M:%S'
c = db.col.find({
'timestamps': {
'$elemMatch': {
'$gte': datetime.datetime.strptime('2016-05-25 13:00:00', f),
'$lte': datetime.datetime.strptime('2016-05-25 14:00:00', f)
}
}
})
任何帮助将不胜感激。
{
"month": {
"$date": 1464134400000
},
"timestamps": [
{
"$date": 1464181803000
},
{
"$date": 1464182103000
},
{
"$date": 1464182403000
},
{
"$date": 1464183003000
},
{
"$date": 1464183603000
},
{
"$date": 1464184203000
},
{
"$date": 1464184803000
},
{
"$date": 1464185403000
},
{
"$date": 1464186003000
}
],
"status_history": [
1,
1,
1,
0,
1,
1,
1,
0,
1
],
"_id": 12345
}
我查看了find in array between dates in mongo 和Get data between two dates mongo。
【问题讨论】: