在Java中的mongo聚合查询中使用“提示”的语法

时间:2018-09-19 18:07:46

标签: java mongodb mongodb-java

HI可以帮助我如何在mongo聚合查询中使用“提示”,现在我正在使用以下代码查询结果。

AggregationOptions options = AggregationOptions.builder().allowDiskUse(allowDiskUse).build();
        DBCollection dbCollection = db.getPrimativeCollection(colName);           
        Cursor cursor = dbCollection.aggregate(aggregateList, options,
                Config.getInstance().getReadPreferenceEntity(colName));

对于查找查询,我可以像下面这样使用

DBCursor cursor = collection.find(query, projection).hint(hintName).comment(ThreadLocalAuthInfo.getMongoQueryComment());

嗨,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

Database db = DatabaseGroup.getInstance().getDatabase(colName);
        String hint = "dataCenter_1_ownerAccountId_1__id_1";
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("aggregate", colName);
        map.put("pipeline", aggregateList);
        map.put("allowDiskUse", true);
        map.put("cursor", new BasicDBObject());
        if (hint != null) {
            map.put("hint", hint);
        }
        BasicDBObject query = new BasicDBObject(map);
        BasicDBObject resultDoc = db.databaseForSearch.runCommand(query,
                Config.getInstance().getReadPreferenceEntity(colName), BasicDBObject.class);
        BasicDBObject resultCursor = (BasicDBObject) resultDoc.get("cursor");
        List<BasicDBObject> list = (List<BasicDBObject>) resultCursor.get("firstBatch");
        for (BasicDBObject each : list) {
            lstDBObjects.add(each);
        }