猫鼬对象编号 [英] mongoose objectid to number

查看:83
本文介绍了猫鼬对象编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将ObjectId强制转换为数字?在我的应用中,我在客户端使用最新的猫鼬版本和主干.

How can I cast an ObjectId to a number? In my app I'm using the latest mongoose version and backbone on the clientside.

我的问题是,ObjectId总是总是放在引号之间,导致在我的Jade模板和客户端上产生双引号,例如"233453452534".

My problem is that the ObjectId always ends up being put in between quotes which results in double quotes like ""233453452534"" in my jade templates and on my client.

我正在用以下方式查询mongodb

I'm querying mongodb with

this.users.find({},function(err,docs){
  cb(null,docs)
})

console.log(docs)显示

console.log(docs) shows

{ name: 'test',
 _id: 5220bc207f0c866f18000001,
 __v: 0 }

在我的模板中

option(data-id=val._id) #{val.name}

我将此传递给res.render

i pass this to res.render

res.render('test.jade',docs)

和我的html渲染:

""5220bb43b754af4118000001""

用双引号引起来的是我的对象ID. 我试图在模式中设置一个数字,并且可以正常工作,如果它是数字,则不会用引号引起来,所以我猜是因为这是一个objectID,所以会发生这种情况.

with double quotes arround my object id. i tried to set a number in a schema and this works, no quotes arround it if it's a number, so my guess is that that happens because it's an objectID.

推荐答案

尝试使用虚拟ID代替_id

try using the virtual id onstead of _id

option(data-id=val.id) #{val.name}

代替

option(data-id=val._id) #{val.name}

这篇关于猫鼬对象编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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