Пожалуйста, прости мою краткость, я новичок в агрегатной структуре MongoDB.У меня есть следующая коллекция с «историей» как вложенный документ, и я пытаюсь спроецировать соответствующую коллекцию вместе с полным списком истории, но мой запрос возвращает только соответствующий элемент во вложенной истории.
{
"_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"
}
Вот мой запрос
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: {
$push: {
caseNumber: "$caseNumber",
account_name: "$account_name",
status: "$status",
owner: "$owner",
owner_id: "$owner_id",
create_date: "$create_date",
last_update: "$last_update",
resolve_date: "$resolve_date",
provider_group: "$provider_group",
issue_level: "$issue_level",
case_type: "$case_type",
account_id: "$account_id",
history: "$history",
feeds: "$feeds",
product: "$product",
severity: "$severity",
support_group: "$support_group"
}
}
}
},{
$project: {
"dates": "$_id",
_id: 0,
"cases": "$cases"
}
},
{$sort: {_id: 1}}
])
The result i get is the matched element of the flattened array.
{
"dates" : {
"year" : 2019,
"month" : 4,
"week" : 14
},
"totalCases" : 86.0,
"cases" : [
{
"caseNumber" : "C1781724",
"account_name" : "American Fed of Govt Emp",
"status" : "Closed",
"owner" : "Hy Van",
"owner_id" : "005500000071TgPAAU",
"create_date" : ISODate("2019-04-10T22:32:40.000Z"),
"last_update" : ISODate("2019-04-29T18:45:42.000Z"),
"resolve_date" : ISODate("2019-04-15T18:34:50.000Z"),
"provider_group" : "Tier 2",
"issue_level" : "Phone Service",
"case_type" : "Issue-Fault",
"account_id" : "001500000168SrjAAE",
"history" : {
"_id" : ObjectId("5cc75fcd55e11149c3366285"),
"history_id" : "0172J0000t73NlXQAU",
"field" : "Provider_Group__c",
"new_value" : "Tier 2",
"old_value" : "SGS - Web and Chat",
"created_date" : ISODate("2019-04-11T19:10:22.000Z"),
"created_by_id" : "005500000071TgXAAU"
},
"feeds" : [
{
"_id" : ObjectId("5cc75fcd55e11149c336627a"),
"feed_id" : "0D52J00006omQnySAE",
"event" : "EmailMessageEvent",
"created_by_id" : "005500000071TgPAAU",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-15T18:34:52.000Z")
},
{
"_id" : ObjectId("5cc75fcd55e11149c3366279"),
"feed_id" : "0D52J00006omQnxSAE",
"event" : "TextPost",
"created_by_id" : "005500000071TgPAAU",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-15T18:34:50.000Z")
},
{
"_id" : ObjectId("5cc75fcd55e11149c3366278"),
"feed_id" : "0D52J00006oGTMwSAO",
"event" : "EmailMessageEvent",
"created_by_id" : "005500000071WBzAAM",
"visibility" : "AllUsers",
"created_date" : ISODate("2019-04-12T16:15:47.000Z")
},
{
"_id" : ObjectId("5cc75fcd55e11149c3366277"),
"feed_id" : "0D52J00006oGQhvSAG",
"event" : "TextPost",
"created_by_id" : "00550000007gdQEAAY",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-12T15:47:26.000Z")
},
{
"_id" : ObjectId("5cc75fcd55e11149c3366276"),
"feed_id" : "0D52J00006oGQXTSA4",
"event" : "TextPost",
"created_by_id" : "00550000007gdQEAAY",
"visibility" : "InternalUsers",
"created_date" : ISODate("2019-04-12T15:45:30.000Z")
},
{
"_id" : ObjectId("5cc75fcd55e11149c3366275"),
"feed_id" : "0D52J00006oER2gSAG",
"event" : "EmailMessageEvent",
"created_by_id" : "0052J0000082whRQAQ",
"visibility" : "AllUsers",
"created_date" : ISODate("2019-04-10T22:32:42.000Z")
}
],
"product" : "Virtual Office",
"severity" : null,
"support_group" : "Support - SMB"
},
.
.
.
}
Что я могу сделать, чтобы вернуть полный список истории?