use of com.mongodb.client.model.InsertOneOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeInsertOne.
OperationResult executeInsertOne(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
ClientSession session = getSession(arguments);
BsonDocument document = arguments.getDocument("document").asDocument();
InsertOneOptions options = new InsertOneOptions();
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "session":
case "document":
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> toExpected(session == null ? collection.insertOne(document, options) : collection.insertOne(session, document, options)));
}
use of com.mongodb.client.model.InsertOneOptions in project debezium by debezium.
the class ConnectionIT method shouldCreateMovieDatabase.
@Test
public void shouldCreateMovieDatabase() {
Testing.print("Configuration: " + config);
String dbName = "dbA";
primary.execute("shouldCreateMovieDatabase", mongo -> {
Testing.debug("Getting or creating 'movies' collection");
// Create a database and a collection in that database ...
MongoDatabase db = mongo.getDatabase(dbName);
// Get or create a collection in that database ...
db.getCollection("movies");
Testing.debug("Completed getting 'movies' collection");
});
primary.execute("Add document to movies collection", mongo -> {
Testing.debug("Adding document to 'movies' collection");
// Add a document to that collection ...
MongoDatabase db = mongo.getDatabase(dbName);
MongoCollection<Document> movies = db.getCollection("movies");
InsertOneOptions insertOptions = new InsertOneOptions().bypassDocumentValidation(true);
movies.insertOne(Document.parse("{ \"name\":\"Starter Wars\"}"), insertOptions);
assertThat(db.getCollection("movies").count()).isEqualTo(1);
// Read the collection to make sure we can find our document ...
Bson filter = Filters.eq("name", "Starter Wars");
FindIterable<Document> movieResults = db.getCollection("movies").find(filter);
try (MongoCursor<Document> cursor = movieResults.iterator()) {
assertThat(cursor.tryNext().getString("name")).isEqualTo("Starter Wars");
assertThat(cursor.tryNext()).isNull();
}
Testing.debug("Completed document to 'movies' collection");
});
// Now that we've put at least one document into our collection, verify we can see the database and collection ...
assertThat(primary.databaseNames()).contains("dbA");
assertThat(primary.collections()).contains(new CollectionId(replicaSet.replicaSetName(), dbName, "movies"));
// Read oplog from beginning ...
List<Document> eventQueue = new LinkedList<>();
int minimumEventsExpected = 1;
long maxSeconds = 5;
primary.execute("read oplog from beginning", mongo -> {
Testing.debug("Getting local.oplog.rs");
BsonTimestamp oplogStart = new BsonTimestamp(1, 1);
Bson filter = // start just after our last position
Filters.and(// start just after our last position
Filters.gt("ts", oplogStart), // skip internal movements across shards
Filters.exists("fromMigrate", false));
FindIterable<Document> results = mongo.getDatabase("local").getCollection("oplog.rs").find(filter).sort(new Document("$natural", 1)).oplogReplay(// tells Mongo to not rely on indexes
true).noCursorTimeout(// don't timeout waiting for events
true).cursorType(CursorType.TailableAwait);
Testing.debug("Reading local.oplog.rs");
try (MongoCursor<Document> cursor = results.iterator()) {
Document event = null;
long stopTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(maxSeconds);
while (System.currentTimeMillis() < stopTime && eventQueue.size() < minimumEventsExpected) {
while ((event = cursor.tryNext()) != null) {
eventQueue.add(event);
}
}
assertThat(eventQueue.size()).isGreaterThanOrEqualTo(1);
}
Testing.debug("Completed local.oplog.rs");
});
eventQueue.forEach(event -> {
Testing.print("Found: " + event);
BsonTimestamp position = event.get("ts", BsonTimestamp.class);
assert position != null;
});
}
Aggregations