gpt4 book ai didi

mongodb first self join and then join with another table based on some conditions(MongoDB先自联接,然后根据某些条件与另一个表联接)

转载 作者:bug小助手 更新时间:2023-10-25 21:17:45 25 4
gpt4 key购买 nike



I have two collections, transaction and attributes. I want first to get the instrumntIds based on input conditions from transaction collection and then self join to find all the same instrumntIds from this collection. please improve my code, here I tied group but then my query is not getting the data so I removed group part from the query.

我有两个集合,事务和属性。我希望首先根据输入条件从事务集合中获取工具ntID,然后自我联接以从该集合中找到所有相同的工具ntID。请改进我的代码,我在这里绑定了组,但我的查询没有获得数据,所以我从查询中删除了组部分。


db.transaction.aggregate([
{
$lookup: {
from: "transaction",
localField: "data.instrumentId",
foreignField: "data.instrumentId",
as: "s2"
}
},
{
$match: {
"data.postingDate": {
$gte: ISODate("2022-01-01"),
$lte: ISODate("2022-01-31")
}
}
},
{
$project: {
_id:0,
"data.instrumentId": 1,
"data.postingDate": 1
}
}
])

now I want to join this output data with second collection(attribute) based on instrumentId and postingDate. and want to get status from the attribute collection and amount from transaction collection.

现在,我希望将此输出数据与第二个集合(属性)连接在一起,该集合基于InstrumentID和postingDate。并希望从属性集合中获取状态,从交易集合中获取金额。


I am new on mongo so I have confusion like how to self join first and then join with second collection here is my code

我是Mongo上的新手,所以我有困惑,比如如何首先自我加入,然后加入第二个集合这里是我的代码


db.getCollection("transaction").aggregate([
{
$lookup: {
from: "attributes",
let: {
postingDate: "$data.postingDate",
instrumentId: "$data.instrumentId"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$postingDate", "$$postingDate"] },
{ $eq: ["$instrumentId", "$$instrumentId"] }
]
}
}
}
],
as: "attributes"
}
},
{
$unwind: "$attributes"
},
{
$match: {
"data.postingDate": {
$gte: ISODate("2022-01-01"),
$lte: ISODate("2022-01-31")
}
}
},
{
$group: {
_id: {
instrumentId: "$attributes.instrumentId",
status: "$attributes.status"
},
instrumentIds: { $first: "$data.instrumentId" }
}
},
{
$project: {
_id: 0,
amount: "$data.amount",
status: "$attributes.status",
instrumentIds: "$instrumentIds"

}
}
])

Here is my transaction collection

这是我的交易收藏


[
{
"postingDate": "1/1/2023",
"instrumentId": "Type_1",
"Amount": " $2,000,000.00 "
},
{
"postingDate": "1/4/2023",
"instrumentId": "Type_2",
"Amount": " $700,000.00 "
},
{
"postingDate": "1/4/2023",
"instrumentId": "Type_3",
"Amount": " $400,000.00 "
},
{
"postingDate": "2/1/2023",
"instrumentId": "Type_1",
"Amount": " $10,000.00 "
},
{
"postingDate": "2/4/2023",
"instrumentId": "Type_2",
"Amount": " $4,000.00 "
},
{
"postingDate": "2/4/2023",
"instrumentId": "Type_3",
"Amount": " $2,000.00 "
},
{
"postingDate": "3/2/2023",
"instrumentId": "Type_2",
"Amount": " $3,900.00 "
},
{
"postingDate": "3/4/2023",
"instrumentId": "Type_3",
"Amount": " $1,950.00 "
},
{,
"postingDate": "4/1/2023",
"instrumentId": "Type_2",
"Amount": " $3,900.00 "
},
{
"Accounting Period": "Apr-23",
"postingDate": "4/6/2023",
"instrumentId": "Type_3",
"Amount": " $1,950.00 "
},
{
"postingDate": "5/1/2023",
"instrumentId": "Type_2",
"Amount": " $3,900.00 "
},
{
"postingDate": "5/6/2023",
"instrumentId": "Type_3",
"Amount": " $1,950.00 "
},
{
"postingDate": "5/15/2023",
"instrumentId": "Type_1",
"Amount": " $500,000.00 "
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_2",
"Amount": " $3,500.00 "
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_3",
"Amount": " $1,700.00 "
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_1",
"Amount": " $100,000.00 "
}

]

]


and attribute collection is

属性集合是


[
{
"postingDate": "1/1/2023",
"instrumentId": "Type_1",
"status": "A"
},
{
"postingDate": "1/4/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"postingDate": "1/4/2023",
"instrumentId": "Type_3",
"status": "A"
},
{
"postingDate": "2/1/2023",
"instrumentId": "Type_1",
"status": "A"
},
{
"postingDate": "2/4/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"postingDate": "2/4/2023",
"instrumentId": "Type_3",
"status": "A",
},
{
"postingDate": "3/2/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"postingDate": "3/4/2023",
"instrumentId": "Type_3",
"status": "N"
},
{,
"postingDate": "4/1/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"Accounting Period": "Apr-23",
"postingDate": "4/6/2023",
"instrumentId": "Type_3",
"status": "N"
},
{
"postingDate": "5/1/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"postingDate": "5/6/2023",
"instrumentId": "Type_3",
"status": "N"
},
{
"postingDate": "5/15/2023",
"instrumentId": "Type_1",
"status": "N"
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_2",
"status": "A"
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_3",
"status": "A"
},
{
"postingDate": "6/15/2023",
"instrumentId": "Type_1",
"status": "A"
}

]

]


my expected result for the 'postingDate = 4/30/2023' will be look like

我对‘postingDate=4/30/2023’的预期结果如下所示


    [
{
"_id": {
"postingDate": "4/1/2023",
"status": "A"
},
"allAmountsInstrumentId": [
{
"amount": " $700,000.00 ",
"instrumentId": "Type_2",
"postingDate": "1/4/2023"
},
{
"amount": " $4,000.00 ",
"instrumentId": "Type_2",
"postingDate": "2/4/2023"
},
{
"amount": " $3,900.00 ",
"instrumentId": "Type_2",
"postingDate": "3/2/2023"
},
{
"amount": " $3,900.00 ",
"instrumentId": "Type_2",
"postingDate": "4/1/2023"
}
]
},
{
"_id": {
"postingDate": "4/6/2023",
"status": "N"
},
"allAmountsInstrumentId": [
{
"amount": " $400,000.00 ",
"instrumentId": "Type_3",
"postingDate": "1/4/2023"
},
{
"amount": " $2,000.00 ",
"instrumentId": "Type_3",
"postingDate": "2/4/2023"
},
{
"amount": " $1,950.00 ",
"instrumentId": "Type_3",
"postingDate": "3/4/2023"
},
{
"amount": " $1,950.00 ",
"instrumentId": "Type_3",
"postingDate": "4/6/2023"
}
]
}
]

my expected result for the 'postingDate = 05/31/2023' will be look like

我对‘postingDate=05/31/2023’的预期结果如下


    [
{
"_id": {
"postingDate": "5/15/2023",
"status": "N"
},
"allAmountsInstrumentId": [
{
"amount": " $2,000,000.00 ",
"instrumentId": "Type_1",
"postingDate": "1/1/2023"
},
{
"amount": " $10,000.00 ",
"instrumentId": "Type_1",
"postingDate": "2/1/2023"
},
{
"amount": " $500,000.00 ",
"instrumentId": "Type_1",
"postingDate": "5/15/2023"
}
]
},
{
"_id": {
"postingDate": "5/1/2023",
"status": "A"
},
"allAmountsInstrumentId": [
{
"amount": " $700,000.00 ",
"instrumentId": "Type_2",
"postingDate": "1/4/2023"
},
{
"amount": " $4,000.00 ",
"instrumentId": "Type_2",
"postingDate": "2/4/2023"
},
{
"amount": " $3,900.00 ",
"instrumentId": "Type_2",
"postingDate": "3/2/2023"
},
{
"amount": " $3,900.00 ",
"instrumentId": "Type_2",
"postingDate": "4/1/2023"
},
{
"amount": " $3,900.00 ",
"instrumentId": "Type_2",
"postingDate": "5/1/2023"
}
]
},
{
"_id": {
"postingDate": "5/6/2023",
"status": "N"
},
"allAmountsInstrumentId": [
{
"amount": " $400,000.00 ",
"instrumentId": "Type_3",
"postingDate": "1/4/2023"
},
{
"amount": " $2,000.00 ",
"instrumentId": "Type_3",
"postingDate": "2/4/2023"
},
{
"amount": " $1,950.00 ",
"instrumentId": "Type_3",
"postingDate": "3/4/2023"
},
{
"amount": " $1,950.00 ",
"instrumentId": "Type_3",
"postingDate": "4/6/2023"
},
{
"amount": " $1,950.00 ",
"instrumentId": "Type_3",
"postingDate": "5/6/2023"
},
]
}
]

更多回答

Please add what is the expected output.

请添加预期产量是多少。

@CharchitKapoor I updated the collections and mentioned the expected output.

@ChArchitKapoor我更新了集合,并提到了预期的输出。

consider change your structure of a transaction to something like: {"postingDate": ISODate("2023-01-01"), "instrumentId": "Type_1", "Amount": 2000000, "currency": "USD"}

考虑将您的交易结构更改为:{“postingDate”:isodate(“2023-01-01”),“InstrumentID”:“Type_1”,“Amount”:2000000,“Currency”:“U.S.”}

Does this answers it? You can use $setWindowFields to sum the amounts per ` instrumentId`

这能回答这个问题吗?您可以使用$setWindowFields对每个`InformentId`的金额求和

Does this answers your question?

这回答了你的问题吗?

优秀答案推荐

If I understand correctly, you want something like this:

如果我理解正确的话,你想要这样的东西:


db.transactions.aggregate([
{$match: {$expr: {
$lte: [
{$dateFromString: {
dateString: "$postingDate",
format: "%m/%d/%Y"
}},
ISODate("2023-04-30")
]
}}},
{$setWindowFields: {
partitionBy: "$instrumentId",
output: {allAmounts: {
$push: {amount: "$Amount", postingDate: "$postingDate"},
window: {documents: ["unbounded", "unbounded"]}
}}
}},
{$match: {$expr: {$gte: [
{$dateFromString: {
dateString: "$postingDate",
format: "%m/%d/%Y"
}},
ISODate("2023-04-01")
]}}},
{$lookup: {
from: "attributes",
let: {
postingDate: "$postingDate",
instrumentId: "$instrumentId"
},
pipeline: [
{$match: {$expr: {$and: [
{$eq: ["$postingDate", "$$postingDate"]},
{$eq: ["$instrumentId", "$$instrumentId"]}
]}}},
{$project: {status: 1, _id: 0}}
],
as: "attributes"
}},
{$unwind: "$attributes"},
{$group: {
_id: {
postingDate: "$postingDate",
status: "$attributes.status"
},
allAmountsInstrumentId: {$first: "$allAmounts"}
}}
])

See how it works on the playground example

看看它在操场上是如何工作的示例


For mongoDB versions older than 5.0, you can replace the $setWindowFields with a $lookup stage:

对于早于5.0的MongoDB版本,可以将$setWindowFields替换为$lookup阶段:


 {$lookup: {
from: "transactions",
localField: "instrumentId",
foreignField: "instrumentId",
pipeline: [
{$match: {$expr: {$lte: [
{$dateFromString: {
dateString: "$postingDate",
format: "%m/%d/%Y"
}},
ISODate("2023-04-30")
]}}},
{$project: {
amount: "$Amount",
postingDate: 1,
_id: 0
}}
],
as: "allAmounts"
}},

See how it works on the playground example

看看它在操场上是如何工作的示例


更多回答

I am using mongo version 4.4.11 and $setWindowFields is not working on it. is there any alternative for it

我使用的是Mongo版本4.4.11,而$setWindowFields没有在它上面工作。有什么替代办法吗?

$lookup is an alternative

$lookup是一种替代方案

{ $group: { _id: { instrumentId: "$data.instrumentId", postingDate: "$data.postingDate" }, allAmounts: { $push: { amount: "$data.amount", postingDate: "$data.postingDate", instrumentId: "$data.instrumentId" } } } }, I changed setWindowFields to this logic but the self join part is not working

{$group:{_id:{InstrumentID:“$data.InstrumentID”,postingDate:“$data.postingDate”},allAmount:{$PUSH:{Amount:“$data.mount”,postingDate:“$data.postingDate”,InstrumentID:“$data.InformentID”}}},我将setWindowFields更改为此逻辑,但自联接部分不起作用

I am trying to remove $setWindowFields block, because my mongo version is old and it is not supporting the $setWindowFileds, please any help for this

我正在尝试删除$setWindowFields块,因为我的mongo版本是旧的,并且它不支持$setWindowFilez,请对此提供帮助

Updated the answer accordingly

相应地更新了答案

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com