简体   繁体   English

MongoDB findOneAndUpdate 投影

[英]MongoDB findOneAndUpdate projection

I'm currently using MongoDB and using the findOneAndUpdate method.我目前正在使用 MongoDB 并使用findOneAndUpdate方法。 I'm trying to use the projection however it doesn't seem to be working 100% successfully.我正在尝试使用投影,但它似乎没有 100% 成功。

Here is the projection:这是投影:

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

As you can see, I'm trying to set orderID to the value of _id however, it doesn't do anything.如您所见,我正在尝试将orderID设置为_id的值,但是它没有做任何事情。

Here is the code I am executing for reference :这是我正在执行的代码以供参考:

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

I hope someone can help me, thank you!希望有人能帮帮我,谢谢!

As far as I know projection with .find() or similar .findOneAndUpdate() does not support field transformations(adding new field out of existing) which $project in aggregation is capable of doing.据我所知,使用.find()或类似的.findOneAndUpdate()投影不支持聚合中的$project能够执行的字段转换(从现有字段中添加新字段)。 So projection can be used just to include or exclude certain fields from result.因此,投影可以仅用于在结果中包含或排除某些字段。 Although this is true but up-to certain extent this is false, we can transform an array field using projection operators, Check : projection .虽然这是真的,但在某种程度上这是错误的,我们可以使用投影运算符 Check : projection转换数组字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM