개요
이 가이드 에서는 Queryable Encryption 지원 애플리케이션 을 사용하여 암호화됨 필드가 있는 문서 를 조회 하는 방법을 보여 줍니다.
이 가이드의 단계를 완료하면 애플리케이션을 사용하여 암호화된 필드의 데이터를 쿼리하고 인증된 사용자로 해당 필드를 해독할 수 있습니다.
시작하기 전
계속하기 전에 암호화됨 컬렉션 을 만들고 문서를 삽입 하세요.
절차
동일성으로 암호화됨 필드 쿼리
암호화됨 필드 에 동등성 쿼리를 활성화한 경우 해당 필드 에 지정된 값이 있는 문서를 조회 할 수 있습니다.
다음 예시 에서는 암호화됨 필드 에 대해 동등성 쿼리 를 수행하고 해독된 데이터를 출력합니다.
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);
$findResult = $encryptedCollection->findOne([ 'patientRecord.ssn' => '987-65-4320', ]); print_r($findResult);
find_result = encrypted_collection.find_one({ "patientRecord.ssn": "987-65-4320" }) print(find_result)
let 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"), }
범위 를 사용하여 암호화됨 필드 쿼리
암호화됨 필드 에서 범위 쿼리를 활성화한 경우 해당 필드 값이 지정한 범위 내에 있는 문서를 조회 할 수 있습니다.
다음 예시 에서는 암호화됨 필드 에 대해 범위 쿼리 를 수행하고 해독된 데이터를 출력합니다.
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);
$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)
let 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"), }
접두사, 접미사 또는 하위 문자열 일치에 대한 암호화됨 필드 쿼리
암호화됨 필드 에서 접두사, 접미사 또는 하위 문자열 쿼리를 활성화한 경우 해당 필드 값에 검색 기준에 지정된 문자열이 포함된 문서를 조회 할 수 있습니다.
다음 예시 에서는 암호화됨 필드 에 접두사 쿼리 수행하고 해독된 데이터를 출력합니다.
var filter = new BsonDocument("$expr", new BsonDocument("$encStrStartsWith", new BsonDocument { { "input", "$patientRecord.ssn" }, { "prefix", "987" } })); var findResult = await encryptedCollection.Find(filter).ToCursorAsync(); Console.WriteLine(findResult.FirstOrDefault().ToJson());
접미사 쿼리 수행하려면 $encStrStartsWith 을 $encStrEndsWith로 바꿉니다. 그런 다음 prefix 옵션을 suffix 옵션으로 바꿉니다.
하위 문자열 쿼리 수행하려면 다음 예시 와 같이 $encStrContains 연산자 사용합니다.
var filter = new BsonDocument("$expr", new BsonDocument("$encStrContains", new BsonDocument { { "input", "$patientRecord.ssn" }, { "substring", "-65-432" } })); var findResult = await encryptedCollection.Find(filter).ToCursorAsync(); Console.WriteLine(findResult.FirstOrDefault().ToJson());
Document filter = new Document("$expr", new Document("$encStrStartsWith", new Document() .append("input", "$patientRecord.ssn") .append("prefix", "987"))); Patient findResult = collection.find(filter).first(); System.out.println(findResult);
접미사 쿼리 수행하려면 $encStrStartsWith 을 $encStrEndsWith로 바꿉니다. 그런 다음 prefix 옵션을 suffix 옵션으로 바꿉니다.
하위 문자열 쿼리 수행하려면 다음 예시 와 같이 $encStrContains 연산자 사용합니다.
Document filter = new Document("$expr", new Document("$encStrContains", new Document() .append("input", "$patientRecord.ssn") .append("substring", "-65-432"))); Patient findResult = collection.find(filter).first(); System.out.println(findResult);
const findResult = await encryptedCollection.findOne( { $expr: { $encStrStartsWith: { input: "$patientRecord.ssn", prefix: "987" } } })
접미사 쿼리 수행하려면 $encStrStartsWith 을 $encStrEndsWith로 바꿉니다. 그런 다음 prefix 옵션을 suffix 옵션으로 바꿉니다.
하위 문자열 쿼리 수행하려면 다음 예시 와 같이 $encStrContains 연산자 사용합니다.
const findResult = await encryptedCollection.findOne( { $expr: { $encStrContains: { input: "$patientRecord.ssn", substring: "-65-432" } } })
find_result = encrypted_collection.find_one( { "$expr": { "$encStrStartsWith": { "input": "$patientRecord.ssn", "prefix": "987" } } })
접미사 쿼리 수행하려면 $encStrStartsWith 을 $encStrEndsWith로 바꿉니다. 그런 다음 prefix 옵션을 suffix 옵션으로 바꿉니다.
하위 문자열 쿼리 수행하려면 다음 예시 와 같이 $encStrContains 연산자 사용합니다.
find_result = encrypted_collection.find_one( { "$expr": { "$encStrContains": { "input": "$patientRecord.ssn", "substring": "-65-432" } } })
쿼리 결과
앞의 코드 예제의 출력은 다음과 유사해야 합니다.
{ "_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에 반드시 필요합니다. 이 필드의 내용은 수정하지 마세요.