我很感激在這種情況下的任何幫助。MongoDB 中的集合(現在只有 4 個檔案用于演示目的):
{
"_id" : ObjectId("62684847e9594c65cbaa5d85"),
"agentId" : NumberInt(1),
"agentName" : "Yardi Gaondi",
"policyList" : [
{
"receivedDate" : ISODate("2022-03-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(1),
"policyStatusDate" : ISODate("2022-02-20T04:46:15.000 0000")
},
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-01-19T05:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-03-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(3),
"policyStatusDate" : ISODate("2022-01-16T04:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-02-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(1),
"policyStatusDate" : ISODate("2022-01-20T04:46:15.000 0000")
},
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-01-19T05:46:15.000 0000")
}
]
}
]
}
]
}
{
"_id" : ObjectId("62684847e9594c65cbaa5d86"),
"agentId" : NumberInt(2),
"agentName" : "Michelle Hazandi",
"policyList" : [
{
"receivedDate" : ISODate("2022-04-10T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-04-09T05:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-03-10T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-03-09T05:46:15.000 0000")
}
]
}
]
}
]
}
{
"_id" : ObjectId("626da96932cb6043086eae42"),
"agentId" : NumberInt(3),
"agentName" : "Barbie",
"policyList" : [
{
"receivedDate" : ISODate("2022-04-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(1),
"policyStatusDate" : ISODate("2022-05-20T04:46:15.000 0000")
},
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-01-19T05:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-02-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(3),
"policyStatusDate" : ISODate("2022-05-16T04:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-01-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(1),
"policyStatusDate" : ISODate("2022-03-20T04:46:15.000 0000")
},
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-01-19T05:46:15.000 0000")
}
]
}
]
}
]
}
{
"_id" : ObjectId("626da96932cb6043086eae43"),
"agentId" : NumberInt(4),
"agentName" : "Mzandi",
"policyList" : [
{
"receivedDate" : ISODate("2022-05-10T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-02-09T05:46:15.000 0000")
}
]
}
]
},
{
"receivedDate" : ISODate("2022-01-10T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-02-09T05:46:15.000 0000")
}
]
}
]
}
]
}
所以集合由 4 個檔案組成,每個檔案中有一個欄位“policyList”,它是一個物件陣列。在第一個檔案中 policyList 包含 3 個物件,在第二個檔案中只有兩個,依此類推。所以我必須以這種方式過濾這個集合中的檔案:1)我需要在“policyList”陣列中只保留那些與這種條件匹配的物件:其中一個欄位在一定的時間間隔內,這意味著必須是在至少一個欄位中匹配(第一個欄位是“receivedDate” - 位于第一級嵌套的陣列中 - “policyList”或第二個欄位“policyStatusDate” - 位于第三級的陣列中嵌套 - “policiesArray”,如果上述欄位之一匹配,我們從“policyList”回傳 完整物件,這意味著我們不能從“policiesArray”中丟棄任何物件)。一個匹配就足夠了,例如,如果我想查看從 01/02/2022 到 01/03/2022 的檔案,我希望在“policyList”陣列中的第一個檔案中只看到第一個和第三個物件,因為第一個物件匹配“policyStatusDate” - 20/02/2022(匹配“policiesArray”中的一個物件就足夠了)和第三個物件匹配“receivedDate” - 23/02/2022)和第一個檔案中的第二個物件我不希望請參閱,因為本檔案中的兩個日期都不在 2022 年 1 月 2 日至 2022 年 1 月 3 日期間;2)如果“policyList”中的物件之間沒有任何匹配,則意味著“policyList”過濾后必須為空,這種情況下我們不需要回傳這個檔案。
我的聚合請求:
db.getCollection("offers2").aggregate([
{
$project: {
"agentId": "$agentId",
"agentName": "$agentName",
"policyList": {
$filter: {
input: "$policyList",
as: "item",
cond: {
"$or": [
{
"$and": [
{ "$gte": [ "$$item.receivedDate", ISODate("2022-02-01") ] },
{ "$lte": [ "$$item.receivedDate", ISODate("2022-03-01") ] }
]
},
{
$and": [
{ "$gte": [ "$$item.policyStatusDetail.policiesArray.policyStatusDate", ISODate("2022-02-01") ] },
{ "$lte": [ "$$item.policyStatusDetail.policiesArray.policyStatusDate", ISODate("2022-03-01") ] }
]
}
]
}
}
}
}
},
{
$project: {
"agentId": "$agentId",
"agentName": "$agentName",
"policyList": "$policyList",
"numPoliciesPerDate": {
$cond: {
if: {$isArray: "$policyList"}, then: {$size: "$policyList"}, else: "0"
}
}
}
},
{
$match: {
"numPoliciesPerDate": {$gte: 1}
}
}
])
運行此查詢后,我希望在“policyList”陣列中接收第一個物件和第三個物件的第一個檔案,但我只收到第三個物件(此物件中的“receivedDate”匹配)。結果:
{
"_id" : ObjectId("62684847e9594c65cbaa5d85"),
"agentId" : NumberInt(1),
"agentName" : "Yardi Gaondi",
"policyList" : [
{
"receivedDate" : ISODate("2022-02-23T04:46:15.000 0000"),
"policyStatusDetail" : [
{
"policyStsCode" : NumberInt(7),
"policiesArray" : [
{
"policyDetailedCode" : NumberInt(1),
"policyStatusDate" : ISODate("2022-01-20T04:46:15.000 0000")
},
{
"policyDetailedCode" : NumberInt(2),
"policyStatusDate" : ISODate("2022-01-19T05:46:15.000 0000")
}
]
}
]
}
],
"numPoliciesPerDate" : NumberInt(1)
}
...所以看起來條件
$and": [
{ "$gte": [ "$$item.policyStatusDetail.policiesArray.policyStatusDate", ISODate("2022-02-01") ] },
{ "$lte": [ "$$item.policyStatusDetail.policiesArray.policyStatusDate", ISODate("2022-03-01") ] }
]
不正確。我認為這是因為在使用嵌套陣列時可能無法使用點表示法。所以也許有人可以幫助我修復這個聚合查詢,以便我在開始時描述的要求將被完全滿足,并且在我們的例子中,第一個檔案中“policyList”中的第一個物件也將被回傳?
uj5u.com熱心網友回復:
我認為這個可以解決問題,只需將日期替換為您所在時區的正確格式:
[
{
$unwind: {
path: "$policyList"
}
},
{
$match: {
$or: [
{
"policyList.receivedDate": {
$gte: ISODate("2022-02-01T03:00:00.000Z"),
$lte: ISODate("2022-03-01T03:00:00.000Z")
}
},
{
"policyList.policyStatusDetail.policiesArray.policyStatusDate": {
$gte: ISODate("2022-02-01T03:00:00.000Z"),
$lte: ISODate("2022-03-01T03:00:00.000Z")
}
}
]
}
},
{
$group: {
_id: "$_id",
agentId: {
$first: "$agentId"
},
agentname: {
$first: "$agentName"
},
policyList: {
$push: {
receivedDate: "$policyList.receivedDate",
policyStatusDetail: "$policyList.policyStatusDetail"
}
}
}
},
{
$project: {
"agentId": "$agentId",
"agentName": "$agentName",
"policyList": "$policyList",
"numPoliciesPerDate": {
$cond: {
if: {
$isArray: "$policyList"
},
then: {
$size: "$policyList"
},
else: "0"
}
}
}
},
{
$match: {
"numPoliciesPerDate": {
$gte: 1
}
}
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/468808.html
