Search code examples
mongodbmongodb-java

How to create a compound text index in MongoDB using java driver


I have a requirement to create a compound text index on two fields. I am using java driver. I found examples on how to create index for compound fields but not for text indexes. How can I achieve this using java driver


Solution

  • Something along the lines of the following code should do the trick (untested):

    BasicDBObject obj = new BasicDBObject();
    obj.put("name", 1);
    obj.put("comment", "text");
    collection.ensureIndex(obj);