use of com.mongodb.client.model.Field in project mongo-java-driver by mongodb.
the class DocumentationSamples method testWatch.
@Test
public void testWatch() throws InterruptedException {
assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(3, 6));
final MongoCollection<Document> inventory = collection;
final AtomicBoolean stop = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (!stop.get()) {
collection.insertMany(asList(new Document("username", "alice"), new Document()));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// ignore
}
collection.deleteOne(new Document("username", "alice"));
}
}
});
thread.start();
// Start Changestream Example 1
MongoCursor<ChangeStreamDocument<Document>> cursor = inventory.watch().iterator();
ChangeStreamDocument<Document> next = cursor.next();
// End Changestream Example 1
cursor.close();
// Start Changestream Example 2
cursor = inventory.watch().fullDocument(FullDocument.UPDATE_LOOKUP).iterator();
next = cursor.next();
// End Changestream Example 2
cursor.close();
// Start Changestream Example 3
BsonDocument resumeToken = next.getResumeToken();
cursor = inventory.watch().resumeAfter(resumeToken).iterator();
next = cursor.next();
// End Changestream Example 3
cursor.close();
// Start Changestream Example 4
List<Bson> pipeline = asList(match(Document.parse("{'fullDocument.username': 'alice'}")), addFields(new Field<String>("newField", "this is an added field!")));
cursor = inventory.watch(pipeline).iterator();
next = cursor.next();
// End Changestream Example 4
cursor.close();
stop.set(true);
thread.join();
}
Aggregations