Docs Menu
Docs Home
/
MongoDB マニュアル
/ / / / / /

暗号化されたフィールドを持つドキュメントのクエリ

項目一覧

  • Overview
  • 始める前に
  • 手順
  • 等価の暗号化フィールドのクエリ
  • 範囲指定された暗号化されたフィールドのクエリ

このガイドでは、Queryable Encryption 対応のアプリケーションを使用して、暗号化されたフィールドを持つドキュメントを検索する方法について説明します。

このガイドの手順を完了すると、アプリケーションを使用して暗号化されたフィールド内のデータをクエリし、それらのフィールドを承認されたユーザーとして復号化できるようになります。

続行する前に暗号化されたコレクションを作成し、ドキュメントを挿入します

1

暗号化されたフィールドで等価クエリを有効にした場合は、そのフィールドに指定された値を持つドキュメントを検索できます。

次の例では、暗号化されたフィールドに対して等価クエリを実行し、復号化されたデータを出力します。

const findResult = await encryptedCollection.findOne({
"patientRecord.ssn": "987-65-4320",
});
console.log(findResult);
var ssnFilter = Builders<Patient>.Filter.Eq("patientRecord.ssn", patient.PatientRecord.Ssn);
var findResult = await encryptedCollection.Find(ssnFilter).ToCursorAsync();
Console.WriteLine(findResult.FirstOrDefault().ToJson());
var findResult PatientDocument
err = coll.FindOne(
context.TODO(),
bson.M{"patientRecord.ssn": "987-65-4320"},
).Decode(&findResult)
Patient findResult = collection.find(
new BsonDocument()
.append("patientRecord.ssn", new BsonString("987-65-4320")))
.first();
System.out.println(findResult);
const findResult = await encryptedCollection.findOne({
"patientRecord.ssn": "987-65-4320",
});
console.log(findResult);
find_result = encrypted_collection.find_one({
"patientRecord.ssn": "987-65-4320"
})
print(find_result)
2

暗号化されたフィールドで範囲クエリを有効にした場合、そのフィールドの値が指定した範囲内にあるドキュメントを検索できます。

次の例では、暗号化されたフィールドに対して範囲クエリを実行し、復号化されたデータを出力します。

const findResult = await encryptedCollection.findOne({
"patientRecord.billAmount": { $gt: 1000, $lt: 2000 },
});
console.log(findResult);
var filter = Builders<Patient>.Filter.Gt("patientRecord.billAmount", 1000) &
Builders<Patient>.Filter.Lt("patientRecord.billAmount", 2000);
var findResult = encryptedCollection.Find(filter).FirstOrDefault();
Console.WriteLine(findResult.ToJson());
filter := bson.D{
{"patientRecord.billAmount", bson.D{
{"$gt", 1000},
{"$lt", 2000},
}},
}
var findResult PatientDocument
err = coll.FindOne(context.TODO(), filter).Decode(&findResult)
if err != nil {
log.Fatal(err)
}
if err != nil {
fmt.Print("Unable to find the document\n")
} else {
output, _ := json.MarshalIndent(findResult, "", " ")
fmt.Printf("%s\n", output)
}
Document filter = new Document("patientRecord.billAmount",
new Document("$gt", 1000).append("$lt", 2000));
Patient findResult = collection.find(filter).first();
System.out.println(findResult);
const findResult = await encryptedCollection.findOne({
"patientRecord.billAmount": { $gt: 1000, $lt: 2000 },
});
console.log(findResult);
query = {"patientRecord.billAmount": {"$gt": 1000, "$lt": 2000}}
find_result = encrypted_collection.find_one(query)
print(find_result)

上記のコード例の出力は、次のようになります。

{
"_id": {
"$oid": "648b384a722cb9b8392df76a"
},
"name": "Jon Doe",
"record": {
"ssn": "987-65-4320",
"billing": {
"type": "Visa",
"number": "4111111111111111"
},
"billAmount": 1500
},
"__safeContent__": [
{
"$binary": {
"base64": "L1NsYItk0Sg+oL66DBj6IYHbX7tveANQyrU2cvMzD9Y=",
"subType": "00"
}
}
]
}

警告

MongoDB_ENUS_JAJP フィールドを変更しないでください

__safeContent__フィールドは Queryable Encryption に必須です。 このフィールドの内容は変更しないでください。

戻る

コレクションを作成する