mongoose: Updating the path 'x' would create a conflict at 'x'
Do you want to request a feature or report a bug? bug
What is the current behavior? mongoose throws an error when executing following query, when executed with
const result = await this.jobAlarmModel
.findOneAndUpdate(query, update, {
returnDocument: 'after',
upsert: true
})
.lean()
.exec();
QUERY {
"targets": {
"$elemMatch": {
"type": "user",
"params": "58d1310c6162444627d0431e" // ObjectId
}
},
"subscriptions._id": "612e07ec24dd373ff6ed7f53" // ObjectId
}
update {
"$unset": {
"deactivated": true
},
"$setOnInsert": {
"interval": 12,
"lastCheck": "2021-08-31T11:12:32.635Z",
"targets": [
{
"type": "user",
"params": "58d1310c6162444627d0431e" // ObjectId
}
]
},
"$set": {
"subscriptions.$": {
"search": {
"region": "AT",
"filters": [],
"_location": "57feb684ebca08c825327731",
"locationCoords": {
"lon": 16.374,
"lat": 48.208
},
"_field": [
"55d05b8c315478e647148bea",
"55d06b4c347584e641746bea",
"55d14b9c378694e682645bea",
"55d20b2c362610e685346bea",
"55d21b3c390229e676643bea",
"55d25b2c316398e653743bea",
"55d27b6c394742e606746bea",
"55d31b1c365318e683848bea",
"55d52b3c347314e611042bea",
"55d56b7c310848e678649bea",
"55d58b1c377013e659243bea",
"55d59b7c347254e692244bea",
"55d6389d365315e629446c08",
"55d638ae365315e629446c09",
"55d638f4365315e629446c0a",
"55d6391c365315e629446c0c",
"55d63b68365315e629446c16",
"55d66b5c319117e666745bea",
"55d78b1c322056e639043bea",
"55d81b8c304579e699343bea",
"55d84b5c308150e679540bea",
"55d88b8c326897e669748bea",
"55d97b5c372222e635748bea",
"55d98b6c305015e673045bea",
"569e8adb1142e6fb6f096887",
"569e8aea1142e6fb6f096888",
"569e8b021142e6fb6f09688b",
"569e8b0b1142e6fb6f09688c",
"56b35442b5c5f2a71436dc56",
"56b3544db5c5f2a71436dc57",
"56b35456b5c5f2a71436dc58"
]
},
"lastResult": "2021-08-31T11:12:32.635Z",
"lastView": "2021-08-31T11:12:32.635Z",
"createdAt": "2021-08-31T11:12:32.635Z",
"_id": "612e07ec24dd373ff6ed7f53" // ObjectId
}
}
}
throws
MongoServerError: Updating the path 'subscriptions' would create a conflict at 'subscriptions'
at MessageStream.messageHandler (/home/simon/Dev/hokify/hokify-server/node_modules/mongodb/src/cmap/connection.ts:740:20)
at MessageStream.emit (node:events:394:28)
at MessageStream.emit (node:domain:475:12)
at processIncomingData (/home/simon/Dev/hokify/hokify-server/node_modules/mongodb/src/cmap/message_stream.ts:167:12)
at MessageStream._write (/home/simon/Dev/hokify/hokify-server/node_modules/mongodb/src/cmap/message_stream.ts:64:5)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:394:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
ok: 0,
code: 40,
codeName: 'ConflictingUpdateOperators',
operationTime: new Timestamp({ t: 1630408352, i: 257 }),
'$clusterTime': {
clusterTime: new Timestamp({ t: 1630408352, i: 257 }),
signature: {
hash: new Binary(Buffer.from("b082c2eec59a4c328f73ac552f65029e2bb0b39a", "hex"), 0),
keyId: new Long("6963739298038284312")
}
}
}
but calling it with the native collection, it works:
const result = await this.jobAlarmModel
.collection
.findOneAndUpdate(query, update, {
returnDocument: 'after',
upsert: true
});
=> no error at all.
{
lastErrorObject: { n: 1, updatedExisting: true },
value: {
_id: new ObjectId("612e07ec24dd373ff6ed7f54"),
__v: 0,
createdAt: 2021-08-31T10:43:56.378Z,
interval: 12,
lastCheck: 2021-08-31T10:43:56.373Z,
subscriptions: [ [Object] ],
targets: [ [Object] ],
updatedAt: 2021-08-31T10:43:56.378Z
},
ok: 1,
operationTime: new Timestamp({ t: 1630408283, i: 22 }),
'$clusterTime': {
clusterTime: new Timestamp({ t: 1630408283, i: 22 }),
signature: {
hash: new Binary(Buffer.from("87cdc921cb1ad264b0e8858ffbcb9c7722b1c00e", "hex"), 0),
keyId: new Long("6963739298038284312")
}
}
}
it seems mongoose does some “bad” magic with the query 😉. Works with mongosoe 5.x, but not with mongoose 6.x
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. mongoose 6.0.3 node v16.7.0 mongodb 4.4.1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (4 by maintainers)
Commits related to this issue
- fix(setDefaultsOnInsert): avoid adding unnecessary auto `_id` to $setOnInsert Re: #10646 — committed to Automattic/mongoose by vkarpov15 3 years ago
this seems to have popped back up in v6.3.X, had to downgrade to 6.1.10 to prevent the error