请原谅,我是MongoDB聚合框架的新手。我有以下带有“历史记录”作为嵌套文档的集合,并且我试图将匹配的集合与历史记录的完整列表一起投影,但是我的查询仅返回嵌套历史记录中的匹配元素。
$.a[0].value
这是我的查询
{
"_id" : ObjectId("5cc75fbcd1de1e96e0925a7e"),
"case_id" : "5002J00001E2JheQAF",
"__v" : 0,
"account_id" : "0015000000xxYAWAA2",
"account_name" : "HelloWorld",
"caseNumber" : "C1000013634",
"caseOrigin" : "Email",
"case_type" : "A-Partner",
"create_date" : ISODate("2019-04-29T20:15:48.000Z"),
"current_mrr" : null,
"escalated_team" : null,
"history" : [
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a85"),
"history_id" : "0172J0000tXXzkuQAD",
"field" : "created",
"new_value" : null,
"old_value" : null,
"created_date" : ISODate("2019-04-29T20:15:52.000Z"),
"created_by_id" : "005500000071WBzAAM"
},
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a84"),
"history_id" : "0172J0000tXXzkyQAD",
"field" : "Provider_Group__c",
"new_value" : "HelloWorld Provisioning",
"old_value" : "SGS - Web and Chat",
"created_date" : ISODate("2019-04-29T20:15:52.000Z"),
"created_by_id" : "005500000071WBzAAM"
},
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a7d"),
"history_id" : "0172J0000tYzZgoQAF",
"field" : "Status",
"new_value" : "In Progress",
"old_value" : "Pending Customer Response",
"created_date" : ISODate("2019-04-30T13:38:38.000Z"),
"created_by_id" : "005500000071WBzAAM"
},
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a7b"),
"history_id" : "0172J0000tYzbgJQAR",
"field" : "Status",
"new_value" : "Resolved",
"old_value" : "In Progress",
"created_date" : ISODate("2019-04-30T13:54:44.000Z"),
"created_by_id" : "0052J000007nM11QAE"
}
],
"feeds" : [
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a78"),
"feed_id" : "0D52J00006qBd5PSAS",
"event" : "TextPost",
"created_by_id" : "0052J000007nM11QAE",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-30T13:54:42.000Z")
},
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a77"),
"feed_id" : "0D52J00006qBd4kSAC",
"event" : "EmailMessageEvent",
"created_by_id" : "0052J000007nM11QAE",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-30T13:54:32.000Z")
},
{
"_id" : ObjectId("5cdad34e4520d570fc0f6a76"),
"feed_id" : "0D52J00006qBc4GSAS",
"event" : "EmailMessageEvent",
"created_by_id" : "005500000071WBzAAM",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-30T13:38:34.000Z")
}
],
"issue_level" : "HelloWorld",
"last_month_service_mrr" : 572.866,
"last_update" : ISODate("2019-05-14T14:00:39.000Z"),
"owner" : "Ajay AJAY",
"owner_id" : "0052J000007nM11QAE",
"product" : "Virtual World",
"provider_group" : "HelloWorld Provisioning",
"provider_group_manager" : null,
"provider_group_manager_email" : null,
"resolve_date" : ISODate("2019-04-30T13:54:42.000Z"),
"rt" : null,
"service_mrr" : 583,
"severity" : null,
"status" : "Closed",
"subject" : "HelloWorld - The Complex - 7516664 - Unsuspend",
"support_country" : null,
"support_group" : "Support - ETS"
}
我该怎么做才能返回完整的历史记录列表?
答案 0 :(得分:0)
我的查询可以满足我的要求,但没有返回匹配文档的完整历史记录列表。唯一的方法是使用返回的案例进行第二次查询以检索完整文档。
db.getCollection('cases').aggregate([
{
$match: {
"history.created_date": {
'$gte': new Date("2019-04-01T00:00:00"),
'$lte': new Date("2019-04-30T23:59:59")
}
}
},
{
"$unwind": "$history"
}
,{
"$match": {
"history.created_date":{
'$gte': new Date("2019-04-01T00:00:00"),
'$lte': new Date("2019-04-30T23:59:59")
},
"$or" :[{"history.new_value":{
"$in":['Tier 2','Global Application Support Team','HelloWorld Provisioning'],
}},{
"history.field" : "Resolved"
}],
"history.old_value":{
"$nin":['HelloWorld Provisioning','Global Application Support Team'],
},
}
}
,{
$group: {
"_id": {
year: {
$year: "$history.created_date"
},
month: {
$month: "$history.created_date"
},
week: {
$week: "$history.created_date"
}
},
cases: {$addToSet: "$caseNumber"}
}
},{
$project: {
"dates": "$_id",
_id: 0,
"cases": "$cases"
}
},
{$sort: {_id: 1}}
])