如何在java中使用mongodb驱动程序使用累加器进行聚合查询
我对MongoDB及其与java的交互非常陌生。 我用的是这个司机如何在java中使用mongodb驱动程序使用累加器进行聚合查询,java,mongodb,Java,Mongodb,我对MongoDB及其与java的交互非常陌生。 我用的是这个司机 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.4.2</version> </dependency> 我编写的java代码如下(但是我不确定是否正确使用了addToSet方法): Ag
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
我编写的java代码如下(但是我不确定是否正确使用了addToSet方法):
AggregateIterable输出=collection.aggregate(Arrays.asList(
新文档(“$match”,新文档(“val.elem.0001”,新文档(“$exists”,true)),
新文档(“$project”,新文档(“字段路径”,“$val.elem.0001”),
新文档(“$group”,新文档(“\u id”,新文档(“字段”,新文档(“$literal”,“0001”))
.append(“PATH”,acculators.addToSet(“$PATH”,“$FIELD_PATH”;”));
这是正确的吗?因为如果我添加“附加”部分,我就不能在屏幕上打印结果。返回的错误是
找不到com.mongodb.client.model.BsonField类的编解码器
因此,为了继续并使我所问的内容更具可读性和全面性:
- 查询的java表示是否正确
- 如果是,为什么我不能打印或访问查询结果
提前感谢,如果您需要更多信息,我随时准备提供。您使用的api不正确 将您的聚合更改为以下(对于表达式,请坚持使用
文档
)
您可以通过使用静态导入进一步减少聚合
import static com.mongodb.client.model.Accumulators.addToSet;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Projections.computed;
import static java.util.Arrays.*;
AggregateIterable<Document> output = collection.aggregate(asList(
match(Filters.exists("val.elem.0001")),
project(computed("FIELD_PATH","$val.elem.0001")),
group( new Document("FIELD", new Document("$literal", "0001")), addToSet("PATH", "$FIELD_PATH"))));
导入静态com.mongodb.client.model.acumerators.addToSet;
导入静态com.mongodb.client.model.Aggregates.group;
导入静态com.mongodb.client.model.Aggregates.match;
导入静态com.mongodb.client.model.Aggregates.project;
导入静态com.mongodb.client.model.Projections.computed;
导入静态java.util.Arrays.*;
AggregateIterable输出=collection.aggregate(asList(
匹配(Filters.exists(“val.elem.0001”),
项目(计算(“字段路径”,“$val.elem.0001”),
组(新文档(“字段”,新文档($literal”,“0001”)),addToSet(“路径”,“$FIELD_PATH”));
更多信息
您使用的api不正确 将您的聚合更改为以下(对于表达式,请坚持使用
文档
)
您可以通过使用静态导入进一步减少聚合
import static com.mongodb.client.model.Accumulators.addToSet;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Projections.computed;
import static java.util.Arrays.*;
AggregateIterable<Document> output = collection.aggregate(asList(
match(Filters.exists("val.elem.0001")),
project(computed("FIELD_PATH","$val.elem.0001")),
group( new Document("FIELD", new Document("$literal", "0001")), addToSet("PATH", "$FIELD_PATH"))));
导入静态com.mongodb.client.model.acumerators.addToSet;
导入静态com.mongodb.client.model.Aggregates.group;
导入静态com.mongodb.client.model.Aggregates.match;
导入静态com.mongodb.client.model.Aggregates.project;
导入静态com.mongodb.client.model.Projections.computed;
导入静态java.util.Arrays.*;
AggregateIterable输出=collection.aggregate(asList(
匹配(Filters.exists(“val.elem.0001”),
项目(计算(“字段路径”,“$val.elem.0001”),
组(新文档(“字段”,新文档($literal”,“0001”)),addToSet(“路径”,“$FIELD_PATH”));
更多信息
您编写的第一个聚合不起作用,因为$addToSet的解释失败。但是其他的都很完美,所以非常感谢你对我的帮助!在使用聚合器时,我将使用这种方法。您编写的第一个聚合不起作用,因为$addToSet的解释失败。但是其他的都很完美,所以非常感谢你对我的帮助!在使用聚合器时,我将使用这种方法。
AggregateIterable<Document> output = collection.aggregate(Arrays.asList(
new Document("$match", new Document("val.elem.0001",new Document("$exists",true))),
new Document("$project", new Document("FIELD_PATH","$val.elem.0001")),
new Document("$group", new Document("_id",new Document("FIELD", new Document("$literal", "0001"))).append("PATH", new Document("$addToSet", "$FIELD_PATH")))));
AggregateIterable<Document> output = collection.aggregate(Arrays.asList(
Aggregates.match(Filters.exists("val.elem.0001")),
Aggregates.project(Projections.computed("FIELD_PATH","$val.elem.0001")),
Aggregates.group( new Document("FIELD", new Document("$literal", "0001")), Accumulators.addToSet("PATH", "$FIELD_PATH"))));
import static com.mongodb.client.model.Accumulators.addToSet;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Projections.computed;
import static java.util.Arrays.*;
AggregateIterable<Document> output = collection.aggregate(asList(
match(Filters.exists("val.elem.0001")),
project(computed("FIELD_PATH","$val.elem.0001")),
group( new Document("FIELD", new Document("$literal", "0001")), addToSet("PATH", "$FIELD_PATH"))));