У меня есть несколько следующих json-документов в коллекции mongo db под названием GOT, и я хочу индексировать элементы массива вложенных массивов metaTag в каждом документе в коллекции GOT.Индексирование mongodb json document java driver
Я попытался следующие:
BasicDBObject index = new BasicDBObject("season.questions.questionEntry.metaTags",1);
mongoColl.createIndex(index);
Я не уверен, если это точечная нотация является правильным способом для идентификации элемента в МОЕЙ JSon документе индекса? Как узнать, успешно ли был создан индекс?
Edit: добавлен мой запрос:
// 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