gpt4 book ai didi

java - MongoDB聚合Java API不返回结果

转载 作者:行者123 更新时间:2023-11-30 08:02:07 26 4
gpt4 key购买 nike

您好,我正在使用 Java API 连接到我的数据库。我正在使用新的类来连接,因为几乎所有示例中的类都被标记为已弃用我使用 MongoClient 连接到 MongoDatabase 数据库。之后我会这样做:

MongoCollection<Document> coll = db.getCollection("collName");
AggregateIterable<Document> res = collection.aggregate(myquery);
for(Document d : res)
{
System.out.println(d.toJson());
}

它不打印任何内容。我也尝试使用 result.first() 但它只打印 null。

现在我不发布有关我的数据查询的详细信息的原因如下。如果我查看/var/log/mongodb 中的日志文件,我会看到翻译后的查询。如果我将确切的查询发布到 mongo shell 中。它就像一个魅力。我在这里发现一篇文章说我的聚合函数的顺序可能是一个问题,但如果粘贴到 shell 中它可以工作,那就没有意义了。

这是我修改后的查询

Document sort = new Document("$sort", new Document("Day", 1));

Document matchBeforeUnwindDayOfTheWeek = new Document("Day", day);

Document matchBeforeUnwindVar1 = new Document("'value.Data'", new Document("$elemMatch",
new Document("var1", new Document("$gt", minvar1).append("$lt", maxvar1))));
Document matchBeforeUnwindVar2 = new Document("'value.Data'", new Document("$elemMatch",
new Document("var2", new Document("$gt", mingvar2).append("$lt", maxvar2))));

List<Document> matchBeforeUnwindAnd = new LinkedList<Document>();
matchBeforeUnwindAnd.add(matchBeforeUnwindDayOfTheWeek);
matchBeforeUnwindAnd.add(matchBeforeUnwindVar1);
matchBeforeUnwindAnd.add(matchBeforeUnwindVar2);

Document matchBeforeUnwind = new Document("$match", new Document("$and", matchBeforeUnwindAnd));

Document unwind = new Document("$unwind", "$value.Data");

Document matchAfterUnwindVar1 = new Document("'value.Data.var1'",
new Document("$gt", minvar1).append("$lt", minvar1));
Document matchAfterUnwindVar2 = new Document("'value.Data.var2'",
new Document("$gt", minvar2).append("$lt", maxvar2));

List<Document> matchAfterUnwindAnd = new LinkedList<Document>();
matchAfterUnwindAnd.add(matchAfterUnwindVar1);
matchAfterUnwindAnd.add(matchAfterUnwindVar2);

Document matchAfterUnwind = new Document("$match", new Document("$and", matchAfterUnwindAnd));

Document groupFields = new Document("_id", "$_id");
groupFields.put("Grouped", new Document("$push", "$value.Data"));

Document group = new Document("$group", groupFields);

List<Document> query = new LinkedList<Document>();
query.add(sortByDayOfTheWeek);
query.add(matchBeforeUnwind);
query.add(unwind);
query.add(matchAfterUnwind);
query.add(group);

AggregateIterable<Document> result = collection.aggregate(query);

编辑:我用一个非常简单的查询进行了尝试,如下所示。查询在日志文件中仍然可以正常转换。

最佳答案

ref. of mongo java driver您应该更改 for 语句,如下所示:

import com.mongodb.*;
import com.mongodb.Block;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static java.util.Arrays.asList;

MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("demo");
MongoCollection < Document > collection = database.getCollection("collectionName");
AggregateIterable < Document > res = collection.
aggregate(asList(
new Document("$match", new Document("_id", new ObjectId("55a8ad7f68d7f0852ea1c8a7")))));

res.forEach(new Block < Document > () {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
});

关于java - MongoDB聚合Java API不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com