0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@ Vishwas Thanks for the reply as I wasn't sure the dot notation was the correct usage. One question though having tried this method of indexing on my documents (see sample in link above) in this collection there is no performance difference! So I'm wondering if this has worked correctly?
Performance depends on which fields you are used to index and query. You need to create index on right fields to improve performance. you can use .explain("executionStats") with your query to get details about use of index. e.g. db.collection.find({your query}).explain("executionStats")
I have added my complex query and not sure how to produce execution stats with this type of query?
if it is aggregation then use explain:true after pipeline. for simple find then use above given syntax.
@ Vishwas Haha I was not the author of this query - can you please explain?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.