-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cast to ObjectId failed for value "0" (type number) at path "_id" because of "BSONError" #13732
Comments
please if the info i provided is not enough you can notify me |
the |
It was "id" not "_id"
…On Tue, Aug 15, 2023, 4:11 PM Mohamed Oraby ***@***.***> wrote:
the _id field in Mongo has some requirements, like it should be unique
and it should be 24 characters long (hexadecimal not just any characters)
—
Reply to this email directly, view it on GitHub
<#13732 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARL6I35IDXTR2LDLDCJB5QLXVOGRHANCNFSM6AAAAAA3P2VZGY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
I took a look, the issue is that, in 7.4.0, Mongoose introduced a setter for We're going to reconsider this particular setter for our next major release. |
Ok, thanks for the update.
…On Fri, Aug 25, 2023 at 3:39 PM Valeri Karpov ***@***.***> wrote:
I took a look, the issue is that, in 7.4.0, Mongoose introduced a setter
for id, so Mongoose takes the id field from your data and sets it as _id,
and then tries to cast 9 to an ObjectId.
We're going to reconsider this particular setter for our next major
release.
—
Reply to this email directly, view it on GitHub
<#13732 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARL6I36DDYKZU77IVOSAMCDXXC2KHANCNFSM6AAAAAA3P2VZGY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
Prerequisites
Mongoose version
7.4.2
Node.js version
18.14.2
MongoDB server version
6.0.8
Typescript version (if applicable)
No response
Description
I have a JSON file with an array of objects which have an id property like this from 0 to 8
{ "id": 0, "name": "Hiker", "duration": 5, "Size": 25, }
I read the data from the file using
const text= fs.readfile("./file.json", utf-8)
I then converted it to an object using
const data = JSON.parse(text)
I created a fxn to import the file using
Model.create(data)
but I got this error
//////////
Microsoft Windows [Version 10.0.19045.3324]
(c) Microsoft Corporation. All rights reserved.
C:\Users\zedd\Desktop\Learning\learn-node.js\Express>node import-data.js --import
🎇🎇🎇 Error: Tour validation failed: _id: Cast to ObjectId failed for value "9" (type number) at path "_id" because of "BSONError"
at ValidationError.inspect (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\error\validation.js:50:26)
at formatValue (node:internal/util/inspect:806:19)
at inspect (node:internal/util/inspect:365:10)
at formatWithOptionsInternal (node:internal/util/inspect:2273:40)
at formatWithOptions (node:internal/util/inspect:2135:10)
at console.value (node:internal/console/constructor:340:14)
at console.log (node:internal/console/constructor:377:61)
at importData (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\import-data.js:24:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: {
_id: CastError: Cast to ObjectId failed for value "9" (type number) at path "_id" because of "BSONError"
at ObjectId.cast (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\schema\objectid.js:250:11)
at SchemaType.applySetters (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\schematype.js:1220:12)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1411:22)
at model.set [as _id] (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\helpers\document\compile.js:205:19)
at model.idSetter (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\helpers\schema\idGetter.js:44:12)
at VirtualType.applySetters (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\virtualtype.js:166:16)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1271:12)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1113:16)
at model.Document (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:164:12)
at model.Model (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\model.js:123:12) {
stringValue: '"9"',
messageFormat: undefined,
kind: 'ObjectId',
value: 9,
path: '_id',
reason: BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new ObjectId (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\bson@5.4.0\node_modules\bson\lib\bson.cjs:2006:23) at castObjectId (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\cast\objectid.js:25:12)
at ObjectId.cast (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\schema\objectid.js:248:12)
at SchemaType.applySetters (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\schematype.js:1220:12)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1411:22)
at model.set [as _id] (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\helpers\document\compile.js:205:19)
at model.idSetter (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\helpers\schema\idGetter.js:44:12)
at VirtualType.applySetters (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\virtualtype.js:166:16)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1271:12)
at model.$set (C:\Users\zedd\Desktop\Learning\learn-node.js\Express\node_modules.pnpm\mongoose@7.4.2\node_modules\mongoose\lib\document.js:1113:16),
valueType: 'number'
}
},
_message: 'Tour validation failed'
}
number
///////////////////////////////////////////
removing the "id" field clears the error and increasing the length of integer to 12 characters clears it too. But isn't mongoose supposed to be able to cast integers into OID?
HERE IS MY CODE
import-data.js.txt
Steps to Reproduce
I have a JSON file with an array of objects which have an id property like this from 0 to 8
{ "id": 0, "name": "Hiker", "duration": 5, "Size": 25, }
I read the data from the file using
const text= fs.readfile("./file.json", utf-8)
I then converted it to an object using
const data = JSON.parse(text)
I created a fxn to import the file using
Model.create(data)
but I got this error
Expected Behavior
{ "id": 0, }
to be replaced in database with
{ "_id": objectId(0) }
or something like that
The text was updated successfully, but these errors were encountered: