对于最新的稳定版本,请使用 Spring Data MongoDB 4.5.2spring-doc.cadn.net.cn

查询文档

您可以使用QueryCriteria类来表达您的查询。它们具有镜像本机 MongoDB 运算符名称的方法名称,例如lt,lte,is,等。 这QueryCriteria类遵循流畅的 API 风格,因此您可以将多个方法条件和查询链接在一起,同时使用易于理解的代码。为了提高可读性,静态导入允许您避免使用“new”关键字进行创建QueryCriteria实例。 您还可以使用BasicQuery创建Query实例,如以下示例所示:spring-doc.cadn.net.cn

示例 1.从纯 JSON 字符串创建 Query 实例
BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}");
List<Person> result = mongoTemplate.find(query, Person.class);

查询集合中的文档

之前,我们看到了如何使用findOnefindById方法MongoTemplate. 这些方法以正确的方式返回单个域对象,或者使用响应式 API 返回单个域对象Mono发出单个元素。我们还可以查询要作为域对象列表返回的文档集合。假设我们有许多Person对象,名称和年龄存储为集合中的文档,并且每个人都有一个带有余额的嵌入式帐户文档,我们现在可以使用以下代码运行查询:spring-doc.cadn.net.cn

使用 MongoTemplate 查询文档
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

// ...

List<Person> result = template.query(Person.class)
  .matching(query(where("age").lt(50).and("accounts.balance").gt(1000.00d)))
  .all();
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

// ...

Flux<Person> result = template.query(Person.class)
  .matching(query(where("age").lt(50).and("accounts.balance").gt(1000.00d)))
  .all();

所有查找方法都采用Query对象作为参数。此对象定义用于执行查询的条件和选项。这些条件是通过使用Criteria具有名为where实例化新的Criteria对象。 我们建议使用 静态导入org.springframework.data.mongodb.core.query.Criteria.whereQuery.query使查询更具可读性。spring-doc.cadn.net.cn

查询应返回ListFluxPerson满足指定条件的对象。本节的其余部分列出了CriteriaQuery与 MongoDB 中提供的运算符相对应的类。大多数方法返回Criteria对象,为 API 提供流畅的样式。spring-doc.cadn.net.cn

Criteria类的方法

Criteriaclass 提供了以下方法,所有这些方法都对应于 MongoDB 中的运算符:spring-doc.cadn.net.cn

Criteria 类还为地理空间查询提供了以下方法。spring-doc.cadn.net.cn

Queryclass 有一些额外的方法,允许选择某些字段以及限制和排序结果。spring-doc.cadn.net.cn

Query 类的方法

模板 API 允许直接使用结果投影,使您能够将查询映射到给定域类型,同时将作结果投影到另一个域类型上,如下所述。spring-doc.cadn.net.cn

class

template.query(SWCharacter.class)
    .as(Jedi.class)

有关结果预测的更多信息,请参阅文档的 预测 部分。spring-doc.cadn.net.cn

选择字段

MongoDB支持投影查询返回的字段。投影可以包含和排除字段(_id字段始终包含在内,除非明确排除),否则。spring-doc.cadn.net.cn

示例 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 结果将包含_idlast_name通过{ "last_name" : 1 }.
2 结果将仅包含last_name通过{ "_id" : 0, "last_name" : 1 }.
3 结果将包含_id和整个address对象通过{ "address" : 1 }.
4 结果将包含_id以及address对象,该对象仅包含city字段通过{ "address.city" : 1 }.

从 MongoDB 4.4 开始,您可以使用聚合表达式进行字段投影,如下所示:spring-doc.cadn.net.cn

示例 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 使用本机表达式。使用的字段名称必须引用数据库文档中的字段名称。
2 分配表达式结果投影到的字段名称。生成的字段名称不会映射到域模型。
3 使用AggregationExpression. 本地以外的MongoExpression,字段名称将映射到域模型中使用的字段名称。
4 将 SpEL 与AggregationExpression以调用表达式函数。字段名称映射到域模型中使用的字段名称。

@Query(fields="…")允许在Repository级别,如 MongoDB 基于 JSON 的查询方法和字段限制中所述。spring-doc.cadn.net.cn

其他查询选项

MongoDB 提供了多种将元信息(例如注释或批量大小)应用于查询的方法。使用Query应用程序接口 直接有几种方法可用于这些选项。spring-doc.cadn.net.cn

提示

索引提示可以通过两种方式应用,即使用索引名称或其字段定义。spring-doc.cadn.net.cn

template.query(Person.class)
    .matching(query("...").withHint("index-to-use"));

template.query(Person.class)
    .matching(query("...").withHint("{ firstname : 1 }"));

游标批量大小

游标批大小定义了每个响应批中要返回的文档数。spring-doc.cadn.net.cn

Query query = query(where("firstname").is("luke"))
    .cursorBatchSize(100)

排序规则

将排序规则与集合作一起使用是指定Collation实例,如以下两个示例所示:spring-doc.cadn.net.cn

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);

读取首选项

ReadPreference可以使用可以直接在Query对象,如下所述。spring-doc.cadn.net.cn

template.find(Person.class)
    .matching(query(where(...)).withReadPreference(ReadPreference.secondary()))
    .all();
Query实例将取代默认值ReadPreferenceMongoTemplate.

评论

查询可以配备注释,这使得它们更容易在服务器日志中查找。spring-doc.cadn.net.cn

template.find(Person.class)
    .matching(query(where(...)).comment("Use the force luke!"))
    .all();

查询非重复值

MongoDB提供了一个作,通过使用结果文档中的查询来获取单个字段的不同值。结果值不需要具有相同的数据类型,该功能也不局限于简单类型。对于检索,实际的结果类型对于转换和键入确实很重要。以下示例显示了如何查询不同值:spring-doc.cadn.net.cn

示例 4.检索非重复值
template.query(Person.class)  (1)
  .distinct("lastname")       (2)
  .all();                     (3)
1 查询Person收集。
2 选择lastname田。 字段名称根据域类型属性声明进行映射,获取潜力@Field注释。
3 将所有非重复值检索为ListObject(由于未指定显式结果类型)。

将不同值检索到CollectionObject是最灵活的方式,因为它会尝试确定域类型的属性值并将结果转换为所需的类型或映射Document结构。spring-doc.cadn.net.cn

有时,当所需字段的所有值都固定为某种类型时,直接获取正确键入的Collection,如以下示例所示:spring-doc.cadn.net.cn

示例 5.检索强类型非重复值
template.query(Person.class)  (1)
  .distinct("lastname")       (2)
  .as(String.class)           (3)
  .all();                     (4)
1 查询Person.
2 选择lastname田。 fieldname 根据域类型属性声明进行映射,获取潜力@Field注释。
3 检索到的值将转换为所需的目标类型 — 在本例中,String. 如果存储的字段包含文档,也可以将值映射到更复杂的类型。
4 将所有非重复值检索为ListString. 如果类型无法转换为所需的目标类型,则此方法会抛出DataAccessException.

+= 地理空间查询spring-doc.cadn.net.cn

MongoDB 通过使用运算符(例如$near,$within,geoWithin$nearSphere. 特定于地理空间查询的方法可在Criteria类。 还有一些形状类(Box,CirclePoint)与地理空间相关Criteria方法。spring-doc.cadn.net.cn

在MongoDB事务中使用GeoSpatial查询时需要注意,请参阅事务中的特殊行为

要了解如何执行地理空间查询,请考虑以下事项Venue类(取自集成测试并依赖于MappingMongoConverter):spring-doc.cadn.net.cn

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,可以使用以下查询:spring-doc.cadn.net.cn

Circle circle = new Circle(-73.99171, 40.738868, 0.01);
List<Venue> venues =
    template.find(new Query(Criteria.where("location").within(circle)), Venue.class);

要在Circle使用球面坐标,可以使用以下查询:spring-doc.cadn.net.cn

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,可以使用以下查询:spring-doc.cadn.net.cn

//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);

要查找附近的场地Point,您可以使用以下查询:spring-doc.cadn.net.cn

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);

要查找附近的场地Point使用球面坐标,可以使用以下查询:spring-doc.cadn.net.cn

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);

地理近距离查询

2.2 中更改!
MongoDB 4.2 删除了对
geoNear之前用于运行NearQuery.spring-doc.cadn.net.cn

Spring 数据 MongoDB 2.2MongoOperations#geoNear使用$geoNear 聚合而不是geoNear命令运行NearQuery.spring-doc.cadn.net.cn

计算的距离(dis使用 geoNear 命令时),以前在包装器类型中返回,现在被嵌入 到生成的文档中。 如果给定的域类型已包含具有该名称的属性,则计算出的距离 被命名为calculated-distance带有一个潜在的随机后缀。spring-doc.cadn.net.cn

目标类型可能包含以返回的距离命名的属性,以便(另外)将其直接读回域类型,如下所示。spring-doc.cadn.net.cn

GeoResults<VenueWithDistanceField> = template.query(Venue.class) (1)
    .as(VenueWithDistanceField.class)                            (2)
    .near(NearQuery.near(new GeoJsonPoint(-73.99, 40.73), KILOMETERS))
    .all();
1 用于标识目标集合和潜在查询映射的域类型。
2 目标类型包含dis类型字段Number.

MongoDB 支持查询数据库的地理位置,同时计算与给定原点的距离。通过近地查询,您可以表达诸如“查找周围 10 英里内的所有餐厅”之类的查询。为了让您这样做,MongoOperations提供geoNear(…)采用NearQuery作为参数(以及已经熟悉的实体类型和集合),如以下示例所示:spring-doc.cadn.net.cn

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);

我们使用NearQuerybuilder API 来设置查询以返回所有Restaurant给定Point到 10 英里。 这Metrics这里使用的 enum 实际上实现了一个接口,以便其他指标也可以插入距离。 一个Metric由乘数支持,将给定指标的距离值转换为原生距离。 此处显示的示例将 10 视为英里。使用其中一个内置指标(英里和公里)会自动触发要在查询上设置的球形标志。 如果你想避免这种情况,请通过普通double值转换为maxDistance(…). 有关更多信息,请参阅NearQueryDistance.spring-doc.cadn.net.cn

地理近距离作返回GeoResults封装GeoResult实例。 包皮GeoResults允许访问所有结果的平均距离。 单GeoResult对象携带找到的实体加上它与原点的距离。spring-doc.cadn.net.cn

GeoJSON 支持

MongoDB 支持地理空间数据的 GeoJSON 和简单(旧版)坐标对。这些格式既可用于存储数据,也可用于查询数据。请参阅有关 GeoJSON 支持的 MongoDB 手册,了解要求和限制。spring-doc.cadn.net.cn

域类中的 GeoJSON 类型

在域类中使用 GeoJSON 类型非常简单。这org.springframework.data.mongodb.core.geo包包含诸如GeoJsonPoint,GeoJsonPolygon,等。这些类型扩展了现有的org.springframework.data.geo类型。以下示例使用GeoJsonPoint:spring-doc.cadn.net.cn

public class Store {

	String id;

	/**
	 * { "type" : "Point", "coordinates" : [ x, y ] }
	 */
	GeoJsonPoint location;
}

如果coordinates的 GeoJSON 对象表示纬度经度对,经度在前,然后是纬度
GeoJsonPoint因此对待getX()作为经度getY()作为纬度spring-doc.cadn.net.cn

存储库查询方法中的 GeoJSON 类型

使用 GeoJSON 类型作为存储库查询参数强制使用$geometry运算符,如以下示例所示:spring-doc.cadn.net.cn

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 点或旧坐标对。spring-doc.cadn.net.cn

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] }
  }
}

尽管语法不同,但无论集合中的目标文档格式如何,服务器都可以接受这两种格式 正在使用。spring-doc.cadn.net.cn

距离计算存在巨大差异。使用旧格式作 在类似地球的球体上以弧度为单位,而 GeoJSON 格式使用 Meters

为避免严重头痛,请确保将Metric设置为所需的计量单位,以确保 距离要正确计算。spring-doc.cadn.net.cn

换句话说:spring-doc.cadn.net.cn

假设您有 5 个文档,如下所示:spring-doc.cadn.net.cn

{
    "_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 ] }
}

从 400 米半径内获取所有文档[-73.99171, 40.738868]使用 地理JSON:spring-doc.cadn.net.cn

示例 6.GeoNear 与 GeoJSON
{
    "$geoNear": {
        "maxDistance": 400, (1)
        "num": 10,
        "near": { type: "Point", coordinates: [-73.99171, 40.738868] },
        "spherical":true, (2)
        "key": "location",
        "distanceField": "distance"
    }
}

退回以下 3 份文件:spring-doc.cadn.net.cn

{
    "_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 距中心点的距离(以米为单位)。

现在,当使用传统坐标对时,如前所述,对弧度进行作。所以我们使用Metrics#KILOMETERS when constructing the `$geoNear命令。这Metric确保距离乘数设置正确。spring-doc.cadn.net.cn

示例 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"
    }
}

返回 3 个文档,就像 GeoJSON 变体一样:spring-doc.cadn.net.cn

{
    "_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算子。特定于全文查询的方法和作可在TextQueryTextCriteria.进行全文搜索时,请参阅 MongoDB 参考以了解其行为和限制。spring-doc.cadn.net.cn

在实际使用全文搜索之前,必须正确设置搜索索引。 有关如何创建索引结构的更多详细信息,请参阅文本索引。 以下示例演示如何设置全文搜索:spring-doc.cadn.net.cn

db.foo.createIndex(
{
  title : "text",
  content : "text"
},
{
  weights : {
              title : 3
            }
}
)

搜索coffee cake可以按如下方式定义和运行:spring-doc.cadn.net.cn

示例 8.全文查询
Query query = TextQuery
  .queryText(new TextCriteria().matchingAny("coffee", "cake"));

List<Document> page = template.find(query, Document.class);

要根据weightsTextQuery.sortByScore.spring-doc.cadn.net.cn

示例 9.全文查询 - 按分数排序
Query query = TextQuery
  .queryText(new TextCriteria().matchingAny("coffee", "cake"))
  .sortByScore() (1)
  .includeScore(); (2)

List<Document> page = template.find(query, Document.class);
1 使用 score 属性按触发器的相关性对结果进行排序.sort({'score': {'$meta': 'textScore'}}).
2 TextQuery.includeScore()将计算出的相关性包含在生成的Document.

您可以通过在搜索词前加上 或-notMatching,如以下示例所示(请注意,这两行具有相同的效果,因此是多余的):spring-doc.cadn.net.cn

// search for 'coffee' and not 'cake'
TextQuery.queryText(new TextCriteria().matching("coffee").matching("-cake"));
TextQuery.queryText(new TextCriteria().matching("coffee").notMatching("cake"));

TextCriteria.matching按原样采用提供的术语。 因此,您可以通过将短语放在双引号之间来定义短语(例如,\"coffee cake\")或使用 byTextCriteria.phrase.以下示例显示了定义短语的两种方法:spring-doc.cadn.net.cn

// search for phrase 'coffee cake'
TextQuery.queryText(new TextCriteria().matching("\"coffee cake\""));
TextQuery.queryText(new TextCriteria().phrase("coffee cake"));

您可以为$caseSensitive$diacriticSensitive通过使用TextCriteria. 请注意,这两个可选标志已在MongoDB 3.2中引入,除非显式设置,否则不会包含在查询中。spring-doc.cadn.net.cn

按示例查询

“按示例查询”可用于模板 API 级别运行示例查询。spring-doc.cadn.net.cn

以下截图显示了如何通过示例进行查询:spring-doc.cadn.net.cn

类型化示例查询
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] }).spring-doc.cadn.net.cn

通过使用UntypedExampleMatcher,则可以绕过默认行为并跳过类型限制。因此,只要字段名称匹配,几乎任何域类型都可以用作创建引用的探测,如以下示例所示:spring-doc.cadn.net.cn

示例 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);

当包括nullExampleSpec,Spring Data Mongo 使用嵌入式文档匹配而不是点表示法属性匹配。 这样做会强制对嵌入文档中的所有属性值和属性顺序进行精确的文档匹配。spring-doc.cadn.net.cn

UntypedExampleMatcher如果你在单个集合中存储不同的实体或选择不编写类型提示,这可能是你的正确选择。spring-doc.cadn.net.cn

另外,请记住,使用@TypeAlias需要预先初始化MappingContext.为此,请配置initialEntitySet以确保读取作的正确别名解析。spring-doc.cadn.net.cn

Spring Data MongoDB 提供了对不同匹配选项的支持:spring-doc.cadn.net.cn

StringMatcher选项
匹配 逻辑结果

DEFAULT(区分大小写)spring-doc.cadn.net.cn

{"firstname" : firstname}spring-doc.cadn.net.cn

DEFAULT(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: firstname, $options: 'i'}}spring-doc.cadn.net.cn

EXACT(区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /^firstname$/}}spring-doc.cadn.net.cn

EXACT(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /^firstname$/, $options: 'i'}}spring-doc.cadn.net.cn

STARTING(区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /^firstname/}}spring-doc.cadn.net.cn

STARTING(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /^firstname/, $options: 'i'}}spring-doc.cadn.net.cn

ENDING(区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /firstname$/}}spring-doc.cadn.net.cn

ENDING(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /firstname$/, $options: 'i'}}spring-doc.cadn.net.cn

CONTAINING(区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /.*firstname.*/}}spring-doc.cadn.net.cn

CONTAINING(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /.*firstname.*/, $options: 'i'}}spring-doc.cadn.net.cn

REGEX(区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /firstname/}}spring-doc.cadn.net.cn

REGEX(不区分大小写)spring-doc.cadn.net.cn

{"firstname" : { $regex: /firstname/, $options: 'i'}}spring-doc.cadn.net.cn

查询集合以匹配 JSON 架构

可以使用架构查询任何集合,以查找与 JSON 架构定义的给定结构匹配的文档,如以下示例所示:spring-doc.cadn.net.cn

示例 11.查询与$jsonSchema
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();

template.find(query(matchingDocumentStructure(schema)), Person.class);

请参阅 JSON Schema 部分以了解有关 Spring Data MongoDB 中的 Schema 支持的更多信息。spring-doc.cadn.net.cn