Mongo 3.6.3 java驱动程序聚合提示 - 返回未定义字段'提示'

时间:2018-03-12 18:02:25

标签: mongodb aggregate

我试图在MongoDB Java Driver 3.6.3中传递一个聚合提示。聚合API允许添加提示,如:

MongoCollection<Document> coll = database.getCollection("myCollection")
ArrayList<BasicDBObject> docList = new ArrayList<BasicDBObject>();
BasicDBObject hint = new BasicDBObject("$hint","reportjob_customerId_1_siiDocumentAttributes.deleted_1");
MongoCursor<Document> cursor = coll.aggregate(docList).hint(hint).allowDiskUse(batchContext.isAllowDiskUse()).iterator();

我已尝试使用$hinthint,但Mongo始终返回Command失败,错误为-1:&#39;无法识别的字段&#39;提示&#39;

如何在聚合调用中正确发送提示?

1 个答案:

答案 0 :(得分:0)

看起来没有支持将索引名称直接传递给提示。所以你已经将索引创建文档传递给你可以通过名称获取的提示,并使用key来获取索引文档。

MongoCollection<Document> coll = database.getCollection("myCollection");
Bson index = coll.listIndexes().into(new ArrayList<>()).stream().filter(item -> item.getString("name").equals("reportjob_customerId_1_siiDocumentAttributes.deleted_1")).findFirst().get().getString("key");
List<BasicDBObject> docList = new ArrayList<BasicDBObject>();
MongoCursor<Document> cursor = coll.aggregate(docList).hint(index).allowDiskUse(batchContext.isAllowDiskUse()).iterator();

旧版驱动程序支持索引名称和文档。

MongoClient client =  new MongoClient();
DB database = client.getDB("myDatabase");
DBCollection coll = database.getCollection("myCollection");
List<BasicDBObject> docList = new ArrayList<BasicDBObject>();
AggregationOptions options = AggregationOptions.builder().allowDiskUse(batchContext.isAllowDiskUse()).build();
DBCursor dbCursor = ((DBCursor) coll.aggregate(docList, options)).hint("reportjob_customerId_1_siiDocumentAttributes.deleted_1");

您可以创建一个jira,以便在新驱动程序here中传递索引名称