2017-10-06 92 views
1

我正在開發一個實現爲前端的項目:VUE.JS和後端:Java servlet(tomcat)。我的servlet使用數千種產品在mongoDB數據庫上執行請求。 (我是新的mongodb查詢)。Mongodb java驅動程序性能低下

對我來說問題在於我的servlet需要很多時間來響應數據數組。

我需要建議,以加快我的查詢,並加快我的servlet進程發送數據到前端。

  1. 首先,我想知道,如果在MongoDB中,是否有效地使用這樣的查詢(這是許多查找查詢的例子,因爲我有來從衆多的收藏品的數據):

    db.getCollection('collection_name').aggregate([ 
        { 
         "$match":{ 
         "_id": ObjectId("xxxxxxxxxxxxxxxxx") 
         } 
        }, 
        { 
         "$lookup":{ 
         "from":"collection_name", 
         "localField":"id", 
         "foreignField":"id", 
         "as":"trans" 
         } 
        }, 
        { 
         "$unwind":"$trans" 
        }, 
        { 
         "$lookup":{ 
         "from":"collection_name", 
         "localField":"trans.id", 
         "foreignField":"trans_id", 
         "as":"orders" 
         } 
        }, 
        { 
         "$unwind":"$orders" 
        }, 
        { 
         "$match":{ 
         "$and":[ 
          { 
           "orders.status":"status_state" 
          } 
         ] 
         } 
        }, 
        { 
         "$unwind":"$orders.products" 
        }, 
        { 
         "$lookup":{ 
         "from":"collection_name", 
         "localField":"orders.products.id", 
         "foreignField":"id", 
         "as":"detailsProducts" 
         } 
        }, 
        { 
         "$unwind":"$detailsProducts" 
        }, 
        { 
         "$unwind":"$detailsProducts.products" 
        }, 
        { 
         "$project":{ 
         "_id":1, 
         "orders":1, 
         "detailsProducts": 1 
         } 
        } 
    ]) 
    
  2. 在第二正如我說,我的Java Servlet採取許多時間來執行DATAS處理,definding我的聚合後,我執行循環操作將數據推入JSON數組這樣的:

    // Define json array 
    JSONArray array = new JSONArray(); 
    
    // Get collection 
    AggregateIterable<Document> myCollection = 
        MongoDB.getCollection("coll_name").aggregate(...); 
    
    // Going through all documents (thousands of document) 
    for (Document orderDocument : orderCollection) { 
        // Get all products and they references 
        arrays.put(new JSONObject(orderDocument.toJson())); 
    } 
    
    // AFTER THE END OF LOOP, MY SERVLET SEND THE ARRAY TO THE FRONT 
    

注:我用MongoDB的Java驅動程序3.5工作(我嘗試兩種:核心驅動和異步驅動器)

我希望得到一些建議,以加快我的治療和優化我的要求。

+0

你問有關Java驅動程序的性能,而不是蒙戈本身?如果您在Mongo中直接執行查詢,您會獲得滿意的性能嗎? –

+0

感謝您的回答。當我在Mongo或RoboMongo中直接執行我的查詢時,可以獲得令人滿意的性能。 –

回答

0

建立一個JSONArray與成千上萬的元素不會很好地擴展。你最好直接將JSON流式傳輸回客戶端。因此,假設此方法返回合理時間:

// Get collection 
AggregateIterable<Document> myCollection = 
    MongoDB.getCollection("coll_name").aggregate(...); 

然後像下面應該顯着提高的東西:

import java.io.IOException; 
import javax.servlet.http.HttpServletResponse; 
import org.bson.Document; 
import org.bson.codecs.DocumentCodec; 
import org.bson.codecs.Encoder; 
import org.bson.codecs.EncoderContext; 
import org.bson.json.JsonWriter; 
... 
public void writeAsJsonTo(AggregateIterable<Document> orderCollection, 
     HttpServletResponse httpServletResponse) throws IOException { 
    final Encoder<Document> encoder = new DocumentCodec(); 
    final JsonWriter jsonWriter = 
     new JsonWriter(httpServletResponse.getWriter()); 
    final EncoderContext encoderContext = EncoderContext.builder() 
     .isEncodingCollectibleDocument(true).build(); 

    jsonWriter.writeStartDocument(); 
    jsonWriter.writeStartArray("orders"); 
    // Going through all documents (thousands of document) 
    for (Document orderDocument : orderCollection) { 
     encoder.encode(jsonWriter, orderDocument, encoderContext); 
    } 
    jsonWriter.writeEndArray(); 
    jsonWriter.writeEndDocument(); 
} 
+0

謝謝你的迴應,這非常有幫助。我的治療現在更快。當我的應用程序運行在具有許多許多數據的生產環境中時,我將看到完整的結果。 –