Java 如何优化Mongodb的查询

Java 如何优化Mongodb的查询,java,mongodb,Java,Mongodb,我在这个特定的收藏中有30万份文件。 每个文件被视为一次出租车旅行。 每个文档都包含一个出租车站号和一个执照号 我的目标是计算每个出租车站每个出租车牌照的出行次数。 例如: 出租车站A许可证X有5次行程。 出租车A站有9次出行。等等 如何优化我的查询?它需要30分钟以上的时间才能完成 List /*of*/ taxistationOfCollection, taxiLicenseOfTaxistation; //Here I get all the distinct TaxiSt

我在这个特定的收藏中有30万份文件。 每个文件被视为一次出租车旅行。 每个文档都包含一个出租车站号和一个执照号

我的目标是计算每个出租车站每个出租车牌照的出行次数。
例如:
出租车站A许可证X有5次行程。
出租车A站有9次出行。等等

如何优化我的查询?它需要30分钟以上的时间才能完成

List /*of*/ taxistationOfCollection, taxiLicenseOfTaxistation;
        //Here I get all the distinct TaxiStation numbers in the collection
        taxistationOfCollection = coll.distinct("TaxiStation");

        BasicDBObject query, tripquery;
        int tripcount;

        //Now I have to loop through each Taxi Station
        for(int i = 0; i<taxistationOfCollection.size(); i++)
        {
            query = new BasicDBObject("TaxiStation", taxistationOfCollection.get(i));
            //Here, I make a list of each distinct Taxi License in the current Taxi station
            taxiLicenseOfTaxistation = coll.distinct("TaxiLicense", query);

            //Now I make a loop to process each Taxi License within the current Taxi station
            for(int k = 0; k<taxiLicenseOfTaxistation.size();k++)
            {
                tripcount=0;
                if(taxiLicenseOfTaxistation.get(k) !=null)
                {
                    //I'm looking for each Taxi Station with this Taxi License
                    tripquery= new BasicDBObject("TaxiStation", taxistationOfCollection.get(i)).append("TaxiLicense", taxiLicenseOfTaxistation.get(k));
                    DBCursor cursor = coll.find(tripquery);

                    try {
                        while(cursor.hasNext()) {
                            //Increasing my counter everytime I find a match
                            tripcount++;
                            cursor.next();
                        } 
                    } finally {
                        //Finally printing the results
                        System.out.println("Station: " + taxistationOfCollection.get(i) + " License:" + taxiLicenseOfTaxistation.get(k)
                                + " Trips: " + tripcount);
                    }



                }
            }
        }
可能就是你要找的。通过聚合操作,整个代码在数据库上运行,只需几行即可执行。性能也应该更好,因为数据库可以处理所有需要完成的事情,并且可以充分利用索引和其他内容

从你发布的内容来看,这可以归结为一个问题。在外壳中,这看起来像:

db.taxistationOfCollection.aggregate([
                         {$group: 
                             { _id:
                                    {station: "$TaxiStation", 
                                    licence: "$TaxiLicense"},
                              count : {$sum : 1}
                          }
                        ])
这将为您提供表单的文档

{_id : {station: stationid, licence: licence_number}, count: number_of_documents}
对于Java,它将如下所示:

 DBObject taxigroup = new BasicDBObject("$group",
                               new BasicDBObject("_id", 
                                   new BasicDBObject("station","$TaxiStation")
                                   .append("Licence","$TaxiLicense"))
                               .append("count", new BasicDBObject("$sum",1)));
AggregationOutput aggout = taxistationOfCollection.aggregate(
                                                      Arrays.asList(taxigroup));

请注意,代码片段没有经过测试。

您能发布一个示例文档吗?很难从一开始就把它拼凑起来query@Trudbert当然我知道如何使用
find(tripquery)
效率很低,但我不确定如何解决这个问题。由于某种原因,我在最后一部分遇到了一个错误。类型列表的聚合(列表)未定义您使用的驱动程序版本是什么?这是相关文档,可能是一些命名冲突,比如:?文档中的2.2是数据库版本,而不是驱动程序版本。驱动程序V2.12应该在数据库2.2上支持这一点。我从这里获得了2.12驱动程序。
 DBObject taxigroup = new BasicDBObject("$group",
                               new BasicDBObject("_id", 
                                   new BasicDBObject("station","$TaxiStation")
                                   .append("Licence","$TaxiLicense"))
                               .append("count", new BasicDBObject("$sum",1)));
AggregationOutput aggout = taxistationOfCollection.aggregate(
                                                      Arrays.asList(taxigroup));