我有两个集合,事务和属性.我希望首先根据输入条件从transaction个集合中获取工具ntId,然后self 联接以从该集合中找到所有相同的工具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
}
}
")
现在,我希望将此输出数据与第二个集合(attribute)连接起来,该集合基于InstrumentID和postingDate.并希望从属性集合中获取状态,从交易集合中获取金额.
我是Mongo上的新手,所以我有困惑,比如如何首先self 加入,然后加入第二个集合这里是我的代码
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"
}
}
")
这是我的交易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 "
}
"
属性集合是
[
{
"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"
},
"
}
"