gpt4 book ai didi

node.js - SQL 'UNION ALL' 类似 MongoDB 中的实现

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:27 26 4
gpt4 key购买 nike

有两个集合:

销售

{
"_id" : ObjectId("5ba0bfb8d1acdc0de716e839"),
"invoiceNumber" : 1,
"saleDate" : ISODate("2018-09-01T00:00:00.000Z"),
"totalTaxAmount" : 613,
"subTotalAmount" : 2000,
"totalAmount" : 2613,
"balance" : 2613,
"financialYear" : "2018-2019",
"modeOfPayment" : "Digital Transfer",
"customerName": "Acme Inc"
}

交易

{
"_id" : ObjectId("5bbb4e131fb8af0dc645212d"),
"transactionNumber" : 1
"transactionDate" : ISODate("2018-09-03T00:00:00.000Z"),
"transactionType" : "Income",
"partyName" : "Acme Inc",
"transactionMode" : "Digital Transfer",
"amount" : 2613,
"paidItems" : [
{
"orderId" : "5b90a7d62bb5a21be4ff97e3",
"invoiceNumber" : "1",
"orderType" : "sale",
"totalAmount" : 2613,
"balance" : 613,
"payingAmount" : 2000
}
]
}

我需要检索按日期排序的两个日期(即 saleDate、transactionDate)之间特定方(即 customerName、partyName)的销售和交易作为“标题”;如下:

[
{
"date": ISODate("2018-09-01T00:00:00.000Z"),
"heading": "Sale",
"particulars": "Invoice # 1",
"amount": 2613
},
{
"date": ISODate("2018-09-03T00:00:00.000Z"),
"heading": "Payment by Digital Transfer",
"particulars": "Transaction # 1",
"amount": 2000
}
]

我研究并尝试了 aggregation , $lookup但它没有返回所需的内容。

从 SQL 切换到 MongoDB。在 SQL 中,以下查询工作正常:

select sale_date as dated, 'Sale' as heading, 'Invoice # ' + 
convert(varchar(12),invoice_number) as particulars,
convert(varchar(12), total) as amount,
from sales where sale_date between @from_date AND @to_date AND
customer_name=@customer_name
UNION ALL
select transaction_date as dated, 'Payment by ' + transaction_mode as
heading, 'Transaction # ' + convert(varchar(12), transaction_id) as
particulars, convert(varchar(12), amount) as amount from transactions
where transaction_date between @from_date AND @to_date AND
party_name=@customer_name
order by dated DESC

有一个feature request已在 MongoDB 社区提交,但“未解决”。

我想知道在 mongoShell 或 MongoDB 驱动程序(mongoose/JS)中是否有任何方法可以实现此目的。使用当前稳定版本的 MongoDB、nodejs、express 和 mongoose。谢谢!

最佳答案

您可以尝试以下聚合

db.sales.aggregate([
{ "$limit": 1 },
{ "$facet": {
"collection1": [
{ "$limit": 1 },
{ "$lookup": {
"from": "sales",
"pipeline": [
{ "$match": {
"date": { "$gte": ISODate("2018-09-01"), "$lte": ISODate("2018-09-10") },
"customer.name": customerName
}},
{ "$project": {
"_id":0, "dated": "$saleDate", "heading": "Sale", "particulars": "$invoiceNumber",
"amount": "$totalAmount", "modeOfPayment": null
}}
],
"as": "collection1"
}}
],
"collection2": [
{ "$limit": 1 },
{ "$lookup": {
"from": "transactions",
"pipeline": [
{ "$match": {
"transactionDate": { "$gte": ISODate("2018-09-01"), "$lte": ISODate("2018-09-10") },
"userId": userId, "partyName": customerName
}},
{ "$project": {
"_id":0, "dated": "$transactionDate", "heading": "Payment","particulars": "$transactionNumber",
"amount": "$amount", "paymentMode": "$transactionMode"
}}
],
"as": "collection2"
}}
]
}},
{ "$project": {
"data": {
"$concatArrays": [
{ "$arrayElemAt": ["$collection1.collection1", 0] },
{ "$arrayElemAt": ["$collection2.collection2", 0] },
]
}
}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" } },
{ "$sort": { "dated": -1 }}
])

关于node.js - SQL 'UNION ALL' 类似 MongoDB 中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715308/

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