Field projection is not performed in the MongoDB Java Driver find method

Hi,

In the quiz about Basic Reads I mark this as false, but the answer says that this statement is true:

Field projection is not performed in the MongoDB Java Driver find method.

Why not this is not performed in find method?
db.movies.find({cast: "Salma Hayek"}, { title: 1, year: 1 })

Document result =
    moviesCollection
        .find(queryFilter)
        .limit(1)
        .projection(new Document("title", 1).append("year", 1))
        .iterator()
        .tryNext();

For me, it IS performed in find method. Did I understand incorrectly?

“Field projection is not performed in the MongoDB Java Driver find method.” - Its true, because it is performed in the projection method.

The findmethod is executed on moviesCollection (an instance of MongoCollection) and the projection is executed on the return value of find ( FindIterable).
See the MongoDB API documentation for com.mongodb.client.MongoCollection.

NOTE:
This code db.movies.find({cast: "Salma Hayek"}, { title: 1, year: 1 }) is not from MongoDB Java driver.

1 Like

@ Prasad_45636
Thanks!
I had the same question.
It is clear now.

NOTE: …is not from MongoDB Java driver -> helped me.

I also have one more question.
If I will execute the following command in java
Document result = moviesCollection.find(queryFilter).limit(1).projection(new Document(“title”, 1).append(“year”, 1)).iterator().tryNext();

It will return all the fields from the DB (for example cast, directors, rating, year etc) and after that will keep only the title and year.

If so, the data will be received back from mongoDB will increase the traffic without reason.

1 Like