使用org.springframework.data.MongoDB.core.MongoTemplate的MongoDB$addFields
如何在Spring Data MongoDB中编写一个使用org.springframework.data.MongoDB.core.MongoTemplate的MongoDB$addFields,mongodb,spring-data-mongodb,Mongodb,Spring Data Mongodb,如何在Spring Data MongoDB中编写一个$addFields查询,以实现更简单、更复杂的字段添加,如下所示: db.getCollection("mycollection").aggregate( [ { "$addFields" : { "existingObjectField.newFieldArray" : [ "$existingObjectField.existingFieldObject"
$addFields
查询,以实现更简单、更复杂的字段添加,如下所示:
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"
}
]
))
在第一个add字段中,我只是用一个现有的object字段创建一个新的数组字段
在第二个添加字段中,执行相同的操作,但在文档数组中的对象内。类似于
match/unwind
AddFieldOperation在spring data mongo
中不存在,但您可以编写自己的,也可以编写一个自定义聚合
类,将调用方方法添加到AddFieldOperation
,如下所示
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类
public class CustomAggregation extends Aggregation {
public static AddFieldOperation addField(final Document document) {
return new AddFieldOperation(document);
}
}
一切就绪,您需要调用Addfield
方法并传递文档对象示例中的所有查询:-
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对象中。类似于match/unwind
AddFieldOperation在spring data mongo
中不存在,但您可以编写自己的查询,也可以编写一个自定义聚合
类来添加调用方方法添加字段操作
如下
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类
public class CustomAggregation extends Aggregation {
public static AddFieldOperation addField(final Document document) {
return new AddFieldOperation(document);
}
}
一切就绪,您需要调用Addfield
方法并传递文档对象示例中的所有查询:-
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对象中即可。添加字段的new Document()
是什么?我想对整个集合运行我的查询。我了解什么是org.bson.Document
。我想问的是,这里的上下文值是什么。您能否将第一个添加的字段<代码>现有ObjectField.newFieldArray和第二个添加的字段<代码>现有FieldArray(在我的MongoDB查询中)的<代码>值部分放在示例中进行演示?我们将该字段添加到的<代码>新文档()是什么?我想对整个集合运行我的查询。我了解什么是org.bson.Document
。我想问的是,这里的上下文值是什么。您能否将第一个添加的字段现有ObjectField.newFieldArray
和第二个添加的字段现有FieldArray
(在我的MongoDB查询中)的部分放入示例中进行演示?