传入的参数必须是12个字节的单个字符串 [英] Argument passed in must be a single String of 12 bytes
问题描述
mongoDB集合包含以下数据
mongoDB collection contains the following data
db.stack.find()
{ "_id" : "8GieRu" }
_id不是12个字节的单个字符串,
The _id is not single String of 12 bytes,
根据[ObjectID] [1]的MongoDB文档, id(字符串)–可以是24字节的十六进制字符串,12字节的二进制字符串或数字.
As per the MongoDB document of [ObjectID][1], id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.
使用猫鼬,可通过此Json访问此集合
Using Mongoose this collection is accessed using this Json
{"_id" : new mongoose.Types.ObjectId("8GieRu")}
并抛出以下错误
/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
猫鼬严格检查固定长度的ObjectId,如何使用给定长度的猫鼬传递Object_id
Mongoose is strictly checking the ObjectId of fixed length, how can i pass Object_id using mongoose with the given length
推荐答案
您在这里混合了两个概念.
You mix two concepts here.
"_ id"可以具有任何值(即使子文档如{firstName:'Foo',lastName:'Simpson'}
,"ObjectId"也具有一组固定的类型,但有一些限制,因为错误消息正确指出了这一点.
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.
所以你的陈述应该是
{'_id':'putWhatEverYouWantHere'}
这篇关于传入的参数必须是12个字节的单个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!