Java中使用3.0驱动程序的可裁剪光标示例?

Java中使用3.0驱动程序的可裁剪光标示例?,java,mongodb,Java,Mongodb,有人能用Java提供一个完整的可裁剪光标示例吗?我使用的是3.0驱动程序,所有示例都是2.x。我的类路径中只有mongo-java-driver-3.0.0.jar。我想在所有文档插入到我的集合中时获取它们 //this does not work... MongoCollection<BasicDBObject> col = database.getCollection(colName, BasicDBObject.class); DBCursor cur = col.find()

有人能用Java提供一个完整的可裁剪光标示例吗?我使用的是3.0驱动程序,所有示例都是2.x。我的类路径中只有mongo-java-driver-3.0.0.jar。我想在所有文档插入到我的集合中时获取它们

//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);

//this does work but only returns the messageNumber field. I need the doc.
  MongoCursor<Long> c = database.getCollection(colName).distinct("messageNumber", Long.class).iterator();
//这不起作用。。。
MongoCollection col=database.getCollection(colName,BasicDBObject.class);
DBCursor cur=col.find().sort(新的BasicDBObject(“$natural”,1))
.addOption(Bytes.QUERYOPTION_TAILABLE)
.addOption(Bytes.QUERYOPTION\u数据);
//这不起作用。。。
BasicDBObjectBuilder=BasicDBObjectBuilder.start();
添加(“消息类型”、“状态请求”);
DBObject searchQuery=builder.get();
DBObject sortBy=BasicDBObjectBuilder.start(“$natural”,1.get();
BasicDBObjectBuilder builderForFields=BasicDBObjectBuilder.start();
DBObject fields=builderForFields.get();
DBCursor=newdbcursor(col,searchQuery,fields,ReadPreference.primary());
cursor.sort(sortBy);
cursor.addOption(Bytes.QUERYOPTION\u数据);
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
//这确实有效,但只返回messageNumber字段。我需要医生。
MongoCursor c=database.getCollection(colName).distinct(“messageNumber”,Long.class).iterator();
我看到MongoCursor接口是在3.0中添加的。这是干什么用的?它是否取代了DBCursor


非常感谢

在最后一行中,将
.distinct(“messageNumber”,Long.class)
替换为
.find()

仅返回您请求的一个字段的唯一值


返回集合中的所有文档及其所有字段。

对参与方来说有点晚,但如果您仍然需要帮助:

find(query).projection(fields).cursorType(CursorType.TailableAwait).iterator();
该代码适用于
MongoCollection

CursorType是一个枚举,它具有以下值:

Tailable
TailableAwait
与旧的DBCursor addOption字节类型相对应:

Bytes.QUERYOPTION_TAILABLE
Bytes.QUERYOPTION_AWAITDATA

我希望这会有所帮助。

这是您可能正在寻找的-MongoDB 3.0中的事件流。*使用新的api,即3.0.2

Document query = new Document(); \\here use { indexedField: { $gt: <lastvalue> } index is not used(except for auto deleting documents) but created in capped collection
Document projection = new Document();
MongoCursor<Document> cursor= mongocollection.find(query).projection(projection).cursorType(CursorType.TailableAwait).iterator();  //add .noCursorTimeout(true) here to avoid timeout if you are working on big data

while (cursor.hasNext()){
    Document doc = cursor.next();
    //do what you want with doc
}
单据查询=新建单据()\\此处使用{indexedField:{$gt:}索引未使用(自动删除文档除外),而是在capped集合中创建的
文档投影=新文档();
MongoCursor=mongocollection.find(query).projection(projection).cursorType(cursorType.TailableAwait).iterator();//此处添加.noCursorTimeout(true),以避免处理大数据时超时
while(cursor.hasNext()){
Document doc=cursor.next();
//你想怎么处置博士
}

这样mongo cursor将检查capped collection中的新条目

这似乎不会使其可裁剪。在v3中如何完成?在DBCursor出现之前。addOption(…)光标是否有超时?@user320550有默认超时。为避免超时,将其添加到光标-.noCursorTimeout(true)现在MongoCursor=mongocollection.find(query).projection(projection).noCursorTimeout(true).cursorType(cursorType.TailableAwait).iterator();谢谢。您是如何实际实现的?您是否有一个单独的线程继续运行此代码以查找MongoOplog的任何新条目?