查询嵌套 arry 中的日期范围不起作用



我有两个查询正在运行。 两者都查询日期范围内的文档。 第一个示例使用此方法:

db.collection('' + site_id + '_page_visits').find({created_at: {"$gte": new Date("" + date + "T00:00:00.000Z"), "$lte": new Date("" + date + "T23:59:59.999Z")}}).toArray(...

但是,我需要在第二个查询中检查的日期嵌套在call_to_action_responses内的数组中,并且对日期范围过滤器使用相同的设置不起作用,例如:

db.collection('' + site_id + '_leads').find({"call_to_action_responses.response_date": {$gte: new Date("" + date + "T00:00:00.000Z"), $lte: new Date("" + date + "T23:59:59.999Z")}, "call_to_action_responses.page_url": {$not: regex}, "call_to_action_responses.page_url": {$ne: ''} }).toArray(...

使用该代码,它仅返回与$lte匹配的所有文档。 它似乎忽略了过滤器的$gte部分,该部分在第一个示例中有效。

示例记录如下所示:

{
_id: ObjectId("XXxXXxxxxxXXxXx"),
cookie: "xxcXXXXxxx-xxxXXXx-XXXXxx",
updated_at: ISODate("2016-09-20T01:31:56.677Z"),
created_at: ISODate("2015-04-22T08:32:34.864Z"),
call_to_action_responses: [
{
response_date: ISODate("2015-04-22T08:32:34.863Z"),
page_version: "1",
template_response_path_base: "page_path",
page_url: "http://www.webiste.com/about",
page_id: ObjectId("5527d6c40de2c02a0b0000f2"),
email: "user_email_at@email.com",
lead_data: {
phone: "XXXXXXXXX",
viewed_assets: "This_Asset",
campaign_medium: "",
campaign_content: "",
first_name: "first",
last_name: "last",
campaign_source: "",
campaign_name: "",
company: "Test Company",
title: ""
},
_id: ObjectId("XXXXxxXXxXXxXXXX"),
name: "View ",
target_url: "http://www.webiste.com/about"
}
]
}

谁能看出我在这里做错了什么? 我让我知道这个语法,因为我似乎无法拨入它。 感谢您的任何帮助!

以下查询会有所帮助:

db.collection.aggregate([
{
$addFields: {
call_to_action_responses: {
$filter: {
input: "$call_to_action_responses",
as: "i",
cond: {
$and: [
{
$gte: [
"$$i.response_date",
new Date("2015-03-23T00:00:00.000Z")
]
},
{
$lte: [
"$$i.response_date",
new Date("2015-06-23T23:59:59.999Z")
]
}
]
}
}
}
}
}
])

MongoPlayGroundLink

最新更新