org.springframework.data.mongodb.core.query.Criteria.elemMatch()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(309)

本文整理了Java中org.springframework.data.mongodb.core.query.Criteria.elemMatch()方法的一些代码示例,展示了Criteria.elemMatch()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Criteria.elemMatch()方法的具体详情如下:
包路径:org.springframework.data.mongodb.core.query.Criteria
类名称:Criteria
方法名:elemMatch

Criteria.elemMatch介绍

[英]Creates a criterion using the $elemMatch operator
[中]使用$elemMatch运算符创建条件

代码示例

代码示例来源:origin: com.epam.reportportal/commons-dao

  1. @Override
  2. public List<UserFilter> findAvailableFilters(String projectName, String[] ids, String userName) {
  3. //where ID from provided array AND it's shared on project
  4. Query q = Query.query(where(ID).in(Arrays.asList(ids))
  5. .andOperator(new Criteria()
  6. .orOperator(
  7. where(OWNER).is(userName),
  8. where(PROJECT).is(projectName),
  9. where(ENTRIES).elemMatch(where("projectId").is(projectName)))));
  10. return mongoTemplate.find(q, UserFilter.class);
  11. }
  12. }

代码示例来源:origin: com.github.rutledgepaulv/q-builders

  1. @Override
  2. protected Criteria visit(ComparisonNode node) {
  3. ComparisonOperator operator = node.getOperator();
  4. Collection<?> values = node.getValues().stream().map(normalizer).collect(Collectors.toList());
  5. String field = node.getField().asKey();
  6. if(ComparisonOperator.EQ.equals(operator)) {
  7. return where(field).is(single(values));
  8. } else if(ComparisonOperator.NE.equals(operator)) {
  9. return where(field).ne(single(values));
  10. } else if (ComparisonOperator.EX.equals(operator)) {
  11. return where(field).exists((Boolean)single(values));
  12. } else if (ComparisonOperator.GT.equals(operator)) {
  13. return where(field).gt(single(values));
  14. } else if (ComparisonOperator.LT.equals(operator)) {
  15. return where(field).lt(single(values));
  16. } else if (ComparisonOperator.GTE.equals(operator)) {
  17. return where(field).gte(single(values));
  18. } else if (ComparisonOperator.LTE.equals(operator)) {
  19. return where(field).lte(single(values));
  20. } else if (ComparisonOperator.IN.equals(operator)) {
  21. return where(field).in(values);
  22. } else if (ComparisonOperator.NIN.equals(operator)) {
  23. return where(field).nin(values);
  24. } else if (ComparisonOperator.RE.equals(operator)) {
  25. return where(field).regex((String)single(values));
  26. } else if (ComparisonOperator.SUB_CONDITION_ANY.equals(operator)) {
  27. return where(field).elemMatch(condition(node));
  28. }
  29. throw new UnsupportedOperationException("This visitor does not support the operator " + operator + ".");
  30. }

代码示例来源:origin: com.bq.oss.lib/queries-mongo

  1. private Criteria criteria(QueryOperator operator, String field, QueryLiteral<?> value) {
  2. Criteria criteria = new Criteria(field);
  3. switch (operator) {
  4. case $ALL:
  5. return criteria.all(((ListQueryLiteral) value).getLiterals());
  6. case $EQ:
  7. return criteria.is(value.getLiteral());
  8. case $GT:
  9. return criteria.gt(value.getLiteral());
  10. case $GTE:
  11. return criteria.gte(value.getLiteral());
  12. case $IN:
  13. return criteria.in(((ListQueryLiteral) value).getLiterals());
  14. case $NIN:
  15. return criteria.nin(((ListQueryLiteral) value).getLiterals());
  16. case $LT:
  17. return criteria.lt(value.getLiteral());
  18. case $LTE:
  19. return criteria.lte(value.getLiteral());
  20. case $NE:
  21. return criteria.ne(value.getLiteral());
  22. case $LIKE:
  23. return criteria.regex((String) value.getLiteral(), "i"); // i means case insensitive
  24. case $ELEM_MATCH:
  25. return criteria.elemMatch(getCriteriaFromResourceQuery((ResourceQuery) value.getLiteral()));
  26. case $EXISTS:
  27. return criteria.exists((Boolean) value.getLiteral());
  28. }
  29. return criteria;
  30. }

相关文章