MongoDB $addFields using org.springframework.data.mongodb.core.MongoTemplate

拥有回忆 提交于 2021-01-29 22:44:34

问题


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.


回答1:


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

Now make CustomAggregation class.

public class CustomAggregation extends Aggregation {

  public static AddFieldOperation addField(final Document document) {

    return new AddFieldOperation(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));

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.



来源:https://stackoverflow.com/questions/55282406/mongodb-addfields-using-org-springframework-data-mongodb-core-mongotemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!