【问题标题】:mongoDB aggregate nested document to only show three at a timemongoDB聚合嵌套文档一次只显示三个
【发布时间】:2020-12-10 17:19:28
【问题描述】:

这是我的桌子:

    {
        "_id" : ObjectId("5f2e8e0d4976ed2c04fc2e90"),
        "reference" : "5f2e8e0d4976ed2c04fc2e8f",
        "id" : "5e87187f6e8c15766e6994f8",
        "name" : "no name",
        "items" : [
                {
                        "_id" : ObjectId("5f2e994336caf138c13560fc"),
                        "requestedItems" : "5e87187f6e8c15766e6994f8",
                        "name" : "no name",
                },
                {
                        "_id" : ObjectId("5f2e994336caf138c135601c"),
                        "requestedItems" : "5e87187f6e8c15766e6994f8",
                        "name" : "no name",
                },
                {
                        "_id" : ObjectId("5f2e994336caf138c135201c"),
                        "requestedItems" : "5e87187f6e8c15766e6994f8",
                        "name" : "no name",
                },
                {
                        "_id" : ObjectId("5f2e994336caf138c139521c"),
                        "requestedItems" : "5e87187f6e8c15766e6994f8",
                        "name" : "no name",
                },
                {
                        "_id" : ObjectId("5f2e994336caf138c139504f"),
                        "requestedItems" : "5e87187f6e8c15766e6994f8",
                        "name" : "no name",
                },

        ],
        "createdAt" : ISODate("2020-08-08T11:35:41.675Z"),
        "updatedAt" : ISODate("2020-08-18T13:34:43.077Z"),
        "date" : "8-8-2020",
        "__v" : 0
}

我只想查询“ITEMS”中的最后三个子文档我一直在尝试 MongoDB 聚合框架但没有成功。这是我想出的。非常感谢您提供的任何帮助。

    reqInventory.aggregate([
 {$match:{"id" : "5e87187f6e8c15766e6994f8"}},
  { $addFields: {
    items: {
      $map: {
        input: '$items',
        as: 'new',
        in: {
          requestedItems : '$$new.requestedItems',
          name : '$$new.name',
          items: { $slice: [ '$$new.items', 3 ] }
          
        }
      }
    }
  }}

【问题讨论】:

    标签: mongodb mongoose aggregate nested-documents


    【解决方案1】:

    您可以使用{ $limit: <positive integer> } 来限制您的回复。

    这是给你更多解释的方式 https://docs.mongodb.com/manual/reference/operator/aggregation/limit/

    【讨论】:

      【解决方案2】:

      您可以直接在数组字段中使用$slice 负数,

      reqInventory.aggregate([
        {
          $match: { "id": "5e87187f6e8c15766e6994f8" }
        },
        {
          $addFields: { items: { $slice: ["$items", -3] } }
        }
      ])
      

      Playground

      【讨论】:

        【解决方案3】:

        可以直接使用项目

        reqInventory.aggregate([
              {
        $match:{"id" : "5e87187f6e8c15766e6994f8"}
        },
                  {
                    '$project': {
                      'last3ITems': {
                        '$slice': [
                          '$$ROOT.items', 2, 5
                        ]
                      }
                    }
                  }
                ]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-22
          • 2020-03-08
          • 2014-03-04
          • 1970-01-01
          • 2019-02-16
          • 1970-01-01
          • 2020-12-31
          • 2021-03-13
          相关资源
          最近更新 更多