MongoServerError:findAndModify :: 期间计划执行器错误:由 :: 在路径“_id”上执行更新将修改不可变字段_id

问题描述 投票:0回答:2

每次我的命令结束时,我都会收到此错误。我不知道该怎么做,命令 Gas 之前一直在工作,但当我添加一个更新另一个数组的新函数时,它就走下坡路了。

MongoServerError: Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id'

at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:230:30)

at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:61:60)

at MessageStream.emit (node:events:527:28)

at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:125:16)

at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)

at writeOrBuffer (node:internal/streams/writable:390:12)

at _write (node:internal/streams/writable:331:10)

at MessageStream.Writable.write (node:internal/streams/writable:335:10)

at TLSSocket.ondata (node:internal/streams/readable:777:22)

at TLSSocket.emit (node:events:527:28) {

ok: 0,

code: 66,

codeName: 'ImmutableField',

'$clusterTime': {

clusterTime: new Timestamp({ t: 1678502007, i: 4 }),

signature: {

hash: new Binary(Buffer.from("6d07aad2ad2644b91f653725c0f9ffbbcd035be7", "hex"), 0),

keyId: new Long("7148511101804609538")

}

},

operationTime: new Timestamp({ t: 1678502007, i: 4 }),

[Symbol(errorLabels)]: Set(0) {}

}

我添加的功能:

async function task(card, damage, interaction) {

  let profile = await profileModel.findOne({

    user: card.userID,

  });

  if (profile.task.length === 0) return;

  profile.task.forEach((e, i) => {

    if (e.complete !== true && e.type === "damage") {

      e.damageDone += damage;

    }

  });

  profile.markModified("task");

  await profileModel.findOneAndUpdate({ user: interaction.user.id }, profile);

  return profile;

}


代码:

await profileModel.findOneAndUpdate(

              {

                user: winner,

              },

              {

                $inc: {

                  coins: bet,

                },

              }

            );

            await profileModel.findOneAndUpdate(

              {

                user: loser,

              },

              {

                $inc: {

                  coins: -bet,

                },

              }

            );

            await interaction.followUp({

              embeds: [endEmbed],

            });

            if (winner === interaction.user.id) {

              if (profile.guild) {

                let guild = await guildModel.findOne({

                  name: profile.guild,

                });

                guild.chestXP += 15;

                await profileModel.findOneAndUpdate(

                  {

                    user: interaction.user.id,

                  },

                  profile

                );

              }

              if (profile.task.length === 0) return;

              let profileTask = profile.task.filter(

                (v) =>

                  v.complete !== true && v.type === "badge" && v.id === "bc"

              );

              if (profileTask.length === 0) return;

              profileTask.forEach(async (element, int) => {

                let task = element;

                task.progress += 1;

                await profileModel.findOneAndUpdate(

                  {

                    user: interaction.user.id,

                  },

                  profile

                );

              });

            }

          }

尝试解决此问题并不再出现错误。

javascript mongodb mongoose discord.js
2个回答
2
投票

我认为将整个数据对象放入更新参数(profile)会导致此错误

await profileModel.findOneAndUpdate(

  {

    user: interaction.user.id,

  },

  profile

);

仅尝试在更新参数中包含必要的更新字段。它将解决您未来的麻烦。


0
投票

我的代码是

 const amenity = await Amenity.findOneAndUpdate(
    { _id: req.params.id },
  );

而不是

 const amenity = await Amenity.findOneAndUpdate(
    { _id: req.params.id },
    req.body,
    {
      new: true,
      runValidators: true,
    }
  );
© www.soinside.com 2019 - 2024. All rights reserved.