我有两个集合,事务和属性。我想首先根据 transaction 集合中的输入条件获取instrumntIds,然后自连接以从此集合中查找所有相同的instrumntIds。请改进我的代码,在这里我绑定了组,但是我的查询没有获取数据,所以我从查询中删除了组部分。
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
}
}
])
现在我想根据instrumentId和postingDate将此输出数据与第二个集合(attribute)连接起来。并希望从属性集合中获取状态并从交易集合中获取金额。
我是 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"
}
}
])
这是我的交易收藏
[
{
"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 "
}
]
属性集合是
[
{
"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"
}
]
我对“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"
}
]
}
]
我对“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"
},
]
}
]
如果我理解正确的话,你想要这样的东西:
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"
]
}
}
}
}
},
{
$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"
}
}
}
])