查询包含加密字段的文档
带有相等查询的 Queryable Encryption 在 MongoDB 7中正式可用 (GA)。 0及更高版本。 Queryable Encryption 公共预览版,在版本6中发布。 0 ,不再受支持。使用公共预览版加密的数据与功能版本不兼容。有关更多信息,请参阅 MongoDB 7中的兼容性更改。 0 。
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)
上述代码示例的输出应类似于以下内容:
{ "_id": { "$oid": "648b384a722cb9b8392df76a" }, "name": "Jon Doe", "record": { "ssn": "987-65-4320", "billing": { "type": "Visa", "number": "4111111111111111" } }, "__safeContent__": [ { "$binary": { "base64": "L1NsYItk0Sg+oL66DBj6IYHbX7tveANQyrU2cvMzD9Y=", "subType": "00" } } ] }
警告
不要修改 __safeContent__ 字段
__safeContent__
字段对于可查询加密至关重要。请勿修改此字段的内容。