2015-08-09 49 views
0

我在一個名爲GOT的mongo db集合中有幾個下面的json文檔,我想索引GOT集合中每個文檔中的嵌套數組metaTag數組元素。索引mongodb json文件java驅動

Json document

我曾嘗試以下:

BasicDBObject index = new BasicDBObject("season.questions.questionEntry.metaTags",1); 
mongoColl.createIndex(index); 

我不知道,如果這點表示法是確定的json文件索引元素中的正確方法是什麼? 如何判斷索引是否成功創建?

編輯:加入我的查詢:

 // Query our collection documents metaTag elements for a matching string 
// @SuppressWarnings("deprecation") 
public void queryMetaTags(String query) 
{ 
    // Query to search all documents in current collection 
    List<String> continentList = Arrays.asList(new String[]{query}); 
    DBObject matchFields = new 
     BasicDBObject("season.questions.questionEntry.metaTags", 
     new BasicDBObject("$in", continentList)); 
    DBObject groupFields = new BasicDBObject("_id", "$_id").append("questions", 
     new BasicDBObject("$push","$season.questions")); 
    //DBObject unwindshow = new BasicDBObject("$unwind","$show"); 
    DBObject unwindsea = new BasicDBObject("$unwind", "$season"); 
    DBObject unwindepi = new BasicDBObject("$unwind", "$season.questions"); 
    DBObject match = new BasicDBObject("$match", matchFields); 
    DBObject group = new BasicDBObject("$group", groupFields); 
    @SuppressWarnings("deprecation") 
    AggregationOutput output = 
    mongoColl.aggregate(unwindsea,unwindepi,match,group); 

    String jsonString = null; 
    JSONObject jsonObject = null; 
    jsonResultsArray = null; 
    ourResultsArray = new ArrayList<JSONObject>(); 

    // Loop for each document in our collection 
    for (DBObject result : output.results()) 
    {  
     try 
     { 
      // Parse our results so we can add them to an ArrayList 
      jsonString = JSON.serialize(result);    
      jsonObject = new JSONObject(jsonString); 
      jsonResultsArray = jsonObject.getJSONArray("questions"); 

      // Put each of our returned questionEntry elements into an ArrayList 
      for (int i = 0; i < jsonResultsArray.length(); i++) 
      { 
       System.out.println("jsonResultsArray element (" + i + "): " + jsonResultsArray.getJSONObject(i).toString()); 
       ourResultsArray.add(jsonResultsArray.getJSONObject(i));   
       //System.out.println("ourResultsArray element (" + i + "): " + ourResultsArray.get(i).toString()); 
      } 
     } 
     catch (JSONException e1) 
     { 
      e1.printStackTrace(); 
     } 
    } 
} 
+0

[用MongoDb創建索引]的可能重複(http://stackoverflow.com/questions/14006294/create-an-index-with-mongodb) – ka4eli

回答

1

您可以使用下面的Java代碼索引所需的關鍵:

Mongo mongo = new Mongo("localhost", 27017); 
    DB db = mongo.getDB("dbName"); //set dbname name here 

    DBCollection collection = db.getCollection("collectionName"); 
    collection.createIndex(new BasicDBObject("season.questions.questionEntry.metaTags",1)); 

Here是關於索引的詳細信息。

+0

@ Vishwas感謝您的答覆,因爲我不確定點符號是正確的用法。有一個問題,儘管在我的文檔中嘗試了這種索引方法(請參閱上面的鏈接示例),但是沒有性能差異!所以我想知道這是否正常工作? – TripVoltage

+0

性能取決於您用於索引和查詢的字段。您需要在正確的字段上創建索引以提高性能。您可以在查詢中使用'.explain(「executionStats」)'來獲取有關索引使用的詳細信息。例如'db.collection.find({你的查詢})。explain(「executionStats」)' – Vishwas

+0

我已經添加了我的複雜查詢並且不知道如何使用這種類型的查詢產生執行統計信息? – TripVoltage

相關問題