Tailable Cursor example in Java using 3.0 driver?

528 views
Skip to first unread message

Andrew M

unread,
May 3, 2015, 7:47:21 AM5/3/15
to mongod...@googlegroups.com
Can someone please provide a complete tailable cursor example in java?  I am using the 3.0 driver and all examples appear to be 2.x.  I have only mongo-java-driver-3.0.0.jar in my classpath.

//this does not work...
MongoCollection<BasicDBObject> col = database.getCollection(colName, BasicDBObject.class);
DBCursor cur = col.find().sort(new BasicDBObject("$natural", 1))
        .addOption(Bytes.QUERYOPTION_TAILABLE)
.addOption(Bytes.QUERYOPTION_AWAITDATA);


// And this does not work...
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
builder.add("messageType","STATUS_REQUEST");
DBObject searchQuery = builder.get();
DBObject sortBy = BasicDBObjectBuilder.start("$natural", 1).get();
BasicDBObjectBuilder builderForFields = BasicDBObjectBuilder.start();
DBObject fields = builderForFields.get();
DBCursor cursor = new DBCursor(col, searchQuery, fields, ReadPreference.primary() );
cursor.sort(sortBy);
cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);


I see that the MongoCursor interface was added in 3.0. What is that for and does it replace DBCursor?

Thanks a lot


Ross Lawley

unread,
May 7, 2015, 4:55:14 AM5/7/15
to mongod...@googlegroups.com
Hi Andrew,

I answered in your other post about how to make the find() method return a tailable cursor but I'm expanding on that here to answer your wider questions.

In 3.0 we made many changes and improvements to the API - you can read more about the reasons in the Introducing the 3.0 Java Driver blog post.  In 3.0 we addressed some of the fundamental issues with the existing DB and DBCollection classes by creating new MongoDatabase and MongoCollection classes.

We didn't abandon the old DB and DBCollection classes but rather deprecated them so to inform users they would eventually be removed.  So old DBCollection code will still work but the preferred way is to use the new  MongoCollection class.

MongoCollection.find() now supports a fluent interface, so rather than the implicit addOption you now explicitly call the fluent cursorType method like so:

MongoCollection<Document> collection = database.getCollection(colName);
MongoCursor<Document> cursor = collection.find()
                                         .sort(new Document("$natural",1)
                                         .cursorType(CursorType.TailableAwait);


In the above example I'm using the Document class rather than DBObject class as they are the new preferred Document class.  There are also some new static helpers to improve the creation of Document like objects see Filters, Projections and Sorts.  More helpers are being added in the forthcoming 3.1 release.

I hope that answers your questions!

Ross

Andrew M

unread,
May 7, 2015, 10:55:39 PM5/7/15
to mongod...@googlegroups.com

MongoCollection<Document> collection = database.getCollection(colName);
MongoCursor<Document> cursor = collection.find()
                                         .sort(new Document("$natural",1)
                                         .cursorType(CursorType.TailableAwait);

Thanks a lot for the help.  In case anyone else needs to know this you were just missing the iterator call on the end:

MongoCollection<Document> collection = database.getCollection(colName);
MongoCursor<Document> c = collection
.find()
.cursorType(CursorType.TailableAwait)
.sort(new Document("$natural", 1))
        .iterator();

 
Reply all
Reply to author
Forward
0 new messages