MongoDB 汇总 Java API 没有 return 结果

MongoDB aggregate Java API doesnt return results

您好,我正在使用 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.

我不post具体查询数据的原因如下。如果我查看 /var/log/mongodb 中的日志文件,我会看到翻译后的查询。如果我post THE EXACT query变成了mongo shell。它就像一个魅力。我在这里发现 post 说我的聚合函数的顺序可能有问题,但如果粘贴在 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);

编辑: 我尝试了一个非常简单的查询,例如下面的 posted。日志文件中的查询仍然可以正常翻译。

对于 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());
     }
 });

错误是 '' 个字符。我认为我需要它们是因为 .-operator。那好吧。谢谢大家的帮助