首页 > 解决方案 > MongoDB UpdateOne 没有更新,尽管它给出了成功的消息

问题描述

所以这是我的数据库数据;

"reviewers": [
            {
                "enrolledEventID": [],
                "enrolledId": [],
                "isOrganiser": false,
                "isAdmin": false,
                "forms": [],
                "userNotification": [],
                "isReviewer": true,
                "submissionsToReview": [],
                "_id": "5f926ea7c2cd694070f02c0a",
                "fullname": "Salman",
                "email": "salman1@gmail.com",
                "password": "$2a$10$NdRPKgYOvJt7.G1Ijh6Ii.sJnX2s4M48HyCdfmwU2OQ1JnFOD75LS",
                "__v": 0,
                "avatarImageSource": "https://ui-avatars.com/api/?name=Salman&size=64&rounded=true&background=random",
                "scores": [
                    0,
                    0,
                    0
                ]
            },
            {
                "enrolledEventID": [],
                "enrolledId": [],
                "isOrganiser": false,
                "isAdmin": false,
                "forms": [],
                "userNotification": [],
                "isReviewer": true,
                "submissionsToReview": [],
                "_id": "5f9655935c8ee155e86d723c",
                "fullname": "sadsad",
                "email": "sadsad@sadsad.com",
                "password": "$2a$10$T1yAtGrQy1UZnLoD3Bgm..yw9f2.pQqVsdlB7g5a9KK19TaEvwTNK",
                "__v": 0,
                "avatarImageSource": "https://ui-avatars.com/api/?name=sadsad&size=64&rounded=true&background=random",
                "scores": [
                    3,
                    2,
                    5
                ]
            },
            {
                "enrolledEventID": [],
                "enrolledId": [],
                "isOrganiser": false,
                "isAdmin": false,
                "forms": [],
                "userNotification": [],
                "isReviewer": true,
                "submissionsToReview": [
                    "5f7fe0fba09f2f30189d9b05"
                ],
                "_id": "5f99126693830c0c0ccc6db0",
                "fullname": "sad123",
                "email": "sad123@sad.com"
]

我想更新审稿人的分数,这是我的代码:

Enroll.updateOne(
  {
    _id: enrollID,
    "reviewers._id": userID,
  },
  {
    $set: {
      "reviewers.$.scores": req.body.scores,
    },
  },
  (errUpdate, resultUpdate) => {
    if (errUpdate) {
      return res.status(500).json({ success: false, error: errUpdate });
    } else {
      return res.status(200).json({ success: true, data: resultUpdate });
    }
  }
);

它给出了一条成功消息,但数据没有更新。我还是去查了一下,分数和以前一样。我在哪里犯错?通常 Mongo 错误会给我一个线索,但这次没有。

标签: javascriptmongodb

解决方案


我假设req.body.scores是一系列分数。如果是这样,试试这个:

Enroll.updateOne(
  {
    _id: enrollID
  },
  {
    $set: {
      scores: req.body.scores,
    },
  },
  (errUpdate, resultUpdate) => {
    if (errUpdate) {
      return res.status(500).json({ success: false, error: errUpdate });
    } else {
      return res.status(200).json({ success: true, data: resultUpdate });
    }
  }
);

推荐阅读