Indexing mongodb json document java driver

I have a couple of the following json documents in a mongo db collection called GOT and I want to index the nested array metaTag array elements in each document in the GOT collection.

Json document

I have tried the following:

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

I’m not sure if this dot notation is the correct way to identify the element in MY json document to index? How can I tell if the index was created successfully?

Edit: added my query:

     // 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();
        }
    }   
}

Answer

You can use following Java code to index on desired key:

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 is details about indexing.

Leave a Reply

Your email address will not be published. Required fields are marked *