如何从 java 集合中的 java 个对象访问变量?

How to access a variable from java objects in a java collection?

我正在使用 Mongo Java 驱动程序,我正尝试在 collection.find() 函数上使用过滤器。例如,当我有一个 java 对象的键时,其 class 包含某些字段:

Document document = (Document) collection.find(and(
    eq("m_seniority", key.getM_seniority()),
    eq("m_currency",key.getM_currency()),
    eq("m_redCode",key.getM_redCode()),
    eq("m_companyId",key.getM_companyId())
)).first();

我使用上面的命令。但是,当我想批量执行此操作时,系统会向我传递一组键( Collection keys ),我无法访问内部 java 对象的特定变量,如下所示:

List<Document> docs =  (List<Document>) collection.find(and(
    eq("m_seniority", keys.getM_seniority()),
    eq("m_currency",keys.getM_currency()),
    eq("m_redCode",keys.getM_redCode()),
    eq("m_companyId",keys.getM_companyId())
)).into(new ArrayList<Document>());

因为吸气剂不属于集合,而只是对象,所以我不能对集合使用吸气剂。我该怎么做?

要针对所有 Collection keys 创建一个 or 查询:

List<Bson> keyFilters = new ArrayList<>();
// for each key create an 'and' filter on seniority, currency, redcode and companyid
for (Key key : keys) {        
    keyFilters.add(
        Filters.and(eq("m_seniority", key.getM_seniority()),
            Filters.eq("m_currency",key.getM_currency()),
            Filters.eq("m_redCode",key.getM_redCode()),
            Filters.eq("m_companyId",key.getM_companyId())
        )
    );
}

List<Document> docs =  (List<Document>) collection.find(
        // 'or' all of the individual key filters
        Filters.or(keyFilters)
).into(new ArrayList<Document>());