节点。js猫鼬。js字符串到objective函数

时间:2022-10-05 02:35:06

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").

是否有一个函数可以使用mongoose将一个字符串转换为一个objective ?模式指定某个东西是一个ObjectId,但是当它从字符串中保存时,mongo告诉我它仍然是一个字符串。例如,对象的_id显示为objectId(“blah”)。

5 个解决方案

#1


279  

You can do it like so:

你可以这样做:

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

#2


10  

You can do it like this:

你可以这样做:

var mongoose = require('mongoose');
var _id = mongoose.mongo.BSONPure.ObjectID.fromHexString("4eb6e7e7e9b7f4194e000001");

EDIT: New standard has fromHexString rather than fromString

编辑:新标准使用fromHexString而不是fromString

#3


6  

Judging from the comments, you are looking for:

从评论来看,你在寻找:

mongoose.mongo.BSONPure.ObjectID.isValid

Or

mongoose.Types.ObjectId.isValid

#4


2  

var mongoose = require('mongoose');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");

#5


2  

I couldn't resolve this method (admittedly I didn't search for long)

我无法解决这个方法(不可否认,我没有搜索很长时间)

mongoose.mongo.BSONPure.ObjectID.fromHexString

If your schema expects the property to be of type ObjectId, the conversion is implicit, at least this seems to be the case in 4.7.8.

如果您的模式期望属性是objective - d类型,那么转换是隐式的,至少在4.7.8中是这样的。

You could use something like this however, which gives a bit more flex:

你可以使用类似这样的东西,它提供了更多的弹性:

function toObjectId(ids) {

    if (ids.constructor === Array) {
        return ids.map(mongoose.Types.ObjectId);
    }

    return mongoose.Types.ObjectId(ids);
}

#1


279  

You can do it like so:

你可以这样做:

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

#2


10  

You can do it like this:

你可以这样做:

var mongoose = require('mongoose');
var _id = mongoose.mongo.BSONPure.ObjectID.fromHexString("4eb6e7e7e9b7f4194e000001");

EDIT: New standard has fromHexString rather than fromString

编辑:新标准使用fromHexString而不是fromString

#3


6  

Judging from the comments, you are looking for:

从评论来看,你在寻找:

mongoose.mongo.BSONPure.ObjectID.isValid

Or

mongoose.Types.ObjectId.isValid

#4


2  

var mongoose = require('mongoose');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");

#5


2  

I couldn't resolve this method (admittedly I didn't search for long)

我无法解决这个方法(不可否认,我没有搜索很长时间)

mongoose.mongo.BSONPure.ObjectID.fromHexString

If your schema expects the property to be of type ObjectId, the conversion is implicit, at least this seems to be the case in 4.7.8.

如果您的模式期望属性是objective - d类型,那么转换是隐式的,至少在4.7.8中是这样的。

You could use something like this however, which gives a bit more flex:

你可以使用类似这样的东西,它提供了更多的弹性:

function toObjectId(ids) {

    if (ids.constructor === Array) {
        return ids.map(mongoose.Types.ObjectId);
    }

    return mongoose.Types.ObjectId(ids);
}