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" } } ]}警告
不要修改 __safeContent__ 字段
__safeContent__ 字段对于 Queryable Encryption 至关重要。请勿修改此字段的内容。