MongoDB findOneAndUpdate投影

问题描述 投票:1回答:1

我当前正在使用MongoDB并使用findOneAndUpdate方法。我正在尝试使用投影,但是它似乎无法100%成功地工作。

这里是投影:

{
  orderID: '$_id',
  _id: false,
  user: true,
  guild: true,
  order: true,
  status: true,
  orderExpiry: true,
  priority: true,
  sentAt: true
}

如您所见,我正在尝试将orderID设置为_id的值,但是它什么也没做。

这是我正在执行的代码供参考:

await this.db.collection('orders').findOneAndUpdate(filter, { $set: { ...data } },
                  { returnOriginal: false, projection: this.getProjectionFields() });

我希望有人能帮助我,谢谢!

mongodb mongodb-query projection
1个回答
0
投票

据我所知,用.find()或类似的.findOneAndUpdate()进行的投影不支持聚合$project能够进行的场转换(从现有场中增加新场)。因此,投影只能用于从结果中包括或排除某些字段。尽管这是正确的,但在一定程度上是错误的,我们可以使用投影运算符来转换数组字段,请检查:projection

© www.soinside.com 2019 - 2024. All rights reserved.