【问题标题】:How to return a filtered result of embedded documents through a query in mongoose如何通过猫鼬中的查询返回嵌入文档的过滤结果
【发布时间】:2020-06-11 17:41:48
【问题描述】:

我正在寻找一个查询,它只能返回一个数组中的某个文档,该数组嵌入在主文档的另一个数组中。 下面是存储在 DB 中的集合:

{
    "List": {
        "_id": "5ee1c1e6739bb23e3c54d35c",
        "capitalAccountID": "CaptialAccount-43",
        "userEmail": "kumarshreyas073@gmail.com",
        "flagID": 1,
        "ledgerName": "Capital Account",
        "subLedgers": [
            {
                "openingBalance": 100000,
                "closingBalance": 90001,
                "transactions": [
                    {
                        "_id": "5ee1c1e6739bb23e3c54d35e",
                        "transactionID": "123456",
                        "date": "09/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 0,
                        "debit": 1
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d35f",
                        "transactionID": "123457",
                        "date": "10/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d360",
                        "transactionID": "123458",
                        "date": "11/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    }
                ],
                "_id": "5ee1c1e6739bb23e3c54d35d",
                "subLedgerID": 1,
                "subLedgerName": "ABC",
                "reference": "dghj",
                "rateOfDuty": 10,
                "gstApplicable": true
            },
            {
                "openingBalance": 100000,
                "closingBalance": 95001,
                "transactions": [
                    {
                        "_id": "5ee1c1e6739bb23e3c54d362",
                        "transactionID": "123459",
                        "date": "13/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 0,
                        "debit": 1
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d363",
                        "transactionID": "123450",
                        "date": "14/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d364",
                        "transactionID": "123451",
                        "date": "15/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d365",
                        "transactionID": "123452",
                        "date": "16/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 0,
                        "debit": 5000
                    }
                ],
                "_id": "5ee1c1e6739bb23e3c54d361",
                "subLedgerID": 2,
                "subLedgerName": "DEF",
                "reference": "dohj",
                "rateOfDuty": 10,
                "gstApplicable": true
            }
        ],
        "idCounter": 43,
        "__v": 0
    }
}

下面的代码返回上面的结果:

await CaptialAccount.findOne(
    {
      userEmail: req.body.userEmail,
    },
    (err, list) => {
      res.status(200).json({
        List: list
      })
    }
  );

我需要根据日期过滤“subLedgers”数组中的“transactions”数组。 例如:从“09/06/2020”到“13/06/2020”。 预期输出:

{
    "List": {
        "_id": "5ee1c1e6739bb23e3c54d35c",
        "capitalAccountID": "CaptialAccount-43",
        "userEmail": "kumarshreyas073@gmail.com",
        "flagID": 1,
        "ledgerName": "Capital Account",
        "subLedgers": [
            {
                "openingBalance": 100000,
                "closingBalance": 90001,
                "transactions": [
                    {
                        "_id": "5ee1c1e6739bb23e3c54d35e",
                        "transactionID": "123456",
                        "date": "09/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 0,
                        "debit": 1
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d35f",
                        "transactionID": "123457",
                        "date": "10/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    },
                    {
                        "_id": "5ee1c1e6739bb23e3c54d360",
                        "transactionID": "123458",
                        "date": "11/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 5000,
                        "debit": 0
                    }
                ],
                "_id": "5ee1c1e6739bb23e3c54d35d",
                "subLedgerID": 1,
                "subLedgerName": "ABC",
                "reference": "dghj",
                "rateOfDuty": 10,
                "gstApplicable": true
            },
            {
                "openingBalance": 100000,
                "closingBalance": 95001,
                "transactions": [
                    {
                        "_id": "5ee1c1e6739bb23e3c54d362",
                        "transactionID": "123459",
                        "date": "13/06/2020",
                        "particulars": "By-sales",
                        "voucherNumber": "asdfghjk",
                        "voucherType": "SR",
                        "credit": 0,
                        "debit": 1
                    }
                ],
                "_id": "5ee1c1e6739bb23e3c54d361",
                "subLedgerID": 2,
                "subLedgerName": "DEF",
                "reference": "dohj",
                "rateOfDuty": 10,
                "gstApplicable": true
            }
        ],
        "idCounter": 43,
        "__v": 0
    }
}

是否有查询来实现这一点? 谢谢。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您必须为此使用管道,利用$filter$elemMatch 的查询等效项只能用于返回数组中与条件匹配的第一项。

    db.collection.aggregate([
        {
            $match: {
                userEmail: req.body.userEmail,
            }
        },
        {
            $addFields: {
                "List.subLedgers": {
                    $map: {
                        input: "$List.subLedgers",
                        as: "subLedger",
                        in: {
                            $mergeObjects: ["$$subLedger", {
                                transactions: {
                                    $filter: {
                                        input: "$$subLedger.transactions",
                                        as: "transaction",
                                        cond: {
                                            $and: [
                                                {
                                                    $gte: ["$$transaction.date", "start date"]
                                                },
    
                                                {
                                                    $lte: ["$$transaction.date", "end date"]
                                                }
                                            ]
                                        }
                                    }
                                }
                            }]
                        }
                    }
                }
            }
        }
    ])
    

    从看起来你的交易日期被保存为字符串,虽然不建议这样做,但只要你能保证它们都在相同的时区和格式,否则你会得到意想不到的结果。

    【讨论】:

    • 非常感谢。这东西节省了我一半的工作。如果您提供一个链接,我可以深入了解此代码,那就太好了。
    • 您可以查看 Mongo 的参考资料,了解此处使用的所有可用聚合运算符:docs.mongodb.com/manual/reference/operator/aggregation。除此之外,如果您想更好地了解任何具体内容,我很乐意回答。
    • 感谢您的帮助。
    猜你喜欢
    • 2020-10-08
    • 2019-02-18
    • 2015-11-28
    • 2020-01-04
    • 2017-10-05
    • 2012-01-08
    • 2018-12-04
    • 2017-01-06
    • 2017-12-03
    相关资源
    最近更新 更多