【Spring连载】使用Spring Data访问 MongoDB----Template API 查询Documents
你可以使用Query和Criteria类来表达查询。它们的方法名称反映了native MongoDB操作名称,如lt、lte、is和其他名称。Query和Criteria类遵循fluent API风格,因此你可以将多个方法标准和查询链接在一起,同时拥有易于理解的代码。为了提高可读性,静态导入可以避免在创建查询和条件实例时使用“new”关键字。你还可以使用BasicQuery从纯JSON字符串创建Query实例,如下例所示:
例1:从纯JSON字符串创建查询实例
BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}");
List<Person> result = mongoTemplate.find(query, Person.class);
一、 查询集合中的Documents
在之前的章节,我们看到了如何在MongoTemplate上使用findOne和findById方法来检索单个文档。这些方法以正确的方式返回单个域对象,或者使用响应式(reactive )API Mono发出单个元素。我们还可以查询要作为域对象列表返回的documents集合。假设我们在一个集合中有许多具有姓名和年龄的Person对象作为documents存储,并且每个人都有一个带有余额的内嵌的帐户document,我们现在可以使用以下代码运行查询:
使用MongoTemplate查询文档
// ...
List<Person> result = template.query(Person.class)
.matching(query(where("age").lt(50).and("accounts.balance").gt(1000.00d)))
.all();
所有find方法都将Query对象作为参数。此对象定义用于执行查询的条件和选项。通过使用Criteria对象来指定条件,该对象具有一个名为where的静态工厂方法来实例化新的Criteria。我们建议对org.springframework.data.mongodb.core.query.Criteria.where和Query.query使用静态导入,使查询更具可读性。
查询应该返回符合指定条件的Person对象的List或Flux。本节的剩余部分列出了与MongoDB中提供的运算符相对应的Criteria和Query类的方法。大多数方法返回Criteria对象,为API提供流畅的样式。
Criteria的方法
Criteria类提供了以下方法,所有这些方法都对应于MongoDB中的运算符:
- Criteria all (Object o)使用$all运算符创建一个条件
- Criteria and (String key)将具有指定key的链式Criteria添加到当前Criteria并返回新创建的Criteria
- Criteria andOperator (Criteria… criteria)使用$and运算符为所有提供的条件创建and查询
- Criteria andOperator (Collection< Criteria> criteria)使用$and运算符为所有提供的条件创建and查询
- Criteria elemMatch (Criteria c)使用$elemMatch运算符创建条件
- Criteria exists (boolean b)使用$exists运算符创建条件
- Criteria gt (Object o)使用$gt运算符创建条件
- Criteria gte (Object o)使用$gte运算符创建条件
- Criteria in (Object… o) 使用$in运算符为varargs参数创建一个条件。
- Criteria in (Collection<?> collection)使用$in运算符并使用集合创建条件
- Criteria is (Object o)使用字段匹配({key:value})创建条件。如果指定的值是一个document,则字段的顺序和document中的完全相等性很重要。
- Criteria lt (Object o)使用$lt运算符创建条件
- Criteria lte (Object o)使用$lte运算符创建一个条件
- Criteria mod (Number value, Number remainder)使用$mod运算符创建一个条件
- Criteria ne (Object o)使用$ne运算符创建条件
- Criteria nin (Object… o)使用$nin运算符创建条件
- Criteria norOperator (Criteria… criteria)为所有提供的条件使用$nor运算符创建nor查询
- Criteria norOperator(Collection<Criteria>Criteria)为所有提供的条件使用$nor运算符创建nor查询
- Criteria not ()使用$not元运算符创建一个条件,该运算符直接影响后面的子句
- Criteria orOperator (Criteria… criteria)为所有提供的条件使用$or运算符创建or查询
- Criteria orOperator (Collection< Criteria> criteria)使用$或运算符为所有提供的条件创建或查询
- Criteria regex (String re)使用$regex创建条件
- Criteria sampleRate (double sampleRate)使用$sampleRate运算符创建条件
- Criteria size (int s)使用$size运算符创建条件
- Criteria type (int t)使用$type运算符创建条件
- Criteria matchingDocumentStructure (MongoJsonSchema schema)使用 $ jsonSchema 运算符为JSON schema criteria创建一个条件。$jsonSchema只能应用于查询的顶层,而不能应用于特定属性。使用schema的properties属性来匹配嵌套字段。
- Criteria bits() 是调用MongoDB bitwise query operators(如$bitsAllClear)的网关。
Criteria类还提供了以下用于地理空间查询的方法。
- Criteria within (Circle circle)使用$geoWithin $center操作符创建地理空间条件。
- Criteria within (Box box)使用$geoWithin $box操作创建地理空间条件。
- Criteria withinSphere (Circle circle)使用$geoWithin $center操作符创建地理空间条件。
- Criteria near (Point point)使用$near操作创建地理空间条件
- Criteria nearSphere (Point point)使用$nearSphere $center操作创建地理空间条件。
- Criteria minDistance (double minDistance)使用 $ minDistance 操作创建地理空间条件,与$near一同使用。
- Criteria maxDistance (double maxDistance)使用 $ maxDistance 操作创建地理空间条件,与$near一同使用。
Query类有一些附加的方法,这些方法允许选择某些字段以及对结果进行limit和排序。
Query类的方法
- Query addCriteria (Criteria criteria),用于向查询添加其他条件
- Field fields (),用于定义要包含在查询结果中的字段
- Query limit (int limit)用于将返回结果的大小限制为提供的limit (用于分页)
- Query skip(int skip)用于跳过结果中提供的documents数(用于分页)
- Query with (Sort sort)用于为结果提供排序定义
- Query with (ScrollPosition position)用于提供滚动(Scroll)位置(基于Offset或Keyset的分页)以开始或恢复滚动
template API允许直接使用结果投影,使你能够将查询映射到给定的域类型,同时将操作结果投影到另一个域类型,如下所述。
class
template.query(SWCharacter.class)
.as(Jedi.class)
有关结果投影的更多信息,请参阅Spring Data Projections的文档。
二 选择字段
MongoDB支持投影查询返回的字段。投影可以根据字段的名称包括和排除字段(除非明确排除,否则始终包括_id字段)。
例2:选择结果字段
public class Person {
@Id String id;
String firstname;
@Field("last_name")
String lastname;
Address address;
}
query.fields().include("lastname"); --------1
query.fields().exclude("id").include("lastname") --------2
query.fields().include("address") --------3
query.fields().include("address.city") --------4
1. 通过{“last_name”:1},结果将同时包含_id和last_name。
2. 通过{“_id”:0,“last_name”:1},结果将仅包含last_name 。
3. 通过{“address”:1}结果将包含_id和整个地址对象。
4. 通过{“address.city”:1},结果将包含_id和address对象,该address仅包含city字段。
从MongoDB 4.4开始,你可以使用聚合表达式进行字段投影,如下所示:
例3:使用表达式计算结果字段
query.fields()
.project(MongoExpression.create("'$toUpper' : '$last_name'")) --------1
.as("last_name"); --------2
query.fields()
.project(StringOperators.valueOf("lastname").toUpper()) --------3
.as("last_name");
query.fields()
.project(AggregationSpELExpression.expressionOf("toUpper(lastname)")) --------4
.as("last_name");
1. 使用native表达式。使用的字段名称必须引用数据库document中的字段名称。
2. 指定表达式结果投影到的字段名称。生成的字段名称未映射到域模型。
3. 使用聚合表达式。除了native MongoExpression之外,字段名被映射到域模型中使用的字段名。
4. 将SpEL与AggregationExpression一起使用可以调用表达式函数。字段名称将映射到域模型中使用的字段名称。
@Query(fields=“…”)允许在存储库级别使用表达式字段投影,如MongoDB 基于JSON的查询方法和字段限制中所述。
三、 其他查询选项
MongoDB提供了各种将元信息(如注释[comment]或批处理大小)应用于查询的方法。直接使用Query API,这些选项有几种方法。
3.1 Hints
可以通过两种方式应用索引提示(hint),使用索引名称或其字段定义。
template.query(Person.class)
.matching(query("...").withHint("index-to-use"));
template.query(Person.class)
.matching(query("...").withHint("{ firstname : 1 }"));
3.2 游标批大小Cursor Batch Size
游标批大小定义了每个响应批中要返回的文档数量。
Query query = query(where("firstname").is("luke"))
.cursorBatchSize(100)
3.3 Collations
在集合操作中使用collations是在查询或操作选项中指定Collation实例的问题,如下两个示例所示:
Collation collation = Collation.of("de");
Query query = new Query(Criteria.where("firstName").is("Amél"))
.collation(collation);
List<Person> results = template.find(query, Person.class);
3.4 读取首选项Read Preference
可以在要运行的Query对象上直接设置要使用的ReadPreference,如下所示。
template.find(Person.class)
.matching(query(where(...)).withReadPreference(ReadPreference.secondary()))
.all();
Query实例上的首选项设置将取代MongoTemplate的默认ReadPreference。
3.5 Comments
查询可以配备注释,这使得它们更容易在服务器日志中查找。
template.find(Person.class)
.matching(query(where(...)).comment("Use the force luke!"))
.all();
四、查询Distinct值
MongoDB提供了一个操作,通过对结果documents的查询来获得单个字段的distinct值。结果值不需要具有相同的数据类型,该功能也不限于简单类型。对于检索,为了转换和typing,实际的结果类型确实很重要。以下示例展示了如何查询distinct值:
例4:检索distinct值
template.query(Person.class) --------1
.distinct("lastname") --------2
.all(); --------3
1. 查询Person集合。
2. 选择lastname字段的distinct值。字段名称是根据域类型属性声明映射的,并考虑到潜在的@Field注解。
3. 将所有distinct值检索为对象列表(由于未指定明确的结果类型)。
将distinct值检索到对象集合中是最灵活的方法,因为它试图确定域类型的属性值,并将结果转换为所需的类型或映射Document结构。
有时,当所需字段的所有值都固定为某个类型时,直接获得正确类型的集合会更方便,如下例所示:
例5:检索强类型的distinct值
template.query(Person.class) --------1
.distinct("lastname") --------2
.as(String.class) --------3
.all(); --------4
1. 查询Person的集合。
2. 选择lastname字段的distinct值。字段名是根据域类型属性声明映射的,同时考虑了潜在的@Field注释。
3. 将检索到的值转换为所需的目标类型——在本例中为String。如果存储的字段包含document,也可以将值映射到更复杂的类型。
4. 以String列表的形式检索所有distinct值。如果无法将类型转换为所需的目标类型,则此方法将抛出DataAccessException。
五、GeoSpatial Queries
MongoDB通过使用$ near、$ within、geoWithin和$nearSphere等运算符来支持GeoSpatial查询。“Criteria”类中提供了特定于地理空间查询的方法。还有一些形状类(Box, Circle 和Point)与地理空间相关的“Criteria”方法一起使用。
在MongoDB事务中使用GeoSpatial查询时需要注意,请参阅事务内部的特殊行为。
要了解如何执行GeoSpatial查询,请考虑以下Venue类(取自集成测试并依赖于MappingMongoConverter):
Venue.java
@Document(collection="newyork")
public class Venue {
@Id
private String id;
private String name;
private double[] location;
@PersistenceConstructor
Venue(String name, double[] location) {
super();
this.name = name;
this.location = location;
}
public Venue(String name, double x, double y) {
super();
this.name = name;
this.location = new double[] { x, y };
}
public String getName() {
return name;
}
public double[] getLocation() {
return location;
}
@Override
public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location="
+ Arrays.toString(location) + "]";
}
}
要查找Circle内的位置,你可以使用以下查询:
Circle circle = new Circle(-73.99171, 40.738868, 0.01);
List<Venue> venues =
template.find(new Query(Criteria.where("location").within(circle)), Venue.class);
要使用球坐标查找圆内的venues,可以使用以下查询:
Circle circle = new Circle(-73.99171, 40.738868, 0.003712240453784);
List<Venue> venues =
template.find(new Query(Criteria.where("location").withinSphere(circle)), Venue.class);
要查找Box内的venues,你可以使用以下查询:
//lower-left then upper-right
Box box = new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404));
List<Venue> venues =
template.find(new Query(Criteria.where("location").within(box)), Venue.class);
你可使用下列查询查询,以查询邻近某个点的venues:
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(Criteria.where("location").near(point).maxDistance(0.01)), Venue.class);
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(Criteria.where("location").near(point).minDistance(0.01).maxDistance(100)), Venue.class);
要使用球坐标查找点附近的venues,可以使用以下查询:
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(
Criteria.where("location").nearSphere(point).maxDistance(0.003712240453784)),
Venue.class);
六、Geo-near 查询
MongoDB 4.2删除了对geoNear命令的支持,该命令以前曾用于运行NearQuery。
Spring Data MongoDB 2.2 MongoOperations#geoNear使用$geoNear aggregation而不是geoNear命令来运行NearQuery。
之前在包装器类型中返回的计算距离(使用geoNear命令时的dis)现在嵌入到生成的document中。如果给定的域类型已经包含具有该名称的属性,则将计算的距离命名为具有潜在随机后缀的calculated-distance。
目标类型可能包含一个以返回距离命名的属性,以便将其直接读回域类型,如下所示。
GeoResults<VenueWithDistanceField> = template.query(Venue.class) --------1
.as(VenueWithDistanceField.class) --------2
.near(NearQuery.near(new GeoJsonPoint(-73.99, 40.73), KILOMETERS))
.all();
1. 用于标识目标集合和潜在查询映射的域类型。
2. 包含类型为Number的dis字段的目标类型。
MongoDB支持在数据库中查询地理位置,同时计算与给定原点的距离。使用geo-near查询,你可以表达诸如“查找周围10英里内的所有餐厅”之类的查询。为了做到这一点,MongoOperations提供了geoNear(…)方法,该方法以NearQuery作为参数(以及已经熟悉的实体类型和集合),如以下示例所示:
Point location = new Point(-73.99171, 40.738868);
NearQuery query = NearQuery.near(location).maxDistance(new Distance(10, Metrics.MILES));
GeoResults<Restaurant> = operations.geoNear(query, Restaurant.class);
我们使用NearQuery builder API来设置查询,以返回10英里内给定Point周围的所有Restaurant实例。这里使用的Metrics枚举实际上实现了一个接口,以便其他度量也可以插入到距离中。度量由乘法器(multiplier)支持,用于将给定度量的距离值转换为native距离。这里显示的样本将认为10是英里。使用其中一个内置指标(英里和公里)会自动触发在查询中设置球形(spherical)标志。如果要避免这种情况,请将double传递到maxDistance(…)中。有关更多信息,请参阅NearQuery和Distance的JavaDoc。
geo-near操作返回一个封装GeoResult实例的GeoResults包装器对象。包装GeoResults 允许访问所有结果的平均距离。单个GeoResult对象携带找到的实体及其与原点的距离。
七、GeoJSON 支持
MongoDB支持地理空间数据的GeoJSON和简单的(遗留的)坐标对。这些格式既可以用于存储数据,也可以用于查询数据。请参阅MongoDB手册关于GeoJSON支持的部分,了解相关要求和限制。
八、域类中的GeoJSON类型
在域类中使用GeoJSON类型很简单。 org.springframework.data.mongodb.core.geo包含GeoJsonPoint、GeoJsonPolygon等类型。这些类型是对现有的org.springframework.data.geo类型的扩展。下面的例子使用了GeoJsonPoint:
public class Store {
String id;
/**
* { "type" : "Point", "coordinates" : [ x, y ] }
*/
GeoJsonPoint location;
}
如果GeoJSON对象的坐标表示纬度和经度对,那么经度在前纬度在后。
因此,GeoJsonPoint将getX()作为经度,将getY()作为纬度。
九、存储库查询方法中的GeoJSON 类型
使用GeoJSON类型作为存储库查询参数,在创建查询时强制使用$geometry操作符,如下面的示例所示:
public interface StoreRepository extends CrudRepository<Store, String> {
List<Store> findByLocationWithin(Polygon polygon); --------1
}
/*
* {
* "location": {
* "$geoWithin": {
* "$geometry": {
* "type": "Polygon",
* "coordinates": [
* [
* [-73.992514,40.758934],
* [-73.961138,40.760348],
* [-73.991658,40.730006],
* [-73.992514,40.758934]
* ]
* ]
* }
* }
* }
* }
*/
repo.findByLocationWithin( --------2
new GeoJsonPolygon(
new Point(-73.992514, 40.758934),
new Point(-73.961138, 40.760348),
new Point(-73.991658, 40.730006),
new Point(-73.992514, 40.758934))); --------3
/*
* {
* "location" : {
* "$geoWithin" : {
* "$polygon" : [ [-73.992514,40.758934] , [-73.961138,40.760348] , [-73.991658,40.730006] ]
* }
* }
* }
*/
repo.findByLocationWithin( --------4
new Polygon(
new Point(-73.992514, 40.758934),
new Point(-73.961138, 40.760348),
new Point(-73.991658, 40.730006)));
1. 使用commons类型的存储库方法定义允许使用GeoJSON和遗留格式调用它。
2. 使用GeoJSON类型来使用$geometry运算符。
3. 请注意,GeoJSON多边形需要定义一个闭合环。
4. 使用遗留格式$polygon运算符。
十、度量和距离计算
MongoDB $geoNear运算符允许使用GeoJSON点或遗留坐标对。
NearQuery.near(new Point(-73.99171, 40.738868))
{
"$geoNear": {
//...
"near": [-73.99171, 40.738868]
}
}
NearQuery.near(new GeoJsonPoint(-73.99171, 40.738868))
{
"$geoNear": {
//...
"near": { "type": "Point", "coordinates": [-73.99171, 40.738868] }
}
}
尽管语法不同,但无论集合中的目标Document使用何种格式,这两种格式服务器都可以接受。
在距离计算上存在巨大差异。使用遗留格式操作类地球球体上的Radians,而GeoJSON格式使用Meters。
为了避免麻烦,请确保将Metric设置为所需的测量单位,以确保正确计算距离。
换句话说:
假设你有5个Documents,如下所示:
{
"_id" : ObjectId("5c10f3735d38908db52796a5"),
"name" : "Penn Station",
"location" : { "type" : "Point", "coordinates" : [ -73.99408, 40.75057 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796ab"),
"name" : "Momofuku Milk Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.985839, 40.731698 ] }
}
使用GeoJSON从[-73.99171, 40.738868]获取400米半径内的所有Documents:
例6:使用GeoJSON的GeoNear
{
"$geoNear": {
"maxDistance": 400,
"num": 10,
"near": { type: "Point", coordinates: [-73.99171, 40.738868] },
"spherical":true,
"key": "location",
"distanceField": "distance"
}
}
Returning the following 3 Documents:
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
"distance" : 0.0 --------3
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 69.3582262492474 --------3
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 69.3582262492474 --------3
}
1. 以米为单位到中心点的最大距离。
2. GeoJSON总是在一个球体上操作。
3. 以米为单位到中心点的距离。
现在,当使用遗留坐标对时,如前所述,是对Radians进行操作。因此,我们在构造$geoNear命令时使用 Metrics#KILOMETERS。Metric可确保距离乘数设置正确。
例7:具有遗留坐标对的GeoNear
{
"$geoNear": {
"maxDistance": 0.0000627142377, --------1
"distanceMultiplier": 6378.137, --------2
"num": 10,
"near": [-73.99171, 40.738868],
"spherical":true, --------3
"key": "location",
"distanceField": "distance"
}
}
Returning the 3 Documents just like the GeoJSON variant:
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
"distance" : 0.0 --------4
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 0.0693586286032982 --------4
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 0.0693586286032982 --------4
}
1. 与中心点的最大距离(弧度)。
2. 距离乘数,所以我们得到千米作为结果距离。
3. 确保我们对2d_sphere索引进行操作。
4. 与中心点的距离(千米)-乘以1000以匹配GeoJSON中的的米。
十一、全文检索
自MongoDB的2.6版本以来,你可以使用$text运算符运行全文查询。TextQuery和TextCriteria中提供了特定于全文查询的方法和操作。在进行全文搜索时,请参阅MongoDB reference以了解其行为和限制。
在实际使用全文搜索之前,必须正确设置搜索索引。有关如何创建索引结构的更多详细信息,请参见文本索引。以下示例展示了如何设置全文搜索:
db.foo.createIndex(
{
title : "text",
content : "text"
},
{
weights : {
title : 3
}
}
)
搜索coffee cake的查询可以定义并运行如下:
例8:全文查询
Query query = TextQuery
.queryText(new TextCriteria().matchingAny("coffee", "cake"));
List<Document> page = template.find(query, Document.class);
使用TextQuery.sortByScore根据权重按相关性排序结果。
例9:全文查询-按分数排序
Query query = TextQuery
.queryText(new TextCriteria().matchingAny("coffee", "cake"))
.sortByScore()
.includeScore();
List<Document> page = template.find(query, Document.class);
1. 使用score属性根据相关性对结果进行排序,这会触发.sort({'score': {'$meta': 'textScore'}})。
2. 使用TextQuery.includeScore()将计算出的相关性包含在结果Document中。
你可以通过在搜索词前加上“-”或使用notMatching来排除搜索词,如下例所示(注意,这两行具有相同的效果,因此是冗余的):
// search for 'coffee' and not 'cake'
TextQuery.queryText(new TextCriteria().matching("coffee").matching("-cake"));
TextQuery.queryText(new TextCriteria().matching("coffee").notMatching("cake"));
TextCriteria.matching按原样接受所提供的搜索词。因此,你可以通过将phrases放在双引号之间(例如"coffee cake")或使用TextCriteria.phrase来定义。以下示例展示了定义phrase的两种方法:
// search for phrase 'coffee cake'
TextQuery.queryText(new TextCriteria().matching("\"coffee cake\""));
TextQuery.queryText(new TextCriteria().phrase("coffee cake"));
你可以通过在TextCriteria上使用相应的方法来设置$ caseSensitive和$diacriticSensitive 的flags。请注意,这两个可选标志是在MongoDB 3.2中引入的,除非显式设置,否则不会包含在查询中。
十二、Query by Example
“Query by Example”可用于Template API级运行example查询。
以下片段展示了如何通过example进行查询:
类型化Example查询
Person probe = new Person();
probe.lastname = "stark";
Example example = Example.of(probe);
Query query = new Query(new Criteria().alike(example));
List<Person> result = template.find(query, Person.class);
默认情况下,Example是有严格类型的。这意味着映射的查询具有包含的类型匹配,将其限制为探测可分配的类型。例如,当坚持使用默认类型键(_class)时,查询具有诸如(_class : { $in : [ com.acme.Person] })之类的限制。
通过使用UntypedExampleMatcher,可以绕过默认行为并跳过类型限制。因此,只要字段名称匹配,几乎任何域类型都可以用作创建引用的探针(probe),如下例所示:
例10:非类型化的示例查询
class JustAnArbitraryClassWithMatchingFieldName {
@Field("lastname") String value;
}
JustAnArbitraryClassWithMatchingFieldNames probe = new JustAnArbitraryClassWithMatchingFieldNames();
probe.value = "stark";
Example example = Example.of(probe, UntypedExampleMatcher.matching());
Query query = new Query(new Criteria().alike(example));
List<Person> result = template.find(query, Person.class);
当在ExampleSpec中包含null值时,Spring Data Mongo使用内嵌式document匹配,而不是点表示法(dot notation)属性匹配。这样做会强制内嵌document中的所有属性值和属性顺序进行精确的document 匹配。
如果你将不同的实体存储在单个集合中或选择不编写类型提示(hints),UntypedExampleMatcher可能是你的正确选择。
此外,请记住,使用@TypeAlias需要对MappingContext进行立即初始化。为此,请配置initialEntitySet,以确保读取操作的别名解析正确。
Spring Data MongoDB提供了对不同匹配选项的支持:
StringMatcher 选项
Matching | Logical result |
---|---|
DEFAULT (case-sensitive) | {“firstname” : firstname} |
DEFAULT (case-insensitive) | {“firstname” : { $regex: firstname, $options: ‘i’}} |
EXACT (case-sensitive) | {“firstname” : { $ regex: /^firstname$/}} |
EXACT (case-insensitive) | {“firstname” : { $ regex: /^firstname$/, $options: ‘i’}} |
STARTING (case-sensitive) | {“firstname” : { $regex: /^firstname/}} |
STARTING (case-insensitive) | {“firstname” : { $regex: /^firstname/, $options: ‘i’}} |
ENDING (case-sensitive) | {“firstname” : { $ regex: /firstname$/}} |
ENDING (case-insensitive) | {“firstname” : { $ regex: /firstname$/, $options: ‘i’}} |
CONTAINING (case-sensitive) | {“firstname” : { $regex: /.firstname./}} |
CONTAINING (case-insensitive) | {“firstname” : { $regex: /.firstname./, $options: ‘i’}} |
REGEX (case-sensitive) | {“firstname” : { $regex: /firstname/}} |
REGEX (case-insensitive) | {“firstname” : { $regex: /firstname/, $options: ‘i’}} |
十三、查询一个集合以匹配JSON Schema
你可以使用schema来查询任何集合中与JSON schema定义的给定结构匹配的文档,如下例所示:
例11:查询与$jsonSchema匹配的文档
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
template.find(query(matchingDocumentStructure(schema)), Person.class);
请参考JSON Schema一节了解更多关于Spring Data MongoDB的schema支持。