Skip to content

Commit 6ce4b6d

Browse files
ricardohsmellomp911de
authored andcommitted
Clarify manual vs. derived QE setup and add example project link.
Added explanation of when to use manual vs. derived Queryable Encryption configuration. Included a brief description of each field's encryption and query behavior in the Patient example. Also added a link to a GitHub project with a complete working example. Signed-off-by: Ricardo Mello <[email protected]> Closes #5016
1 parent 61cbfb5 commit 6ce4b6d

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/main/antora/modules/ROOT/pages/mongodb/mongo-encryption.adoc

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ The information covers the algorithm in use as well as allowed query types along
133133
`MongoOperations#createCollection(...)` can be used to do the initial setup for collections utilizing QE.
134134
The configuration for QE via Spring Data uses the same building blocks (a xref:mongodb/mapping/mapping-schema.adoc#mongo.jsonSchema.encrypted-fields[JSON Schema creation]) as CSFLE, converting the schema/properties into the configuration format required by MongoDB.
135135

136+
You can configure Queryable Encryption either manually or in a derived way:
137+
138+
- Manual setup gives you full control over how encrypted fields are declared and how collections are created. It's useful when you need to explicitly manage data keys, encryption algorithms, and field mappings.
139+
- Derived setup relies on annotations in your domain model and automatically generates the required encrypted field configuration from it. This is simpler and recommended for typical Spring applications where your data model is already annotated.
140+
136141
[tabs]
137142
======
138143
Manual Collection Setup::
@@ -165,30 +170,43 @@ Derived Collection Setup::
165170
----
166171
class Patient {
167172
168-
@Id String id;
173+
@Id String id; <1>
174+
175+
Address address; <1>
176+
177+
@Encrypted(algorithm = "Unindexed")
178+
String pin; <2>
169179
170180
@Encrypted(algorithm = "Indexed")
171181
@Queryable(queryType = "equality", contentionFactor = 0)
172-
String ssn;
182+
String ssn; <3>
173183
174184
@RangeEncrypted(contentionFactor = 8, rangeOptions = "{ 'min' : 0, 'max' : 150 }")
175-
Integer age;
185+
Integer age; <4>
176186
177-
@Encrypted(algorithm = "Unindexed")
178-
String pin;
179-
180-
Address address;
187+
@RangeEncrypted(contentionFactor = 0L,
188+
rangeOptions = "{\"min\": {\"$numberDouble\": \"0.3\"}, \"max\": {\"$numberDouble\": \"2.5\"}, \"precision\": 2 }")
189+
double height; <5>
181190
}
182191
183192
MongoJsonSchema patientSchema = MongoJsonSchemaCreator.create(mappingContext)
184193
.filter(MongoJsonSchemaCreator.encryptedOnly())
185194
.createSchemaFor(Patient.class);
186195
187-
CollectionOptions collectionOptions = CollectionOptions.encryptedCollection(patientSchema);
196+
Document encryptedFields = CollectionOptions.encryptedCollection(patientSchema)
197+
.getEncryptedFieldsOptions()
198+
.map(CollectionOptions.EncryptedFieldsOptions::toDocument)
199+
.orElseThrow();
200+
201+
template.execute(db -> clientEncryption.createEncryptedCollection(db, template.getCollectionName(Patient.class), new CreateCollectionOptions()
202+
.encryptedFields(encryptedFields), new CreateEncryptedCollectionParams("local"))); <1>
188203
189-
mongoTemplate.createCollection(Patient.class, collectionOptions); <1>
190204
----
191-
<1> Using the template to create the collection may prevent capturing generated keyIds. In this case render the `Document` from the options and use the `createEncryptedCollection(...)` method via the encryption library.
205+
<1> id and address are not encrypted and can be queried normally.
206+
<2> pin is encrypted but does not support queries.
207+
<3> ssn is encrypted and allows equality queries.
208+
<4> age is encrypted and allows range queries between 0 and 150.
209+
<5> height is encrypted and allows range queries between 0.3 and 2.5.
192210

193211
The `Queryable` annotation allows to define allowed query types for encrypted fields.
194212
`@RangeEncrypted` is a combination of `@Encrypted` and `@Queryable` for fields allowing `range` queries.
@@ -250,6 +268,8 @@ MongoDB Collection Info::
250268
- Additional options for eg. `min` and `max` need to match the actual field type. Make sure to use `$numberLong` etc. to ensure target types when parsing bson String.
251269
- Queryable Encryption will an extra field `__safeContent__` to each of your documents.
252270
Unless explicitly excluded the field will be loaded into memory when retrieving results.
271+
- For a complete example, see Github project:
272+
https://github.com/mongodb-developer/spring-data-queryable-encryption[spring-data-queryable-encryption]
253273
====
254274

255275
[[mongo.encryption.queryable.automatic]]

0 commit comments

Comments
 (0)