Java Code Examples for com.mongodb.client.MongoCollection#insertMany()

The following examples show how to use com.mongodb.client.MongoCollection#insertMany() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: morphia   File: DatastoreImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <T> void insert(final List<T> entities, final InsertManyOptions options) {
    if (!entities.isEmpty()) {
        Class<?> type = entities.get(0).getClass();
        MappedClass mappedClass = mapper.getMappedClass(type);
        final MongoCollection collection = mapper.getCollection(type);
        MappedField versionField = mappedClass.getVersionField();
        if (versionField != null) {
            for (final T entity : entities) {
                setInitialVersion(versionField, entity);
            }
        }

        MongoCollection mongoCollection = options.prepare(collection);
        if (options.clientSession() == null) {
            mongoCollection.insertMany(entities, options.getOptions());
        } else {
            mongoCollection.insertMany(options.clientSession(), entities, options.getOptions());
        }
    }
}
 
Example 2
Source Project: morphia   File: ArrayExpressionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReverseArray() {
    MongoCollection<Document> users = getDatabase().getCollection("users");
    users.drop();
    users.insertMany(
        List.of(parse("{ '_id' : 1, 'name' : 'dave123', 'favorites' : [ 'chocolate', 'cake', 'butter', 'apples' ] }"),
            parse("{ '_id' : 2, 'name' : 'li', 'favorites' : [ 'apples', 'pudding', 'pie' ] }"),
            parse("{ '_id' : 3, 'name' : 'ahn', 'favorites' : [ ] }"),
            parse("{ '_id' : 4, 'name' : 'ty' }")));

    List<Document> actual = getDs().aggregate("users")
                                   .project(Projection.of()
                                                      .include("name")
                                                      .include("reverseFavorites", reverseArray(field("favorites"))))
                                   .execute(Document.class)
                                   .toList();

    List<Document> expected = List.of(
        parse("{ '_id' : 1, 'name' : 'dave123', 'reverseFavorites' : [ 'apples', 'butter', 'cake', 'chocolate' ] }"),
        parse("{ '_id' : 2, 'name' : 'li', 'reverseFavorites' : [ 'pie', 'pudding', 'apples' ] }"),
        parse("{ '_id' : 3, 'name' : 'ahn', 'reverseFavorites' : [ ] }"),
        parse("{ '_id' : 4, 'name' : 'ty', 'reverseFavorites' : null }"));

    assertDocumentEquals(expected, actual);
}
 
Example 3
Source Project: heimdall   File: DBMongoImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <T> void insertMany(MongoCollection<Document> collection, List<T> objects) {

	try {

		List<Document> ts = new ArrayList<>();
		for (T t : objects) {

			ts.add(Document.parse(json.parse(t)));
		}
		collection.insertMany(ts);
		this.datastore().save(ts);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 4
Source Project: heimdall   File: DBMockImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <T> void insertMany(MongoCollection<Document> collection, List<T> objects) {

    try {

        List<Document> ts = new ArrayList<>();
        for (T t : objects) {

            ts.add(Document.parse(json.parse(t)));
        }
        collection.insertMany(ts);
    } catch (Exception ignored) {
    } finally {

        createMongoClient().close();
    }
}
 
Example 5
Source Project: morphia   File: FiltersTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBitsAllClear() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.drop();

    collection.insertMany(asList(
        new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"),
        new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"),
        new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));

    FindOptions options = new FindOptions().logQuery();

    List<User> found = getDs().find(User.class)
                              .disableValidation()
                              .filter(bitsAllClear("a", 35)).iterator(options)
                              .toList();

    Assert.assertEquals(getDs().getLoggedQuery(options), 2, found.size());

    found = getDs().find(User.class)
                   .disableValidation()
                   .filter(bitsAllClear("a", new int[]{1, 5})).iterator(options)
                   .toList();

    Assert.assertEquals(getDs().getLoggedQuery(options), 2, found.size());
}
 
Example 6
Source Project: baleen   File: MongoGazetteerTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws ResourceInitializationException {
  sfr = new SharedFongoResource();
  MongoDatabase db = sfr.getDB();

  MongoCollection<Document> coll = db.getCollection("gazetteer");
  coll.insertMany(
      Arrays.asList(
          new Document(VALUE, EN_HELLO2),
          new Document(VALUE, Arrays.asList("hi", EN_HELLO, "heya")),
          new Document(VALUE, Arrays.asList("konnichiwa", JP_HELLO)).append(LANGUAGE, "jp"),
          new Document(VALUE, Arrays.asList(DE_HELLO))
              .append(LANGUAGE, "de")
              .append(TRANSLATION, "good day"),
          new Document(VALUE, Arrays.asList("hej")).append(LANGUAGE, "se")));
}
 
Example 7
Source Project: nifi   File: GetMongoIT.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    runner = TestRunners.newTestRunner(GetMongo.class);
    runner.setVariable("uri", MONGO_URI);
    runner.setVariable("db", DB_NAME);
    runner.setVariable("collection", COLLECTION_NAME);
    runner.setProperty(AbstractMongoProcessor.URI, "${uri}");
    runner.setProperty(AbstractMongoProcessor.DATABASE_NAME, "${db}");
    runner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, "${collection}");
    runner.setProperty(GetMongo.USE_PRETTY_PRINTING, GetMongo.YES_PP);
    runner.setIncomingConnection(false);

    mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));

    MongoCollection<Document> collection = mongoClient.getDatabase(DB_NAME).getCollection(COLLECTION_NAME);
    collection.insertMany(DOCUMENTS);
}
 
Example 8
@BeforeEach
public void setUp() {
    mongoClient = mongoDBService.getClient();

    MongoDatabase database = mongoClient.getDatabase("testDatabase");

    /*
     The consume operation needs taliable cursors which require capped
     collections
     */
    CreateCollectionOptions options = new CreateCollectionOptions();
    options.capped(true);
    options.sizeInBytes(1024 * 1024);

    database.createCollection("testCollection", options);

    MongoCollection<Document> collection = database.getCollection("testCollection");

    List<Document> documents = new ArrayList<>(expect);
    for (int i = 0; i < expect; i++) {
        Document doc = new Document();

        doc.append("name", "test");
        doc.append("value", "value " + i);

        documents.add(doc);
    }

    collection.insertMany(documents);
}
 
Example 9
Source Project: morphia   File: FiltersTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBitsAllSet() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.drop();

    collection.insertMany(asList(
        new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"),
        new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"),
        new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));

    FindOptions options = new FindOptions().logQuery();

    List<User> found = getDs().find(User.class)
                              .disableValidation()
                              .filter(bitsAllSet("a", 50)).iterator(options)
                              .toList();

    Assert.assertEquals(getDs().getLoggedQuery(options), 1, found.size());

    options = new FindOptions().logQuery();
    found = getDs().find(User.class)
                   .disableValidation()
                   .filter(bitsAllSet("a", new int[]{1, 5})).iterator(options)
                   .toList();

    Assert.assertEquals(getDs().getLoggedQuery(options), 1, found.size());
}
 
Example 10
Source Project: brave   File: ITMongoDB.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass public static void initCollection() {
  try (MongoClient mongoClient = MongoClients.create(mongoClientSettingsBuilder().build())) {
    MongoDatabase database = mongoClient.getDatabase(DATABASE_NAME);
    MongoCollection<Document> collection = database.getCollection(COLLECTION_NAME);
    Document document1 = new Document("id", 1);
    Document document2 = new Document("id", 2);
    collection.insertMany(Arrays.asList(document1, document2));
  }
}
 
Example 11
Source Project: beam   File: MongoDbIOTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  port = NetworkTestHelper.getAvailableLocalPort();
  LOG.info("Starting MongoDB embedded instance on {}", port);
  IMongodConfig mongodConfig =
      new MongodConfigBuilder()
          .version(Version.Main.PRODUCTION)
          .configServer(false)
          .replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0))
          .net(new Net("localhost", port, Network.localhostIsIPv6()))
          .cmdOptions(
              new MongoCmdOptionsBuilder()
                  .syncDelay(10)
                  .useNoPrealloc(true)
                  .useSmallFiles(true)
                  .useNoJournal(true)
                  .verbose(false)
                  .build())
          .build();
  mongodExecutable = mongodStarter.prepare(mongodConfig);
  mongodProcess = mongodExecutable.start();
  client = new MongoClient("localhost", port);

  LOG.info("Insert test data");
  List<Document> documents = createDocuments(1000);
  MongoCollection<Document> collection = getCollection(COLLECTION);
  collection.insertMany(documents);
}
 
Example 12
Source Project: baleen   File: MongoReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
private void createContent(SharedFongoResource sfr) {
  MongoDatabase db = sfr.getDB();

  MongoCollection<Document> coll = db.getCollection(COLLECTION);
  coll.insertMany(
      Arrays.asList(
          new Document(CONTENT, "Hello World"),
          new Document(CONTENT, "Hello Test"),
          new Document(CONTENT, TEXT)
              .append("key1", "foo")
              .append("key2", "bar")
              .append("key3", Arrays.asList("howdy", "hey"))));
}
 
Example 13
Source Project: morphia   File: AggregationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMerge() {
    checkMinServerVersion(4.2);
    MongoCollection<Document> salaries = getDatabase().getCollection("salaries");

    salaries.insertMany(List.of(
        parse("{ '_id' : 1, employee: 'Ant', dept: 'A', salary: 100000, fiscal_year: 2017 }"),
        parse("{ '_id' : 2, employee: 'Bee', dept: 'A', salary: 120000, fiscal_year: 2017 }"),
        parse("{ '_id' : 3, employee: 'Cat', dept: 'Z', salary: 115000, fiscal_year: 2017 }"),
        parse("{ '_id' : 4, employee: 'Ant', dept: 'A', salary: 115000, fiscal_year: 2018 }"),
        parse("{ '_id' : 5, employee: 'Bee', dept: 'Z', salary: 145000, fiscal_year: 2018 }"),
        parse("{ '_id' : 6, employee: 'Cat', dept: 'Z', salary: 135000, fiscal_year: 2018 }"),
        parse("{ '_id' : 7, employee: 'Gecko', dept: 'A', salary: 100000, fiscal_year: 2018 }"),
        parse("{ '_id' : 8, employee: 'Ant', dept: 'A', salary: 125000, fiscal_year: 2019 }"),
        parse("{ '_id' : 9, employee: 'Bee', dept: 'Z', salary: 160000, fiscal_year: 2019 }"),
        parse("{ '_id' : 10, employee: 'Cat', dept: 'Z', salary: 150000, fiscal_year: 2019 }")));

    getDs().aggregate(Salary.class)
           .group(Group.of(id()
                               .field("fiscal_year")
                               .field("dept"))
                       .field("salaries", sum(field("salary"))))
           .merge(Merge.into("budgets")
                       .on("_id")
                       .whenMatched(WhenMatched.REPLACE)
                       .whenNotMatched(WhenNotMatched.INSERT));
    List<Document> actual = getDs().find("budgets", Document.class).iterator().toList();

    List<Document> expected = List.of(
        parse("{ '_id' : { 'fiscal_year' : 2017, 'dept' : 'A' }, 'salaries' : 220000 }"),
        parse("{ '_id' : { 'fiscal_year' : 2017, 'dept' : 'Z' }, 'salaries' : 115000 }"),
        parse("{ '_id' : { 'fiscal_year' : 2018, 'dept' : 'A' }, 'salaries' : 215000 }"),
        parse("{ '_id' : { 'fiscal_year' : 2018, 'dept' : 'Z' }, 'salaries' : 280000 }"),
        parse("{ '_id' : { 'fiscal_year' : 2019, 'dept' : 'A' }, 'salaries' : 125000 }"),
        parse("{ '_id' : { 'fiscal_year' : 2019, 'dept' : 'Z' }, 'salaries' : 310000 }"));

    assertDocumentEquals(expected, actual);
}
 
Example 14
Source Project: morphia   File: AggregationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPlanCacheStats() {
    checkMinServerVersion(4.2);
    List<Document> list = List.of(
        parse("{ '_id' : 1, 'item' : 'abc', 'price' : NumberDecimal('12'), 'quantity' : 2, 'type': 'apparel' }"),
        parse("{ '_id' : 2, 'item' : 'jkl', 'price' : NumberDecimal('20'), 'quantity' : 1, 'type': 'electronics' }"),
        parse("{ '_id' : 3, 'item' : 'abc', 'price' : NumberDecimal('10'), 'quantity' : 5, 'type': 'apparel' }"),
        parse("{ '_id' : 4, 'item' : 'abc', 'price' : NumberDecimal('8'), 'quantity' : 10, 'type': 'apparel' }"),
        parse("{ '_id' : 5, 'item' : 'jkl', 'price' : NumberDecimal('15'), 'quantity' : 15, 'type': 'electronics' }"));

    MongoCollection<Document> orders = getDatabase().getCollection("orders");
    orders.insertMany(list);

    Assert.assertNotNull(orders.createIndex(new Document("item", 1)));
    Assert.assertNotNull(orders.createIndex(new Document("item", 1)
                                                .append("quantity", 1)));
    Assert.assertNotNull(orders.createIndex(new Document("item", 1)
                                                .append("price", 1),
        new IndexOptions()
            .partialFilterExpression(new Document("price", new Document("$gte", 10)))));
    Assert.assertNotNull(orders.createIndex(new Document("quantity", 1)));
    Assert.assertNotNull(orders.createIndex(new Document("quantity", 1)
                                                .append("type", 1)));

    orders.find(parse(" { item: 'abc', price: { $gte: NumberDecimal('10') } }"));
    orders.find(parse(" { item: 'abc', price: { $gte: NumberDecimal('5') } }"));
    orders.find(parse(" { quantity: { $gte: 20 } } "));
    orders.find(parse(" { quantity: { $gte: 5 }, type: 'apparel' } "));

    List<Document> stats = getDs().aggregate(Order.class)
                                  .planCacheStats()
                                  .execute(Document.class, new AggregationOptions()
                                                               .readConcern(ReadConcern.LOCAL))
                                  .toList();

    Assert.assertNotNull(stats);
}
 
Example 15
/**
 * Inserts a new document in the given raw collection within the given database (row-like mode).
 * @param dbName
 * @param collectionName
 * @param aggregation
 * @throws Exception
 */
@Override
public void insertContextDataRaw(String dbName, String collectionName, ArrayList<Document> aggregation)
    throws Exception {
    MongoDatabase db = getDatabase(dbName);
    MongoCollection collection = db.getCollection(collectionName);
    collection.insertMany(aggregation);
}
 
Example 16
Source Project: quarkus   File: MongoOperations.java    License: Apache License 2.0 4 votes vote down vote up
private static void persist(MongoCollection collection, List<Object> entities) {
    collection.insertMany(entities);
}
 
Example 17
Source Project: game-server   File: MongoUtil.java    License: MIT License 4 votes vote down vote up
/**
 * 插入配置数据 先删除,在插入
 *
 * @param filePath
 * @param sheetName
 * @param dbName
 */
public static String insertConfigData(MongoClient client, String filePath, String sheetName, String dbName) throws Exception {
    String retString = sheetName + "更新成功";
    Args.Four<List<String>, List<String>, List<String>, List<List<Object>>> excel = ExcelUtil.readExcel(filePath, sheetName);
    if (excel == null) {
        LOGGER.warn("{}--{}未找到数据", filePath, sheetName);
        return "内部错误";
    }
    List<Document> documents = new ArrayList<>();

    MongoDatabase database = getMongoDatabase(client, dbName);
    if (database == null) {
        LOGGER.warn("{}数据库不存在", dbName);
        return "检查配置数据库不存在";
    }

    MongoCollection<Document> collection = database.getCollection(sheetName);
    if (collection == null) {
        LOGGER.warn("{}数据库集合{}不存在", dbName, collection);
        return "表不存在";
    }
    int row = excel.d().size();     //数据行数
    int column = excel.a().size();  //字段列数
    try {
        for (int i = 0; i < row; i++) {
            Document document = new Document();
            List<Object> datas = excel.d().get(i);
            for (int j = 0; j < column; j++) {
                document.append(excel.a().get(j), datas.get(j));
            }
            documents.add(document);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        LOGGER.error("Excel表{}有空行,空列,请删除", sheetName);
        retString = sheetName + "表有空行,空列,请删除";
        return retString;
    }

    if (documents.size() < 1) {
        return sheetName + "数据为空";
    }
    collection.drop();
    collection.insertMany(documents);
    return retString;
}
 
Example 18
Source Project: uavstack   File: MongoDBDataStore.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected boolean insert(DataStoreMsg msg) {

    boolean isSuccess = false;

    String collectionName = (String) msg.get(DataStoreProtocol.MONGO_COLLECTION_NAME);

    MongoCollection<Document> collection = this.datasource.getSourceConnect().getCollection(collectionName);

    // collection no exist
    if (null == collection) {

        if (log.isTraceEnable()) {
            log.warn(this, "MongoDB[" + this.datasource.getDataStoreConnection().getDbName() + "] Collection["
                    + collectionName + "] NO EXIST.");
        }

        return adaptor.handleInsertResult(isSuccess, msg, this.datasource.getDataStoreConnection());

    }

    // prepare documents
    List<Map<String, Object>> documents = (List<Map<String, Object>>) adaptor.prepareInsertObj(msg,
            this.datasource.getDataStoreConnection());

    if (null != documents) {

        // convert to Document Object
        List<Document> docs = new ArrayList<Document>();

        for (Map<String, Object> dMap : documents) {

            Document doc = new Document();

            for (String key : dMap.keySet()) {
                doc.append(key, dMap.get(key));
            }

            docs.add(doc);
        }

        // insert documents
        try {
            collection.insertMany(docs);

            isSuccess = true;

        }
        catch (MongoException e) {
            log.err(this, "INSERT MongoDB[" + this.datasource.getDataStoreConnection().getDbName() + "] Collection["
                    + collectionName + "] Documents FAIL.", e);
        }
    }

    return adaptor.handleInsertResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
 
Example 19
Source Project: jpa-unit   File: InsertOperation.java    License: Apache License 2.0 4 votes vote down vote up
private void insertData(final MongoCollection<Document> collection, final List<Document> entries) {
    collection.insertMany(entries);
}
 
Example 20
Source Project: nifi   File: PutMongoRecord.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final RecordReaderFactory recordParserFactory = context.getProperty(RECORD_READER_FACTORY)
            .asControllerService(RecordReaderFactory.class);

    final WriteConcern writeConcern = getWriteConcern(context);

    List<Document> inserts = new ArrayList<>();
    int ceiling = context.getProperty(INSERT_COUNT).asInteger();
    int added   = 0;
    boolean error = false;

    try (final InputStream inStream = session.read(flowFile);
         final RecordReader reader = recordParserFactory.createRecordReader(flowFile, inStream, getLogger())) {
        final MongoCollection<Document> collection = getCollection(context, flowFile).withWriteConcern(writeConcern);
        RecordSchema schema = reader.getSchema();
        Record record;
        while ((record = reader.nextRecord()) != null) {
            // Convert each Record to HashMap and put into the Mongo document
            Map<String, Object> contentMap = (Map<String, Object>) DataTypeUtils.convertRecordFieldtoObject(record, RecordFieldType.RECORD.getRecordDataType(record.getSchema()));
            Document document = new Document();
            for (String name : schema.getFieldNames()) {
                document.put(name, contentMap.get(name));
            }
            inserts.add(convertArrays(document));
            if (inserts.size() == ceiling) {
                collection.insertMany(inserts);
                added += inserts.size();
                inserts = new ArrayList<>();
            }
        }
        if (inserts.size() > 0) {
            collection.insertMany(inserts);
        }
    } catch (SchemaNotFoundException | IOException | MalformedRecordException | MongoException e) {
        getLogger().error("PutMongoRecord failed with error:", e);
        session.transfer(flowFile, REL_FAILURE);
        error = true;
    } finally {
        if (!error) {
            String url = clientService != null
                    ? clientService.getURI()
                    : context.getProperty(URI).evaluateAttributeExpressions().getValue();
            session.getProvenanceReporter().send(flowFile, url, String.format("Added %d documents to MongoDB.", added));
            session.transfer(flowFile, REL_SUCCESS);
            getLogger().info("Inserted {} records into MongoDB", new Object[]{ added });
        }
    }
    session.commit();
}