Node.js Mongoose.js 字符串到 ObjectId 函数 [英] Node.js Mongoose.js string to ObjectId function

查看:37
本文介绍了Node.js Mongoose.js 字符串到 ObjectId 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用 mongoose 将节点中的字符串转换为 objectId 的函数?模式指定某物是一个 ObjectId,但是当它从字符串中保存时,mongo 告诉我它仍然只是一个字符串.例如,对象的 _id 显示为 objectId("blah").

Is there a function to turn a string into an objectId in node using mongoose? The schema specifies that something is an ObjectId, but when it is saved from a string, mongo tells me it is still just a string. The _id of the object, for instance, is displayed as objectId("blah").

推荐答案

你可以这样做:

var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');

这篇关于Node.js Mongoose.js 字符串到 ObjectId 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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