• <i id='M8gFy'><tr id='M8gFy'><dt id='M8gFy'><q id='M8gFy'><span id='M8gFy'><b id='M8gFy'><form id='M8gFy'><ins id='M8gFy'></ins><ul id='M8gFy'></ul><sub id='M8gFy'></sub></form><legend id='M8gFy'></legend><bdo id='M8gFy'><pre id='M8gFy'><center id='M8gFy'></center></pre></bdo></b><th id='M8gFy'></th></span></q></dt></tr></i><div id='M8gFy'><tfoot id='M8gFy'></tfoot><dl id='M8gFy'><fieldset id='M8gFy'></fieldset></dl></div>

  • <tfoot id='M8gFy'></tfoot>
    <legend id='M8gFy'><style id='M8gFy'><dir id='M8gFy'><q id='M8gFy'></q></dir></style></legend>

    1. <small id='M8gFy'></small><noframes id='M8gFy'>

          <bdo id='M8gFy'></bdo><ul id='M8gFy'></ul>
      1. Spring框架中子文档数组字段中的过滤数组

        时间:2024-08-24
        <i id='OSj1l'><tr id='OSj1l'><dt id='OSj1l'><q id='OSj1l'><span id='OSj1l'><b id='OSj1l'><form id='OSj1l'><ins id='OSj1l'></ins><ul id='OSj1l'></ul><sub id='OSj1l'></sub></form><legend id='OSj1l'></legend><bdo id='OSj1l'><pre id='OSj1l'><center id='OSj1l'></center></pre></bdo></b><th id='OSj1l'></th></span></q></dt></tr></i><div id='OSj1l'><tfoot id='OSj1l'></tfoot><dl id='OSj1l'><fieldset id='OSj1l'></fieldset></dl></div>

            <bdo id='OSj1l'></bdo><ul id='OSj1l'></ul>
              <tfoot id='OSj1l'></tfoot>
              <legend id='OSj1l'><style id='OSj1l'><dir id='OSj1l'><q id='OSj1l'></q></dir></style></legend>

              1. <small id='OSj1l'></small><noframes id='OSj1l'>

                  <tbody id='OSj1l'></tbody>
                  本文介绍了Spring框架中子文档数组字段中的过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 Spring Framework 项目的 MongoDB 中的数组中获取元素.

                  I am trying to fetch an element from an array in the MongoDB in my Spring Framework project.

                  我找到了MongoDB shell的解决方案,但我不知道如何通过Spring.data.core.aggregation实现它,Spring不支持聚合运算符@addFields之一.

                  I have find the solution for MongoDB shell, but I do not know how to implement it by Spring.data.core.aggregation, one of aggregation operator @addFields is not supported by Spring.

                  谁能告诉我如何替换这个@addField 或如何以另一种方式实现它?非常感谢!!!

                  Could anyone tell me how to replace this @addField or how to implement in it another way? Thank you so much!!!

                  MongoDB 示例数据:

                  MongoDB sample data:

                  {
                      "_id" : 15,
                      "items" : [
                              {
                                      "columns" : [
                                              {
                                                      "title" : "hhh",
                                                      "value" : 10
                                              },
                                              {
                                                      "title" : "hahaha",
                                                      "value" : 20
                                              }
                                      ]
                              },
                              {
                                      "columns" : [
                                              {
                                                      "title" : "hiii",
                                                      "value" : 50
                                              }
                                      ]
                              }
                      ]
                  }
                  

                  预期结果:

                  {
                  "_id" : 15,
                  "items" : [
                          {
                                  "columns" : [
                                          {
                                                  "title" : "hahaha",
                                                  "value" : 20
                                          }
                                  ]
                          },
                          {
                                  "columns" : []
                          }
                  ]
                  

                  }

                  MongoDB Shell的解决方案:

                  The solution for MongoDB Shell:

                  let value = "hahaha";
                  
                  db.coll.aggregate([
                      {
                          "$addFields": { 
                              "items": { 
                                  "$map": { 
                                      "input": "$items", 
                                      "as": "item", 
                                      "in": { 
                                          "columns": { 
                                              "$filter": { 
                                                  "input": "$$item.columns", 
                                                  "as": "elt", 
                                                  "cond": { "$eq": [ "$$elt.title", value ] } 
                                              } 
                                          }
                                      }
                                  } 
                              } 
                          } 
                      } 
                  ])
                  

                  MongoDB 版本:3.4.1
                  春季版:1.4.3

                  MongoDB version: 3.4.1
                  Spring version: 1.4.3

                  推荐答案

                  您可以尝试以下方法,但您需要使用 1.8.5 版本.

                  You can try the following but you'll need to use 1.8.5 version.

                  Aggregation aggregation = newAggregation(
                              project("_id").and(new AggregationExpression() {
                                  @Override
                                  public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                                      DBObject filter = new BasicDBObject("input", "$$item.columns").append("as", "elt").append("cond",
                                              new BasicDBObject("$eq", Arrays.<Object>asList("$$elt.title", "hahaha")));
                                      DBObject map = new BasicDBObject("input", "$items").append("as", "item").append("in", filter);
                                      return new BasicDBObject("$map", map);
                                  }
                              }).as("items")
                    );
                  

                  在 1.10.0.RC1 中添加了对一些 Mongo3.2 聚合运算符的支持.如果您可以更新到发布候选版本,您可以使用以下版本.我在 RC 中找不到 $addFields 阶段,所以保留了 $project 阶段.

                  The support for some of Mongo3.2 aggregation operators were added in 1.10.0.RC1. If you are okay with updating to release candidate version you can use the below version. I couldn't find $addFields stage in the RC so kept the $project stage.

                  Aggregation aggregation = newAggregation(
                              project("_id")
                                      .and(mapItemsOf("items").as("item").andApply(filter("item.columns")
                                              .as("elt")
                                              .by(valueOf("elt.title").equalToValue("hahaha"))
                                      )).as("items")
                  );
                  

                  静态导入:

                  import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
                  import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
                  import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;
                  import static org.springframework.data.mongodb.core.aggregation.VariableOperators.mapItemsOf;
                  

                  这篇关于Spring框架中子文档数组字段中的过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java类中的Grails @Autowire不起作用 下一篇:如何在 Eclipse 中启用 TODO/FIXME/XXX 任务标签?

                  相关文章

                  <legend id='d5yCj'><style id='d5yCj'><dir id='d5yCj'><q id='d5yCj'></q></dir></style></legend>
                • <i id='d5yCj'><tr id='d5yCj'><dt id='d5yCj'><q id='d5yCj'><span id='d5yCj'><b id='d5yCj'><form id='d5yCj'><ins id='d5yCj'></ins><ul id='d5yCj'></ul><sub id='d5yCj'></sub></form><legend id='d5yCj'></legend><bdo id='d5yCj'><pre id='d5yCj'><center id='d5yCj'></center></pre></bdo></b><th id='d5yCj'></th></span></q></dt></tr></i><div id='d5yCj'><tfoot id='d5yCj'></tfoot><dl id='d5yCj'><fieldset id='d5yCj'></fieldset></dl></div>
                    <bdo id='d5yCj'></bdo><ul id='d5yCj'></ul>

                    <tfoot id='d5yCj'></tfoot>

                    <small id='d5yCj'></small><noframes id='d5yCj'>