Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

$hash(集計式)

バージョン8.3の新機能

$hash

Generates and returns a binary hash value (BinData) from a UTF-8 string or binary data.集計パイプラインで $hash を使用して、ストレージ、検証、または比較用の binary ハッシュを計算します。binary データではなく16進数文字列を取得するには、$hexHash を使用します。

$hash の構文は次のとおりです。

{
$hash: {
input: <expression>,
algorithm: <string>
}
}
フィールド
タイプ
説明

input

必須。ハッシュを計算する値。有効な UTF-8 string または BinData に解決される必要があります。

algorithm

文字列

必須。ハッシュアルゴリズム。指定できる値は以下のとおりです。

  • "md5"

  • "sha256"

  • "xxh64"

input
結果
ノート

UTF-8 string

input は有効な UTF-8 string である必要があります。

null、未定義、または欠落しているフィールド

null

inputnull または未定義に解決されるか、欠落しているフィールドを参照する場合、$hashnull を返します。

input が UTF-8 string または BinData 以外のタイプに解決される場合、$hash はエラーを返します。

algorithm が許容値の 1 つではない場合、$hash はエラーを返します。

重要

MD5 は暗号化された安全なアルゴリズムではないため、セキュリティに注意が必要なアプリケーションには適していません。機密データをハッシュしている場合は、代わりに "sha256" を使用します。

MD5 も FIPSモードでは無効になっています。配置が FIPSモードを有効にして実行されている場合は、代わりに "sha256" または "xxh64" を使用します。

次の例では、これらのdocumentを含む files という名前のコレクションを使用します。

db.files.insertMany( [
{ _id: 1, filename: "report.pdf" },
{ _id: 2, filename: "archive.zip" }
] )

次の例では、各 document の filenameフィールドの SHA-256 ハッシュを計算します。

db.files.aggregate( [
{
$project: {
filename: 1,
hash: {
$hash: {
input: "$filename",
algorithm: "sha256"
}
}
}
}
] )
[
{
_id: 1,
filename: 'report.pdf',
hash: Binary.createFromBase64('ZGbkUKFrd7hlxYKda2xW2fiSlWR1ZC2+uczENA/gGxU=', 0)
},
{
_id: 2,
filename: 'archive.zip',
hash: Binary.createFromBase64('jsO6p+AcJW/43CrXux9Tpmq+hHLmCKPmqJjcARAsmVA=', 0)
}
]

次の例では、リテラルの string 値をハッシュします。

db.aggregate( [
{ $documents: [ { val: "hello" } ] },
{
$project: {
_id: 0,
hash: {
$hash: {
input: "$val",
algorithm: "xxh64"
}
}
}
}
] )
[
{
hash: Binary.createFromBase64('JseCfYifbaM=', 0)
}
]

次の例では、binary データフィールドの SHA-256 ハッシュを計算します。

db.binaries.insertMany( [
{ _id: 1, data: new BinData(0, "aGVsbG8=") }
] )
db.binaries.aggregate( [
{
$project: {
hash: {
$hash: {
input: "$data",
algorithm: "sha256"
}
}
}
}
] )
[
{
_id: 1,
hash: Binary.createFromBase64('LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=', 0)
}
]

inputnull であるか、 欠落しているフィールドを参照している場合、$hashnull を返します。

db.aggregate( [
{
$documents: [
{ val: null },
{}
]
},
{
$project: {
hash: {
$hash: {
input: "$val",
algorithm: "sha256"
}
}
}
}
] )
[
{
hash: null
},
{
hash: null
}
]

戻る

$gte

項目一覧