【问题标题】:How to return a filtered result of array inside a documents through a query in mongoose如何通过猫鼬中的查询返回文档内数组的过滤结果
【发布时间】:2020-10-08 05:33:33
【问题描述】:

我正在寻找一个只能返回主文档数组中的特定文档的查询。下面是存储在 DB 中的集合:

{
            "_id": "5ed49b42d6fc0c3878c875a2",
            "stockID": "Stock-1",
            "openingBalance": 30000,
            "closingBalance": 29960,
            "userEmail": "kumarshreyas073@gmail.com",
            "stockName": "Coconut",
            "unitMeasure": "Kgs",
            "referenceName": "U16H",
            "gstApplicable": true,
            "rateOfDuty": 15,
            "price": 30,
            "total": 900000,
            "idCounter": 1,
            "transactions": [
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed630530de9f419fc45d2ed",
                    "transactionID": "5ed630530de9f419fc45d2eb",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed630c72218b03658bf8191",
                    "transactionID": "5ed630c62218b03658bf818f",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64ebd89ee57327cd46772",
                    "transactionID": "5ed64ebd89ee57327cd46770",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64f0cf5891925f0f0a8b1",
                    "transactionID": "5ed64f0cf5891925f0f0a8af",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64f42b56b7d1380df8b70",
                    "transactionID": "5ed64f42b56b7d1380df8b6e",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed650674961b126a4092f3f",
                    "transactionID": "5ed650674961b126a4092f3d",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "U123",
                    "voucherType": "Purchase Return",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed72df8f497fb0edc745c4b",
                    "transactionID": "CN-126656",
                    "date": "14/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "U123",
                    "voucherType": "",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed73bf9c922270020db492d",
                    "transactionID": "CN-126665",
                    "date": "14/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "U123",
                    "voucherType": "Purchase Return",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed753a85c61c71e3089f40f",
                    "transactionID": "CN-196700",
                    "date": "14/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "U123",
                    "voucherType": "Purchase Return",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed753ed5c61c71e3089f415",
                    "transactionID": "CN-196000",
                    "date": "14/05/2020"
                },
                {
                    "particulars": "By demo5",
                    "voucherNumber": "1230456",
                    "voucherType": "Purchase",
                    "inward": 50,
                    "outward": 0,
                    "_id": "5ee735e807a0112c9c079d9a",
                    "transactionID": "5ee735e807a0112c9c079d98",
                    "date": "15/06/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "12345670",
                    "voucherType": "gdbxdkvghj",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ee9a4b2b8d4c72440e7a6a1",
                    "transactionID": "5ee9a33a6c1f103fa82992fd",
                    "date": "23/06/2020"
                },
                {
                    "particulars": "To demo1",
                    "voucherNumber": "1234567000",
                    "voucherType": "gdbxdkvghj",
                    "inward": 0,
                    "outward": 20,
                    "_id": "5ee9e28ef327db4358b7465d",
                    "transactionID": "5ee9e15cf327db4358b74651",
                    "date": "17/06/2020"
                }
            ],
            "__v": 0
        }

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

var stockAccount = await Stock.aggregate([
    {
      $match: {
        userEmail: req.body.userEmail,
      },
    },
    {
      $match: {
        "transactions.date": {
          $gte: req.body.fromDate,
          $lte: req.body.toDate,
        },
      },
    },
  ]);

我需要根据日期过滤“交易”数组。例如:从“09/05/2020”到“23/05/2020”。预期输出:

{
            "_id": "5ed49b42d6fc0c3878c875a2",
            "stockID": "Stock-1",
            "openingBalance": 30000,
            "closingBalance": 29960,
            "userEmail": "kumarshreyas073@gmail.com",
            "stockName": "Coconut",
            "unitMeasure": "Kgs",
            "referenceName": "U16H",
            "gstApplicable": true,
            "rateOfDuty": 15,
            "price": 30,
            "total": 900000,
            "idCounter": 1,
            "transactions": [
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed630530de9f419fc45d2ed",
                    "transactionID": "5ed630530de9f419fc45d2eb",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed630c72218b03658bf8191",
                    "transactionID": "5ed630c62218b03658bf818f",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64ebd89ee57327cd46772",
                    "transactionID": "5ed64ebd89ee57327cd46770",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64f0cf5891925f0f0a8b1",
                    "transactionID": "5ed64f0cf5891925f0f0a8af",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed64f42b56b7d1380df8b70",
                    "transactionID": "5ed64f42b56b7d1380df8b6e",
                    "date": "23/05/2020"
                },
                {
                    "particulars": "To demo",
                    "voucherNumber": "1234567",
                    "voucherType": "Sales",
                    "inward": 0,
                    "outward": 10,
                    "_id": "5ed650674961b126a4092f3f",
                    "transactionID": "5ed650674961b126a4092f3d",
                    "date": "23/05/2020"
                }
            ],
            "__v": 0
        }

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

【问题讨论】:

    标签: node.js arrays mongodb mongoose


    【解决方案1】:

    我认为您的情况不需要聚合。

    var stockAccount = await Stock.find({
          userEmail: req.body.userEmail,
          "transactions.date": {
            $gte: req.body.fromDate,
            $lte: req.body.toDate,
          }
    });
    

    注意:数据库中日期的格式好像是字符串。因此,日期比较不应该起作用。

    【讨论】:

    • 我试过这个。此查询返回包含所有交易的记录。
    • 可能是因为fromDatetoDate的值不正确?
    • @ShreyasKumar 数据库中日期的格式似乎是一个字符串。所以,日期比较不应该起作用。
    • 是的。这就是我选择流水线的原因。有什么方法吗。
    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2015-11-28
    • 1970-01-01
    • 2020-01-04
    • 2012-01-08
    • 2017-04-18
    • 2018-12-04
    • 2017-01-06
    相关资源
    最近更新 更多