3

java api を使用する mongodb バージョン 3 では、クエリ中にヒントを与える方法 クエリ結果は、MongoCursor を持つ FindIterable です。 特定のインデックスを使用するためのヒントをどのように与えるべきですか?

古いバージョンでは、ヒント用の API を持つ DBCursor があります。

4

2 に答える 2

2

修飾子を使用する必要があります:

BasicDBObject index = new BasicDBObject("num", 1);
BasicDBObject hint = new BasicDBObject("$hint", index);
FindIterable<Document> iterable = collection.find().modifiers(hint);
于 2016-08-05T04:35:48.623 に答える
1

$hint を指定するには、FindIterable.modifiers メソッドを使用する必要があります。

private static void testHint(MongoDatabase db) {
    MongoCollection<Document> col = db.getCollection("Stores");
    FindIterable<Document> iterable = col.find(new Document("store","storenumbervalue"));
    iterable.modifiers(new Document("$hint","index_name"));
    MongoCursor curs =  iterable.iterator();
    while(curs.hasNext()){
        System.out.println(curs.next());
    }        
}

index_name - mongo の実際のインデックス名。

于 2016-07-07T15:43:14.100 に答える