简体   繁体   中英

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

I'm using MongoDB 3.2 and MongoDB Java Driver 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). 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.

My question:
How can I query only specific fields of document in MongoDB Java Driver 3.2?

There is a .projection() method that is chainable to the query result which allows you to specify fields.

Either expressed as a document, with the familiar and well documented BSON syntax:

    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:

    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. 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). 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.

My question:
How can I query only specific fields of document in MongoDB Java Driver 3.2?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM