user3631644
user3631644

Reputation:

MongoDB : Hint with multiple columns in java query example

In a collection subject, in which the documents have social,maths and english as fields. If I need to provide a hint to the following find query, how do I do it?

db.subject.find({maths : 30, social :10,english : 20});

Upvotes: 2

Views: 2394

Answers (3)

Madhu
Madhu

Reputation: 73

I had created index from command line. I tried this from Java,

Query searchQuery = new Query(Criteria.where("maths").is(30));
searchQuery.withHint("index_name");

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49402

You can try this, I haven't tested it :

BasicDBObject query = new BasicDBObject("maths", 30)
                     .append("social",10).append("english",20);
DBCursor cursor = collection.find(query).hint(new BasicDBObject(index , 1));

Upvotes: 3

Jeff Tsui
Jeff Tsui

Reputation: 1276

From the docs (http://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hint)

db.subject.find({maths : 30, social :10,english : 20}).hint({maths : 1}) or any other index

Upvotes: 3

Related Questions