nodejs mongodb对象ID转换为字符串 [英] nodejs mongodb object id to string
本文介绍了nodejs mongodb对象ID转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在nodejs中,使用mongodb,mongoosejs作为orm
IN nodejs, with mongodb, mongoosejs as orm
我正在这样做
我有一个模型,用户
User.findOne({username:'someusername'}).exec(function(err,user){
console.log(user) //this gives full object with something like {_id:234234dfdfg,username:'someusername'}
//but
console.log(user._id) //give undefined.
})
为什么?以及如何将_id字符串化呢?
Why? And how to get the _id to string then?
推荐答案
尝试一下:
user._id.toString()
MongoDB ObjectId 是一个12字节的UUID,可用作长度为24个字符的HEX字符串表示形式.您需要使用console.log
将其转换为字符串以显示在console
中.
A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length. You need to convert it to string to show it in console
using console.log
.
因此,您必须执行以下操作:
So, you have to do this:
console.log(user._id.toString());
这篇关于nodejs mongodb对象ID转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文