MongoDB 中的 $set 是什么?

What is $set in MongoDB?

我正在编写一些 MongoDB 教程。我遇到了这个。

const updatedUser = await User.findByIdAndUpdate(
      req.params.id,
      {
        $set: req.body,
      },
      { new: true }
    );

$set 到底是什么?我认为它用于设置从 req.body 到数据库的任何内容,但我想查看一些关于它和其他 $ 用例的文档。但是,我找不到任何关于 $set.

的文档

$set 运算符用指定的值替换字段的值。

这意味着当你更新一些数据时,你可以设置特定字段的值,即使这些字段还不存在。

这是您需要的所有信息:https://docs.mongodb.com/manual/reference/operator/update/set/

"If the field does not exist, $set will add a new field with the specified value, provided that the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, $set will create the embedded documents as needed to fulfill the dotted path to the field."