retrieving data with mongodb java driver 3.4 using find()-method with projection

steady_progress picture steady_progress · Jul 4, 2017 · Viewed 7.5k times · Source

I am using mongodb java driver 3.4.

In the mongodb database documents are saved according to the following structure:

{
    "_id" : ObjectId("595a9fc4fe3f36402b7edf0e"),
    "id" : "123",
    "priceInfo" : [
        {object1: value1}, {object2: value2}, {object3: value3}
    ]
}

In order to retrieve the "priceInfo"-Array of a Document with a specific id, I wrote the following code:

collection.find(eq("id", id)).first().projection(fields(include("priceInfo"), excludeId()));

I wrote this code according too the documentation, which you can find here:

http://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/model/Projections.html

The problem is that my IDE won't accept this code.

It's giving me the following error indication:

enter image description here

I have no clue why this code doesn't work. At first the IDE suggested including several classes - which I did. But after that I still got an error indication, namely the one you see above.

What's wrong with the code? How can I retrieve the priceInfo array of a Document with ID id?

********************************UPDATE**********************************

As per request, here's the whole class:

package DatabaseAccess;

import Models.GasStation;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import com.mongodb.client.model.Updates;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import org.bson.Document;


public class databaseAccess {

    private final String DB_HOST = "localhost"; 
    private final int DB_PORT = 27017;
    private final String DB_NAME = "db1"; 
    private final String DB_COLLECTION = "prices"; 
    private final MongoClient mongoClient;
    private final MongoDatabase database;
    private final MongoCollection<Document> collection; 

    public databaseAccess(){
        mongoClient = new MongoClient(DB_HOST, DB_PORT);
        database = mongoClient.getDatabase(DB_NAME);
        collection = database.getCollection(DB_COLLECTION);
    }


    public String readFromDB(String id){
        collection.find(eq("id", id)).first().projection(fields(include("priceInfo"), excludeId()));
        return null;     
    }

}

Answer

J-Alex picture J-Alex · Jul 4, 2017

You're operating on chain of calls in your method. Let's analyze each element in chain:

MongoCollection:

FindIterable< TDocument> find() - Finds all documents in the collection.

Return type is FindIterable<TDocument> and you calling the next method in chain on it:

FindIterable< TDocument>

Methods inherited from interface com.mongodb.async.client.MongoIterable:

batchCursor, first, forEach, into, map

Okay, we are going to MongoIterable:

MongoIterable< TResult>:

void first(SingleResultCallback callback) - Helper to return the first item in the iterator or null.

That means first(...) is returned nothing. You're calling projection(...) from nothing, of course this is not applicable, so the compiler marks this as an error.

For calling projection(Bson projection) you shoud have FindIterable<T> instance. MongoCollection.find() can provide you with this instance:

collection.find(eq("id", id)).projection(fields(include("priceInfo"), excludeId()));