What is this FindIterable in the 3.0 API?

2,230 views
Skip to first unread message

Brenton Poke

unread,
Mar 23, 2015, 9:59:47 AM3/23/15
to mongod...@googlegroups.com
What on earth is the FindIterable that is returned when trying to get a collection?
This is the code I'm trying to get to work with the new API:

DBCursor cursor = colls.find(storeInfo);

This statement used to return a DBCursor, but now it returns some mystery interface with no documentation.
I also noticed that I had to make 'colls' into a MongoCollection rather than a DBCollection in order to store the results to begin with.
What is all of this and why was it changed?

On Twitter here.

Justin Lee

unread,
Mar 23, 2015, 10:05:31 AM3/23/15
to mongod...@googlegroups.com
MongoCollection is a new type in the 3.0 driver and returns new types from its methods.  If you want a DBCursor you should use mongoClient.getDB("somedb").getCollection("somecollection").  At some point, you got colls via getDatabase(), I'm guessing.

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "10gen.com", "antwerkz.com" ],
  location : "New York, NY" }

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/38b08316-ed9d-43f7-bd92-deb76b30fb11%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ross Lawley

unread,
Mar 23, 2015, 12:26:23 PM3/23/15
to mongod...@googlegroups.com
Hi Brenton,

First, thanks for posting to the list and trying out the new 3.0 Java driver release candidate.  As you've noticed we've made many changes and improvements to the driver but we are still working on the release.   We are focusing on writing the new documentation site and improving the API documentation, so please bare with us whilst we catch up and improve those areas. 

The old DB and DBCollection API is still available via: MongoClient().getDB("dbName"), however in 3.0 we are introducing MongoDatabase and MongoCollection with new style API's.  

So onto your question: "What is a FindIterable"

In reality it is similar to the DBCursor of old and like a DBCursor its an implementation of the Iterable interface.  So you can treat it similarly for looping query results.  The key differentiator is that the FindIterable is a higher abstraction than a DBCursor, its a chainable interface that allows users to build up queries in a fluent way by changing filters and options.  At its highest level FindIterable is an implementation of the MongoIterable interface which represents the results of some MongoDB operation be it a query or a command.

As MongoDB has matured, new features such as the aggregation framework and new commands such as ListCollections have been introduced.  The type of data some of these operations return has also changed over time, previously it might have returned a list of documents where as now it returns a cursor of results.  In 3.0 we've abstracted this, so that anything returning multiple items now returns an instance of  MongoIterable - underneath it might be using a cursor or it might be a List and the common ground between both is the Iterable interface, hence the choice of the iterable name.  In the core of the driver we handle the different MongoDB versions and produce results that adhere to this single interface.  This way we can keep the top level api stable and prevent unnecessary change to users.  

The final idea behind having fluent style interfaces is that this design pattern helps future proof the API without the continual need for overloads that is visible in the DBCollection API.  Any new options for a query or an aggregation, can be added to the relevant fluent iterable and this keeps the access to the operation clean and simple in MongoCollection.  I think I last counted that the old DBCollection API has over 105 methods including many overloads and the new MongoCollection API provides more functionality with less than 45.

I know as team we still have work to do on the documentation and it is our current priority as the API has stabilised and we near the 3.0 final release.  Apologies for the frustration using the new driver has caused and please do continue to try out the new features and we appreciate any feedback good or bad.

All the best,

Ross

Brenton Poke

unread,
Mar 25, 2015, 7:48:10 AM3/25/15
to mongod...@googlegroups.com
But, if I wanted to update to the 3.0 API, how would I go about getting this for-loop running again:

for(final DBObject s : cursor){}

I can't get a DBObject, and I need one to get this statement running again.

Jeff Yemin

unread,
Mar 25, 2015, 7:59:18 AM3/25/15
to mongod...@googlegroups.com
It will look something like this:

MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("test");
MongoCollection<DBObject> coll = database.getCollection("test"DBObject.class);
coll.insertOne(new BasicDBObject("x", 1));
for (DBObject doc : coll.find()) {
    System.out.println(doc);
}

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Brenton Poke

unread,
Mar 26, 2015, 8:20:18 AM3/26/15
to mongod...@googlegroups.com
Ok, think the new code is working, but I can't produce an executable jar for some reason. I just get this:

Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson

Jeff Yemin

unread,
Mar 26, 2015, 9:34:36 AM3/26/15
to mongod...@googlegroups.com
Can you post a pom.xml or equivalent somewhere so we can take a look?

Regards,
Jeff

Brenton Poke

unread,
Mar 27, 2015, 9:05:04 AM3/27/15
to mongod...@googlegroups.com
I uploaded the pom file i'm currently using.
pom.xml

Jeff Yemin

unread,
Mar 27, 2015, 9:47:09 AM3/27/15
to mongod...@googlegroups.com
I'm not sure what the problem is.  That class is definitely in the 3.0.0-rc1 jar file though:

~/m2/repository/org/mongodb/mongo-java-driver/3.0.0-rc1$ jar tvf mongo-java-driver-3.0.0-rc1.jar | grep Bson.class
   378 Tue Mar 24 18:10:20 EDT 2015 org/bson/conversions/Bson.class



Brenton Poke

unread,
Mar 27, 2015, 9:51:12 AM3/27/15
to mongod...@googlegroups.com

Well, I am using intelliJ and am new to it, if that makes a difference.

You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/pcVX84PPwM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Jeff Yemin

unread,
Mar 27, 2015, 9:52:55 AM3/27/15
to mongod...@googlegroups.com
Probably not.  I assume your code runs ok in IntelliJ?  Seems more related to maven.  You might try posting on another forum to get him with that.



DixieFlatline

unread,
Mar 28, 2015, 8:34:51 AM3/28/15
to mongod...@googlegroups.com
Well, I resolved the problem with the jar, and it had to do with
intelliJ's settings for producing the artifact. I had to wipe clean and
re-do the JAR application output option.

If any of you guys happen to know any intelliJ developers, tell them
that they need to get their shit together regarding Maven integration.
NetBeans is kicking their ass on this front.
> +unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages