简体   繁体   English

如何使用 Java 仅查询 MongoDB 中的特定字段?

[英]How to query only the specific fields in MongoDB with Java?

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2.我正在使用 MongoDB 3.2 和 MongoDB Java 驱动程序 3.2。 In order to query document I use the following code:为了查询文档,我使用以下代码:

Document query = new Document("fetchStatus", new Document("$lte", fetchStatusParam));
ArrayList<Document> unfetchedEvents = dbC_Events.find(query).into(new ArrayList<Document>());

This query works but the problem is that in this case all fields of the document are retrieved (analog of select * in SQL).此查询有效,但问题是在这种情况下检索文档的所有字段(类似于 SQL 中的select * )。 In order to optimize query performance, I want to specify fields I really need and fetch only them.为了优化查询性能,我想指定我真正需要的字段并只获取它们。

I found couple of examples, such as:我找到了几个例子,例如:

BasicDBObject query = new BasicDBObject();
BasicDBObject fields = new BasicDBObject("Name", 1);
coll.find(query, fields);

but all of them are designed for outdated version of MongoDB Java Driver, eg 2.4, while I'm using 3.2.但它们都是为过时版本的 MongoDB Java 驱动程序设计的,例如 2.4,而我使用的是 3.2。

My question:我的问题:
How can I query only specific fields of document in MongoDB Java Driver 3.2?如何在 MongoDB Java Driver 3.2 中仅查询文档的特定字段?

There is a .projection() method that is chainable to the query result which allows you to specify fields.有一个.projection()方法可以链接到查询结果,它允许您指定字段。

Either expressed as a document, with the familiar and well documented BSON syntax:要么表示为文档,使用熟悉且记录良好的 BSON 语法:

    ArrayList<Document> unfecthedEvents = collection.find(
        new Document("fetchStatus", new Document("$lte", fetchStatusParam))
    ).projection(
        new Document("Name",1)
    ).into(new ArrayList<Document>());

Or as a fields property builder which really just translates to exactly the same BSON:或者作为一个fields属性构建器,它实际上只是转换为完全相同的 BSON:

    ArrayList<Document> unfecthedEvents = collection.find(
        new Document("fetchStatus", new Document("$lte", fetchStatusParam))
    ).projection(
            fields(include("Name"))
    ).into(new ArrayList<Document>());

It worked for me as well:它也对我有用:

BasicDBObject whereQuery = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();     

// the conditions in where query
whereQuery.put("dzeeClient", dzeeClient);
whereQuery.put("recommendationRunType", planView);
whereQuery.put("recommendedPlans.enrolled",employeeViewed);

// the fields to be returned from the query-only loginId, and remove _id
fields.put("loginId", 1); 
fields.put("_id", 0);

FindIterable<Document> cursor = collection.find(whereQuery)
                    .projection(fields).sort(new BasicDBObject("timeStamp",-1)).limit(1);

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2.我正在使用MongoDB 3.2和MongoDB Java驱动程序3.2。 In order to query document I use the following code:为了查询文档,我使用以下代码:

Document query = new Document("fetchStatus", new Document("$lte", fetchStatusParam));
ArrayList<Document> unfetchedEvents = dbC_Events.find(query).into(new ArrayList<Document>());

This query works but the problem is that in this case all fields of the document are retrieved (analog of select * in SQL).该查询有效,但是问题在于,在这种情况下,将检索文档的所有字段(SQL中select *模拟)。 In order to optimize query performance, I want to specify fields I really need and fetch only them.为了优化查询性能,我想指定我真正需要的字段并仅获取它们。

I found couple of examples, such as:我发现了几个示例,例如:

BasicDBObject query = new BasicDBObject();
BasicDBObject fields = new BasicDBObject("Name", 1);
coll.find(query, fields);

but all of them are designed for outdated version of MongoDB Java Driver, eg 2.4, while I'm using 3.2.但是它们都是针对MongoDB Java驱动程序的过时版本(例如2.4)而设计的,而我使用的是3.2。

My question:我的问题:
How can I query only specific fields of document in MongoDB Java Driver 3.2?如何仅查询MongoDB Java Driver 3.2中的文档的特定字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只返回Spring数据MongoDB中查询的特定字段? - How to return only specific fields for a query in Spring Data MongoDB? MongoDB:如何使用 Java(使用或不使用 Spring)将带有别名的一个查询的特定字段添加到另一个查询的结果? - MongoDB: how to add the specific fields from one query with alias to result of another query using Java (with or without Spring)? 是否有 mongodb 查询合并特定 mongodb 文档中的字段 - Is there a mongodb query to merge fields in a specific mongodb document 如何仅从Mongodb文档中获取特定字段? - How to get only specific fields from Mongodb document? 如何从Java中的MongoDB Atlas获取特定字段? - how to get specific fields from MongoDB Atlas in java? 如何使用Java在mongodb中重复地仅添加嵌入式字段? - How to add only embedded fields repetitively in mongodb using Java? Java Spring:如何仅验证实体中的特定字段 - Java Spring: how to validate only specific fields from my entity 如何获取仅包含Java中特定字段的对象列表? - How to get a list of objects contained only specific fields in Java? 如何使用Java查询具有特定所需字段的Elasticsearch - How to query elasticsearch with specific needed fields using java 如何在查询 Java Spring Mongo Repository 中返回特定字段的列表? - How to return list of specific fields in query Java Spring Mongo Repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM