Skip to content
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

Closed
2 tasks done
ZeddZoul opened this issue Aug 14, 2023 · 5 comments
Closed
2 tasks done

Comments

@ZeddZoul
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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

@ZeddZoul
Copy link
Author

please if the info i provided is not enough you can notify me

@ZeddZoul ZeddZoul reopened this Aug 14, 2023
@MohOraby
Copy link
Contributor

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)

@ZeddZoul
Copy link
Author

ZeddZoul commented Aug 15, 2023 via email

@vkarpov15 vkarpov15 modified the milestones: 7.4.4, 7.4.5 Aug 15, 2023
@vkarpov15
Copy link
Collaborator

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.

@vkarpov15 vkarpov15 removed this from the 7.4.5 milestone Aug 25, 2023
@ZeddZoul
Copy link
Author

ZeddZoul commented Aug 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants