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).FirstOrDefaultAsync();
Console.WriteLine(findResult.ToJson(new JsonWriterSettings { Indent = true }));var findResult PatientDocumenterr = coll.FindOne( context.TODO(), bson.M{"patientRecord.ssn": "987-65-4320"},).Decode(&findResult)if err != nil { fmt.Print("Unable to find the document\n")} else { output, _ := json.MarshalIndent(findResult, "", " ") fmt.Printf("%s\n", output)}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);$findResult = $encryptedCollection->findOne([ 'patientRecord.ssn' => '987-65-4320',]);
print(json_encode($findResult, JSON_PRETTY_PRINT));find_result = encrypted_collection.find_one({ "patientRecord.ssn": "987-65-4320"})
print(find_result)find_result = encrypted_collection.find("patientRecord.ssn" => "987-65-4320").firstputs find_result.inspectlet find_result = encrypted_coll.find_one(doc! {"patientRecord.ssn": "987-65-4320"}).await?;
match find_result { Some(document) => println!("{:?}", document), None => println!("Document not found"),}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 PatientDocumenterr = 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);$findResult = $encryptedCollection->findOne([ 'patientRecord.billAmount' => ['$gt' => 1000, '$lt' => 2000],]);print_r($findResult);query = {"patientRecord.billAmount": {"$gt": 1000, "$lt": 2000}}find_result = encrypted_collection.find_one(query)
print(find_result)find_result = encrypted_collection.find( "patientRecord.billAmount" => { "$gt" => 1000, "$lt" => 2000 }).firstputs find_result.inspectlet query = doc! { "$and": [ doc! { "patientRecord.billAmount": doc! { "$gt": 1000 } }, doc! { "patientRecord.billAmount": doc! { "$lt": 2000 } } ]};let find_result = encrypted_coll.find_one(query).await?;
match find_result { Some(document) => println!("{:?}", document), None => println!("Document not found"),}クエリ結果
上記のコード例の出力は、次のようになります。
{ "_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 に必須です。 このフィールドの内容は変更しないでください。