Database Manual / Reference / mongosh Methods / Object Constructors

Binary.createFromHexString() (mongosh method方法)

Definition定义

Creates a binary object from a hexadecimal value.从十六进制值创建二进制对象。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

Binary.createFromHexString( <hexadecimalString> )

The hexadecimalString field specifies a string that contains a hexadecimal value. For example, "64c13ab08edf48a008793cac".hexadecimalString字段指定一个包含十六进制值的字符串。例如,"64c13ab08edf48a008793cac"

Examples示例

The following examples show how to add a binary object to a document using Binary.createFromHexString() and how the binary object appears in the output when retrieved.以下示例显示了如何使用Binary.createFromHexString()将二进制对象添加到文档中,以及检索时二进制对象如何显示在输出中。

Create Collection Containing Document with Binary Object使用二进制对象创建包含文档的集合

The following example creates a collection named binaryObjectsFromHexString:以下示例创建了一个名为binaryObjectsFromHexString的集合:

db.binaryObjectsFromHexString.insertOne( {
_id: 0,
binaryObject: Binary.createFromHexString( "64c13ab08edf48a008793cac" )
} )

The binaryObject field contains the binary object created from the string specified in Binary.createFromHexString().binaryObject字段包含根据Binary.createFromHexString()中指定的字符串创建的二进制对象。

Retrieve Document from Collection with Binary Object使用二进制对象从集合中检索文档

The following example retrieves the document:以下示例检索文档:

db.binaryObjectsFromHexString.findOne( { _id: 0 } )

Note

Starting in mongosh 2.0.0, binary values are shown as Binary.createFromBase64( <base64String> ) values instead of Binary( Buffer.from( <base64String> ) ) values. This only changes the display of binary values.mongosh 2.0.0开始,二进制值显示为Binary.createFromBase64( <base64String> )值,而不是Binary( Buffer.from( <base64String> ) )值。这只会更改二进制值的显示。

Example output, which shows the number in base64:示例输出,显示base64格式的数字:

{
_id: 0,
binaryObject: Binary.createFromBase64("ZME6sI7fSKAIeTys")
}