传入的参数必须是单个 12 字节的 String [英] Argument passed in must be a single String of 12 bytes

查看:21
本文介绍了传入的参数必须是单个 12 字节的 String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 (string) – 可以是 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.

使用 Mongoose 使用此 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

Mongoose 正在严格检查固定长度的 ObjectId,我如何使用给定长度的 mongoose 传递 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 字节的 String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆