我正在尝试更新文档,如果存在,则创建一个新文档
我已将UserId
设置为唯一值并尝试使用该条件进行更新
let saveUser = User({
UserId: msg.from.id,
FirstName: msg.from.first_name,
LastName: msg.from.last_name,
IsAdmin: false,
Active: true,
EoS: null,
Stake: 0,
Binance: false,
Poloniex: false,
Bittrex: false
});
User.findOneAndUpdate({UserId: msg.from.id}, saveUser, {upsert: true}, function (err, user) {
if (err)
console.log(err);
else
console.log('User registered');
});
这导致以下错误
{ MongoError: Performing an update on the path '_id' would modify the immutable field '_id'
at /Users/orion/NODE/CryptoBot/node_modules/mongodb-core/lib/connection/pool.js:595:61
at authenticateStragglers (/Users/orion/NODE/CryptoBot/node_modules/mongodb-core/lib/connection/pool.js:513:16)
at Connection.messageHandler (/Users/orion/NODE/CryptoBot/node_modules/mongodb-core/lib/connection/pool.js:549:5)
at emitMessageHandler (/Users/orion/NODE/CryptoBot/node_modules/mongodb-core/lib/connection/connection.js:309:10)
at Socket.<anonymous> (/Users/orion/NODE/CryptoBot/node_modules/mongodb-core/lib/connection/connection.js:452:17)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:599:20)
name: 'MongoError',
message: 'Performing an update on the path \'_id\' would modify the immutable field \'_id\'',
ok: 0,
errmsg: 'Performing an update on the path \'_id\' would modify the immutable field \'_id\'',
code: 66,
codeName: 'ImmutableField' }
如何阻止_id
被修改?或者一般如何解决这个问题?
不重复,因为我按照导致此问题的链接中的解决方案