MongoInsertQuery extends MongoInputQuery { @Override public void performQuery() { if (many) { if (manyOptions != null) { super.collection.insertMany(this.documents, manyOptions); } else { super.collection.insertMany(this.documents); } } else { if (oneOptions != null) { super.collection.insertOne(this.documents.get(0), oneOptions); } else { super.collection.insertOne(this.documents.get(0)); } } } MongoInsertQuery(MongoCollection collection, Document document, InsertOneOptions options);  MongoInsertQuery(MongoCollection collection, List<Document> documents, InsertManyOptions options); @Override void performQuery();  }
@Test void performSingleInsert() { MongoCollection<Document> collection = this.connector.getDatabase().getCollection("insert_query"); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); Document sample = new Document(); sample.put("id", 0); MongoInsertQuery query = new MongoInsertQuery(collection, sample, null); query.performQuery(); assertEquals(1, collection.count()); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); }
@Test void performSingleOptionInsert() { MongoCollection<Document> collection = this.connector.getDatabase().getCollection("insert_query"); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); Document sample = new Document(); sample.put("id", 0); MongoInsertQuery query = new MongoInsertQuery(collection, sample, new InsertOneOptions()); query.performQuery(); assertEquals(1, collection.count()); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); }
@Test void performMultiInsert() { MongoCollection<Document> collection = this.connector.getDatabase().getCollection("insert_query"); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); List<Document> samples = new ArrayList<>(); for (int i = 0; i < 100; i++) { Document sample = new Document(); sample.put("id", i); samples.add(sample); } MongoInsertQuery query = new MongoInsertQuery(collection, samples, null); query.performQuery(); assertEquals(100, collection.count()); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); }
@Test void performMultiOptionInsert() { MongoCollection<Document> collection = this.connector.getDatabase().getCollection("insert_query"); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); List<Document> samples = new ArrayList<>(); for (int i = 0; i < 100; i++) { Document sample = new Document(); sample.put("id", i); samples.add(sample); } MongoInsertQuery query = new MongoInsertQuery(collection, samples, new InsertManyOptions()); query.performQuery(); assertEquals(100, collection.count()); collection.deleteMany(new BsonDocument()); assertEquals(0, collection.count()); }