MongoDB $addFields 使用 org.springframework.data.mongodb.core.MongoTemplate [英] MongoDB $addFields using org.springframework.data.mongodb.core.MongoTemplate

查看:70
本文介绍了MongoDB $addFields 使用 org.springframework.data.mongodb.core.MongoTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在 Spring Data MongoDB Reactive 中编写 $addFields 查询以实现更简单和稍微复杂的字段添加,如下所示:

How do I go about writing an $addFields query in Spring Data MongoDB Reactive for a simpler and a slightly more complex field addition as shown below:

db.getCollection("mycollection").aggregate(
[
    { 
        "$addFields" : {
            "existingObjectField.newFieldArray" : [
                "$existingObjectField.existingFieldObject"
            ]
        }
    },
    { 
        "$addFields" : {
            "existingFieldArray" : {
                "$map" : {
                    "input" : "$existingFieldArray", 
                    "as" : "item", 
                    "in" : {
                        "existingFieldObject" : {
                            "_id" : "$$item. existingFieldObject._id",
                            "newFieldArray" : [
                                "$$item. existingFieldObject.existingFieldObject"
                            ]
                        }
                    }
                }
            }
        }
    },
    { 
        "$out" : "mycollection"
    }
]

);

在第一个添加字段中,我只是使用现有对象字段之一创建一个新的数组字段.

In the first add fields, I am simply creating a new array field with one of the existing object field.

在第二个添加字段中,执行相同但在文档数组中的对象内.

In the 2nd add fields, doing the same but within an object in an array in the document.

推荐答案

Like for match/unwind AddFieldOperation is not present in spring data mongo 但是你可以自己写以及一个 custom Aggregation 类,用于将调用方方法添加到 addFieldOpration,如下所示.

Like for match/unwind AddFieldOperation is not present in spring data mongo But you can write your own and also a custom Aggregation class to add caller method to addFieldOpration as below.

 public class AddFieldOperation implements AggregationOperation {

      private final Document document;

      /**
       * Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}.
       *
       * @param criteriaDefinition must not be {@literal null}.
       */
      public AddFieldOperation(final Document document) {

        Assert.notNull(document, "Criteria must not be null!");
        this.document = document;
      }

      /*
       * (non-Javadoc)
       *
       * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.
       * springframework.data.mongodb.core.aggregation.AggregationOperationContext)
       */
      @Override
      public Document toDocument(final AggregationOperationContext context) {

        return new Document("$addFields", this.document);
      }
    }

现在创建 CustomAggregation 类.

Now make CustomAggregation class.

public class CustomAggregation extends Aggregation {

  public static AddFieldOperation addField(final Document document) {

    return new AddFieldOperation(document);
  }
}

一切准备就绪,您需要调用 Addfield 方法并在 Document 对象示例中传递所有查询:-

Everything is ready you need to call Addfield method and pass all query in Document object example:-

AddFieldOperation addField =
        CustomAggregation.addField(new Document().append("fieldName", fieldValue));

注意
Document 类来自 import package org.bson.Document;
这是将文档表示为 {@code Map}.
spring data mongo 中实现的所有聚合操作最终都转换为 Document 对象,这将在 shell 中执行.因此,如果某些聚合管道尚未在 spirng data 中实现,在这种情况下,我们可以编写自己的并传递在 mongo shell 中编写的查询,我们只需将其传递到 Document 对象中.

Note
Document class is from import package org.bson.Document;
Which is a representation of a document as a {@code Map}.
All aggregation operation implemented in spring data mongo is finally converted to the Document object and this will execute in the shell. So if some of the aggregation pipelines are not yet implemented in spirng data, in that case, we can write our own and pass the query which is written in mongo shell we can just pass it in the Document object.

这篇关于MongoDB $addFields 使用 org.springframework.data.mongodb.core.MongoTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆