[JAVA-3758] Improve the support for mapped find iterables Created: 08/Jun/20  Updated: 04/Aug/20  Resolved: 04/Aug/20

Status: Closed
Project: Java Driver
Component/s: API, Query Operations
Affects Version/s: 4.0.4
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Moritz Hennen Assignee: Jeffrey Yemin
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Currently, when calling FindIterable#map, the API will only return a MongoIterable, limiting the options to further adjust the cursor after mapping it like in the following example:

MongoClient client = MongoClients.create();
MongoDatabase database = client.getDatabase("abc");
MongoCollection<Document> collection = database.getCollection("collection");
FindIterable<Document> iterable = collection.find();
MongoIterable<String> mappedIterable = iterable.map(Document::toJson);

The resulting MongoIterable now does not provide useful FindIterable methods like FindIterable#limit, FindIterable#sort and so on. I have opened up a Pull-Request for this feature on GitHub: https://github.com/mongodb/mongo-java-driver/pull/551



 Comments   
Comment by Jeffrey Yemin [ 04/Aug/20 ]

Hi mail@moritz-hennen.de,

We've decided not to pursue this change until we get signal that it will be useful to a wide group of users. We can reconsider if there are more requests for it.

Closing as Won't Fix.

Comment by Moritz Hennen [ 16/Jul/20 ]

Hi @Jeffrey Yemin,

my map function is

MongoStatsReader::new (Document -> MongoStatsReader)

(https://github.com/KitPvPDE/stats-api/blob/master/src/main/java/net/kitpvp/pluginapi/modules/stats/mongo/MongoStatsReader.java),

and the stats reader class is defined here: https://github.com/KitPvPDE/stats-api/blob/ae45ae5cf757914315687741e41511e01f667d8f/src/main/java/net/kitpvp/pluginapi/modules/stats/StatsReader.java#L15

Comment by Jeffrey Yemin [ 16/Jul/20 ]

Hi mail@moritz-hennen.de,

Can you share your map function and the StatsReader class? It's possible that the built-in PojoCodecProvider will just work out of the box. Note that these map functions were added before we had native support for decoding to POJOs.

Comment by Moritz Hennen [ 16/Jul/20 ]

Hi @Jeffrey Yemin

I developed a simple stats API for saving and loading stats per document, and therefor added a class for find operations. It implements java.lang.Iterable<StatsReader> and the iterator() Method specified by the java.lang.Iterable interface. Currently, because the map method is expected to be used as the last method in the chain, I have to map the existing FindIterable<Document> to the required MongoIterable<StatsReader> just as any method like FindQuery#find, FindQuery#findOne or FindQuery#iterator gets called. ((https://github.com/KitPvPDE/stats-api/blob/ae45ae5cf757914315687741e41511e01f667d8f/src/main/java/net/kitpvp/pluginapi/modules/stats/mongo/queries/FindQuery.java#L16)
) Wouldnt it be easier to already have a FindIterable<StatsReader> (or such similar) to directly proxy those methods to?

I thought about using MongoDatabase#getCollection(name, StatsReader.class), but there I cannot specify a map function as the class parameter gets used for decoding by codec

Comment by Jeffrey Yemin [ 11/Jul/20 ]

Hi mail@moritz-hennen.de

This was done intentionally, with the assumption that applications could apply the map as the last method in the chain, e.g.

collection.find().limit(4).sort(Sorts.ascending("x").map(Document::toJson)

Can you give a scenario or use case showing why that's not practical for you?

Also, if we are going to do this for FindIterable, we will have to do it for all other sub-types of MongoIterable, for sync, reactive streams, and Scala APIs.

Comment by Moritz Hennen [ 11/Jun/20 ]

Update:

Removed the old pull request and submitted a new one, since the old one didnt pass the build test. Submitted a new one, which compiles cleanly.

https://github.com/mongodb/mongo-java-driver/pull/553

Generated at Thu Feb 08 09:00:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.