/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/MatchOperation.java

https://github.com/spring-projects/spring-data-mongodb · Java · 81 lines · 27 code · 13 blank · 41 comment · 1 complexity · 91629681dfb3f1f175030cda5f7a2260 MD5 · raw file

  1. /*
  2. * Copyright 2013-2022 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.mongodb.core.aggregation;
  17. import org.bson.Document;
  18. import org.springframework.data.mongodb.core.query.CriteriaDefinition;
  19. import org.springframework.util.Assert;
  20. /**
  21. * Encapsulates the {@code $match}-operation.
  22. * <p>
  23. * We recommend to use the static factory method
  24. * {@link Aggregation#match(org.springframework.data.mongodb.core.query.Criteria)} instead of creating instances of this
  25. * class directly.
  26. *
  27. * @author Sebastian Herold
  28. * @author Thomas Darimont
  29. * @author Oliver Gierke
  30. * @author Divya Srivastava
  31. * @since 1.3
  32. * @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/">MongoDB Aggregation Framework:
  33. * $match</a>
  34. */
  35. public class MatchOperation implements AggregationOperation {
  36. private final CriteriaDefinition criteriaDefinition;
  37. private final AggregationExpression expression;
  38. /**
  39. * Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}.
  40. *
  41. * @param criteriaDefinition must not be {@literal null}.
  42. */
  43. public MatchOperation(CriteriaDefinition criteriaDefinition) {
  44. Assert.notNull(criteriaDefinition, "Criteria must not be null");
  45. this.criteriaDefinition = criteriaDefinition;
  46. this.expression = null;
  47. }
  48. /**
  49. * Creates a new {@link MatchOperation} for the given {@link AggregationExpression}.
  50. *
  51. * @param expression must not be {@literal null}.
  52. * @since 3.3
  53. */
  54. public MatchOperation(AggregationExpression expression) {
  55. Assert.notNull(expression, "Expression must not be null");
  56. this.criteriaDefinition = null;
  57. this.expression = expression;
  58. }
  59. @Override
  60. public Document toDocument(AggregationOperationContext context) {
  61. return new Document(getOperator(),
  62. context.getMappedObject(expression != null ? expression.toDocument() : criteriaDefinition.getCriteriaObject()));
  63. }
  64. @Override
  65. public String getOperator() {
  66. return "$match";
  67. }
  68. }