【发布时间】:2020-12-01 23:15:03
【问题描述】:
我正在尝试在 2020-11-25T00:00:00.000Z 之前在 mongodb 中查找记录
每条记录都有一个crawls 数组,其中存储了每次抓取的日期。
现在这个查询返回[]
如何正确查询数据库以获得预期的记录?
//record in the db
"_id": "a137f2c46f4611ecd83e5a36bda66813b79f057c",
"company_name": "test",
"updatedAt": {
"$date": "2020-11-29T19:56:08.507Z"
},
"flagged": true,
"crawling": false,
"crawls": [{
"$date": "2020-12-01T22:32:37.575Z"
}, {
"$date": "2020-12-01T22:38:43.826Z"
}, {
"$date": "2020-12-01T22:39:39.702Z"
}, {
"$date": "2020-12-05T22:32:37.575Z"
}]
//my query
let res = await mongo.db('_businesses').collection('businesses').find(
{
crawls: {
$elemMatch: {
$lte: '2020-11-25T00:00:00.000Z'
}
}
}
)
//res = []
【问题讨论】:
-
这可能需要一个格式。你试过
$lte:{$toDate:"2020-11-25T00:00:00.000Z"} -
要清楚,
$date是一个对象类型,与$oid相同,所以它们不能与字符串进行比较(好吧,你会得到 false、null ......)。希望以后能帮到你