基于排序的mongodb聚合展开的添加秩

s4n0splo  于 2023-01-20  发布在  Go
关注(0)|答案(1)|浏览(127)

我使用includeArrayIndex添加基于排序的排名
下面是聚合查询

dbo.collection("funds").aggregate([
  {
    "$sort": {
      "amount": -1
    }
  },
  {
    "$group": {
      "_id": "",
      "items": {
        "$push": "$$ROOT"
      }
    }
  },
  {
    "$unwind": {
      "path": "$items",
      "includeArrayIndex": "items.rank"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$items"
    }
  },
  {
    "$sort": {
      "rank": -1
    }
  }
])

这将分配rank字段,但从0开始,是否有办法从1开始
我试过这样的布景

[
        {
          '$sort': {
            'amount': -1
          }
        }, {
          '$group': {
            '_id': '',
            'items': {
              '$push': '$$ROOT'
            }
          }
        }, {
          '$unwind': {
            'path': '$items',
            'includeArrayIndex': 'items.rank'
          }
        }, {
          '$replaceRoot': {
            'newRoot': '$items'
          }
        },
        {
          '$set': {
            'rank': {
              '$add': [
                '$rank', 1
              ]
            }
          }
        },
        // {
        //   '$sort': {
        //     'rank': 1
        //   }
        // }
      ]

在mongodb Compass aggregation选项卡中,它显示添加了该字段,但是当我使用nodejs运行此脚本时,它没有添加rank字段
即使我试过

const a = await dbo.collection("funds").aggregate(
      [
        {
          '$sort': {
            'amount': 1
          }
        }, {
          '$group': {
            '_id': '',
            'items': {
              '$push': '$$ROOT'
            }
          }
        }, {
          '$unwind': {
            'path': '$items',
            'includeArrayIndex': 'items.rank'
          }
        }, {
          '$replaceRoot': {
            'newRoot': '$items'
          }
        },
        {
          '$addFields': {
            'rank': {
              '$add': [
                '$rank', 1
              ]
            }
          }
        }
      ]
    ).toArray();

这甚至在控制台上打印为

[
  { _id: new ObjectId("6220d2fe20e33d48c865b720"), amount: 1, rank: 1 },
  {
    _id: new ObjectId("6220d2cf20e33d48c865b71e"),
    amount: 10,
    rank: 2
  },
  {
    _id: new ObjectId("6220d2f520e33d48c865b71f"),
    amount: 12,
    rank: 3
  }
]

然后尝试使用$setWindowFields

dbo.collection("funds").aggregate( [
   {
      $setWindowFields: {
         sortBy: { amount: -1 },
         output: {
            rank: {
               $rank: {}
            }
         }
      }
   }
] )

但它表明

err MongoServerError: Unrecognized pipeline stage name: '$setWindowFields'

示例文档如下所示

[
  {amount : 20, name :""},
  {amount : 22, name :""}
]
2izufjch

2izufjch1#

不确定是否已回答此问题。但您只需添加{ $out:“funds”}在您的代码末尾。PFB工作代码...

const a = await dbo.collection("funds").aggregate(
      [
        {
          '$sort': {
            'amount': 1
          }
        }, {
          '$group': {
            '_id': '',
            'items': {
              '$push': '$$ROOT'
            }
          }
        }, {
          '$unwind': {
            'path': '$items',
            'includeArrayIndex': 'items.rank'
          }
        }, {
          '$replaceRoot': {
            'newRoot': '$items'
          }
        },
        {
          '$addFields': {
            'rank': {
              '$add': [
                '$rank', 1
              ]
            }
          }
        },
        { $out: "funds" }
      ], {allowDiskUse:true}
    ).toArray();

相关问题