Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

node.js - Argument passed in must be a single String of 12 bytes

mongoDB collection contains the following data

db.stack.find()
{ "_id" : "8GieRu" }

The _id is not single String of 12 bytes,

As per the MongoDB document of [ObjectID][1], id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.

Using Mongoose this collection is accessed using this Json

{"_id" : new mongoose.Types.ObjectId("8GieRu")}

and throws the below error

/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35
    throw new Error("Argument passed in must be a single String of 12 bytes or
          ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35:11)

  [1]: http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html

Mongoose is strictly checking the ObjectId of fixed length, how can i pass Object_id using mongoose with the given length

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You mix two concepts here.

While "_id" can have any value (even subdocument like {firstName:'Foo',lastName:'Simpson'}, "ObjectId" has a fixed set of types with some restrictions, as the error message correctly states.

So your statement should be

{'_id':'putWhatEverYouWantHere'}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...