[JAVA-1] all method that take a DBObject, make a version that takes a map Created: 17/Apr/09  Updated: 15/Aug/23  Resolved: 21/Apr/09

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Liam Staskawicz Assignee: Kristina Chodorow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

There are many places in the Mongo Java API that use the DBObject interface as parameter or return value. BasicDBObject is the most commonly used implementation of DBObject, which implements java.util.Map, but since none of the methods accept or return Map, we're forced to make intermediate BasicDBObject wrappers around objects that could otherwise be passed straight in. If these methods were updated to accept/return Maps, usage could be simpler and less onerous on user code.

Below is a list of the spots in the API that could benefit (I think) from being updated:

The DBObject interface could implement Map
BasicDBObject - constructor could accept a Map
DBCursor.toArray() - could return List<Map>
DBCursor.curr(), explain(), next() could all return Map
DBCollection - ensureIndex() and friends could accept Map
DBCollection - find() & findOne() could accept Map
DBCollection - getIndexInfo() could return List<Map>
DBCollection - apply(), insert(), remove(), save(), update() could all accept Map



 Comments   
Comment by Kristina Chodorow (Inactive) [ 21/Apr/09 ]

Much better. Added toMap on DBObject.

Comment by Eliot Horowitz (Inactive) [ 21/Apr/09 ]

Can you add a non-static method on DBObject

public Map toMap()

then anything that implements DBObject has to implement toMap.

maybe put in the docs that its an optional operation

Comment by Kristina Chodorow (Inactive) [ 21/Apr/09 ]

So, I can't have DBObject extend Map, unfortunately, because then BasicDBList doesn't work (it extends ArrayList and implements DBObject, and List and Map are incompatible... good job, Java Collections API!).

Overloading DBCollection methods to take Maps is also problematic, as BasicDBObject extends HashMap, so if you call any of the overloaded methods with a BasicDBObject, Java complains that the method you're calling is ambiguous.

However! I've added a constructor to BasicDBObject that takes a Map, and added a static method toMap() that takes a DBObject and returns a Map. I also added BasicDBObjectBuilder.start(Map m).

Not sure what else I can do, if anyone has any ideas, let me know.

Comment by Liam Staskawicz [ 17/Apr/09 ]

I can live with that - even better if DBObject can extend Map in that case

Comment by Eliot Horowitz (Inactive) [ 17/Apr/09 ]

we don't want the return values to be Map because that limits the implementations.
but we can certainly make versions that accept maps

Generated at Thu Feb 08 08:51:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.