[英]MongoDB aggregate Java API doesnt return results
嗨,我正在使用Java API连接到我的数据库。 我使用新的类进行连接,因为几乎所有示例中的类都已标记为不赞成使用我使用MongoClient连接到MongoDatabase db。 之后,我要做:
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中。 它就像一个魅力。 我在这里找到了一个帖子,说我的聚合函数的顺序可能是个问题,但是如果粘贴到外壳中就可以了,那是没有意义的。
这是我修改后的查询
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);
编辑:我尝试了一个非常简单的查询,例如下面发布的查询。 查询仍然可以在日志文件中正常翻译。
与参考。 mongo Java驱动程序 ,应更改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。 那好吧。 谢谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.