首页 > 解决方案 > findOneAndUpdate() 用于两个不同的 ObjectId,但只更新一个

问题描述

我正在构建一个 API,它将跟随者和跟随者推送到用户对象中的两个数组。
我正在尝试使用findOneAndUpdate()它,但它只更新一个对象中的元素。

  let userToFollow = req.body.following;
  let userThatfollows = req.body.follower;

  Users.findOneAndUpdate(
    userToFollow,
    { $push: { followers: userThatfollows } },
    { new: true, upsert: true },
    function (err, doc) {
      console.log(doc);
    }
  );
  Users.findOneAndUpdate(
    userThatfollows,
    { $push: { following: userToFollow } },
    { new: true, upsert: true },
    function (err, doc) {
      console.log(doc);
    }
  );

我不确定这是为什么。

标签: node.jsexpressmongoose

解决方案


推荐阅读