我在一個名爲GOT的mongo db集合中有幾個下面的json文檔,我想索引GOT集合中每個文檔中的嵌套數組metaTag數組元素。索引mongodb json文件java驅動
我曾嘗試以下:
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();
}
}
}
[用MongoDb創建索引]的可能重複(http://stackoverflow.com/questions/14006294/create-an-index-with-mongodb) – ka4eli