在 JavaScript 中将 ObjectID (Mongodb) 转换为 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16478552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:13:08  来源:igfitidea点击:

Convert ObjectID (Mongodb) to String in JavaScript

javascriptmongodbaggregation-framework

提问by vhlen

I want to convert ObjectID (Mongodb) to String in JavaScript. When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine. I can't convert to string.

我想在 JavaScript 中将 ObjectID (Mongodb) 转换为 String。当我从 MongoDB 获得对象时。它就像一个对象有:时间戳,秒,公司,机器。我无法转换为字符串。

回答by anubiskong

Try this:

尝试这个:

objectId.str;

See the doc.

请参阅文档

回答by Sammaye

Here is a working example of converting the ObjectIdin to a string

这是将ObjectIdin 转换为字符串的工作示例

> a=db.dfgfdgdfg.findOne()
{ "_id" : ObjectId("518cbb1389da79d3a25453f9"), "d" : 1 }
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'].toString // This line shows you what the prototype does
function () {
    return "ObjectId(" + tojson(this.str) + ")";
}
> a['_id'].str // Access the property directly
518cbb1389da79d3a25453f9
> a['_id'].toString()
ObjectId("518cbb1389da79d3a25453f9") // Shows the object syntax in string form
> ""+a['_id'] 
518cbb1389da79d3a25453f9 // Gives the hex string

Did try various other functions like toHexString()with no success.

确实尝试了各种其他功能toHexString(),但没有成功。

回答by Karl Pokus

in the shell

壳中

ObjectId("507f191e810c19729de860ea").str

ObjectId("507f191e810c19729de860ea").str

in js using the native driver for node

在 js 中使用节点的本机驱动程序

objectId.toHexString()

objectId.toHexString()

回答by user1438797

Acturally, you can try this:

其实你可以试试这个:

> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'] + ''
"518cbb1389da79d3a25453f9"

ObjectId object + String will convert to String object.

ObjectId 对象 + String 将转换为 String 对象。

回答by Jonatas Eduardo

If someone use in Meteorjs, can try:

如果有人在 Meteorjs 中使用,可以尝试:

In server: ObjectId(507f191e810c19729de860ea)._str.

在服务器中:ObjectId(507f191e810c19729de860ea)._str

In template: {{ collectionItem._id._str }}.

在模板中:{{ collectionItem._id._str }}.

回答by William Myers

Assuming the OP wants to get the hexadecimal string value of the ObjectId, using Mongo 2.2 or above, the valueOf()method returns the representation of the object as a hexadecimal string. This is also achieved with the strproperty.

假设 OP 想要获取 ObjectId 的十六进制字符串值,使用 Mongo 2.2 或更高版本,该valueOf()方法将对象的表示返回为十六进制字符串。这也是通过str属性实现的。

The link on anubiskong's post gives all the details, the danger here is to use a technique which has changed from older versions e.g. toString().

anubiskong 帖子上的链接提供了所有详细信息,这里的危险是使用从旧版本更改的技术,例如toString().

回答by Ashh

You can use $toStringaggregation introduced in mongodb version 4.0which converts the ObjectId to string

您可以使用$toStringmongodb 4.0版中引入的聚合,它将 ObjectId 转换为字符串

db.collection.aggregate([
  { "$project": {
    "_id": { "$toString": "$your_objectId_field" }
  }}
])

回答by Vidar

Use toString: var stringId = objectId.toString()

使用 toString: var stringId = objectId.toString()

Works with the latest Node MongoDB Native driver (v3.0+):

适用于最新的 Node MongoDB Native 驱动程序 (v3.0+):

http://mongodb.github.io/node-mongodb-native/3.0/

http://mongodb.github.io/node-mongodb-native/3.0/

回答by ndoty

this works, You have mongodb object: ObjectId(507f191e810c19729de860ea), to get string value of _id, you just say ObjectId(507f191e810c19729de860ea).valueOf();

这有效,你有mongodb对象:ObjectId(507f191e810c19729de860ea),要获取_id的字符串值,你只需说 ObjectId(507f191e810c19729de860ea).valueOf();

回答by Sergio

Just use this : _id.$oid

只需使用这个: _id.$oid

And you get the ObjectId string. This come with the object.

然后你得到 ObjectId 字符串。这随对象而来。