返回具有指定值但未定义的实体的 ID 列表 entity/POJO

Returning IDs list of entities with specified value without defined entity/POJO

我正在尝试编写一种方法,该方法 returns 为集合中具有特定状态的所有记录提供 ID(作为 List )。
我正在寻找解决方案,但不幸的是我找不到任何正确的方法。
我目前有这样的东西:

List<String> getAllLecturersIdList() {
    MongoCollection<Document> collection.mongoTemplate.getCollection("lecturers");
    MongoCursor<Document> cursor = collection.find().iterator();
    ArrayList<String> listOfIDS = new ArrayList<>();
    while (cursor.hasNext()) {
        listOfIDS.add(cursor.next().getObjectId("_id").toString());
    }
    return listOfIDS;

}

这个方法returns我列出了所有讲师的ID。
讲师实体还有一个“状态”字段,其值如 ACTIVE、LEAVE、FIRED 等。
我希望只返回状态为 ACTIVE 的讲师 ID。
如何在从集合返回时仅具有 ACTIVE 状态的实体,而不清理存储库/服务级别?
提前感谢您的帮助!
重要 - 我不想在应用程序中创建实体结构。
因此,解决方案不能包含 POJO / 实体 Class,这就是问题所在(我不能使用例如 Criteria,因为每个示例都带有已定义的实体)

以下应该有效:

List<String> getAllLecturersIdList() {
    MongoCollection<Document> collection.mongoTemplate.getCollection("lecturers");
    MongoCursor<Document> cursor = collection.find().iterator();
    ArrayList<String> listOfIDS = new ArrayList<>();
    while (cursor.hasNext()) {
        Document document = cursor.next();
        if (document.getString("status") == "ACTIVE") {
          listOfIDS.add(document.getObjectId("_id").toString());
        }
    }
    return listOfIDS;
}

如果没有结果类型映射您的查询结果,您可以回退到低级别 MongoOperations#executeQuery 方法:

List<String> getAllLecturersIdList() {
    Query query = new Query();
    query.fields().include("_id");
    query.addCriteria(Criteria.where("status").is("ACTIVE"));
    ArrayList<String> listOfIDS = new ArrayList<>();
    mongoTemplate.executeQuery(query, "lecturers", document -> listOfIDS.add(document.getObjectId("_id").toString()));
    return listOfIDS;
}