@Override public Integer call() { collection().insertMany(documents); return 0; // java driver for mongo returns 0 anyway } });
/** * Inserts one or more documents. * This method is equivalent to a call to the bulkWrite method. * The documents will be inserted in the order provided, * stopping on the first failed insertion. * * @param documents documents */ public void insert(List<Document> documents, boolean ordered) { InsertManyOptions options = new InsertManyOptions(); if (!ordered) { options.ordered(false); } collection.insertMany(documents, options); }
@Signature public void insertMany(List<Document> docs) { getWrappedObject().insertMany(docs); }
protected List<Object> insertDocumentList(final String collectionName, final List<Document> documents) { if (documents.isEmpty()) { return Collections.emptyList(); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Inserting list of Documents containing {} items", documents.size()); } execute(collectionName, collection -> { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null, null, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); if (writeConcernToUse == null) { collection.insertMany(documents); } else { collection.withWriteConcern(writeConcernToUse).insertMany(documents); } return null; }); return MappedDocument.toIds(documents); }
@Override public CompletableFuture<?> appendPage(Page page) { MongoCollection<Document> collection = mongoSession.getCollection(schemaTableName); List<Document> batch = new ArrayList<>(page.getPositionCount()); for (int position = 0; position < page.getPositionCount(); position++) { Document doc = new Document(); for (int channel = 0; channel < page.getChannelCount(); channel++) { MongoColumnHandle column = columns.get(channel); doc.append(column.getName(), getObjectValue(columns.get(channel).getType(), page.getBlock(channel), position)); } batch.add(doc); } collection.insertMany(batch, new InsertManyOptions().ordered(true)); return NOT_BLOCKED; }
collection.insertMany(bulkInserts, INSERT_UNORDERED);
collection.insertMany(inserts); added += inserts.size(); inserts = new ArrayList<>(); collection.insertMany(inserts);
protected List<Object> insertDocumentList(final String collectionName, final List<Document> documents) { if (documents.isEmpty()) { return Collections.emptyList(); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Inserting list of Documents containing {} items", documents.size()); } execute(collectionName, collection -> { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null, null, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); if (writeConcernToUse == null) { collection.insertMany(documents); } else { collection.withWriteConcern(writeConcernToUse).insertMany(documents); } return null; }); return MappedDocument.toIds(documents); }
public void importData(ServerConfiguration configuration, MongoCollection mongoCollection, List<Document> mongoDocuments, boolean replaceAllDocuments) { Task task = mongoClient -> { MongoDatabase mongoDatabase = mongoCollection.getParentDatabase(); com.mongodb.client.MongoDatabase database = mongoClient.getDatabase(mongoDatabase.getName()); com.mongodb.client.MongoCollection<Document> collection = database.getCollection(mongoCollection.getName()); if (replaceAllDocuments) { collection.deleteMany(EMPTY_DOC); } collection.insertMany(mongoDocuments); }; executeTask(configuration, task); }
peopleCollection.insertMany(Arrays.asList( new Document() .append("_id", new ObjectId("582ecee28feed271b9f54e58"))
@Override public void appendEvents(MongoCollection<Document> eventCollection, List<? extends EventMessage<?>> events, Serializer serializer) { eventCollection.insertMany(createEventDocuments(events, serializer).collect(Collectors.toList())); }
@Override public void appendEvents(MongoCollection<Document> eventCollection, List<? extends EventMessage<?>> events, Serializer serializer) { eventCollection.insertMany(createEventDocuments(events, serializer).collect(Collectors.toList())); }
private void flush() { if (batch.isEmpty()) { return; } MongoDatabase mongoDatabase = client.getDatabase(spec.database()); MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(spec.collection()); mongoCollection.insertMany(batch); batch.clear(); }
@Override public void addLinks(List<String> sourceIds, String targetId, Session session) { MongoDBSession mongodbSession = (MongoDBSession) session; MongoCollection<Document> coll = getCollection(mongodbSession); List<Document> newDocs = sourceIds.stream() .map(sourceId -> buildDoc(sourceId, targetId)) .filter(doc -> coll.count(doc) == 0) .collect(Collectors.toList()); if (!newDocs.isEmpty()) { coll.insertMany(newDocs); } }
@Override public <T> void insertMany(MongoCollection<Document> collection, List<T> objects) { try { List<Document> ts = Lists.newArrayList(); 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); } }
@Override public void insertMany(@NotNull ClientSession clientSession, @NotNull List<? extends TDocument> tDocuments, @NotNull InsertManyOptions options) { maybeThrowExceptionBeforeUpdate(); collection.insertMany(clientSession, tDocuments, options); maybeThrowExceptionAfterUpdate(); }
@Override public void insertMany(@NotNull ClientSession clientSession, @NotNull List<? extends TDocument> tDocuments) { maybeThrowExceptionBeforeUpdate(); collection.insertMany(clientSession, tDocuments); maybeThrowExceptionAfterUpdate(); }
@Override public void insertMany(@NotNull List<? extends TDocument> tDocuments, @NotNull InsertManyOptions options) { maybeThrowExceptionBeforeUpdate(); collection.insertMany(tDocuments, options); maybeThrowExceptionAfterUpdate(); }
@Override public void insertMany(@NotNull List<? extends TDocument> tDocuments) { maybeThrowExceptionBeforeUpdate(); collection.insertMany(tDocuments); maybeThrowExceptionAfterUpdate(); }
@Test public void testUpdateWithIdInMulti() { collection.insertMany(Arrays.asList(json("_id: 1"), json("_id: 2"))); collection.updateMany(json("_id: {$in: [1, 2]}"), json("$set: {n: 1}")); List<Document> results = toArray(collection.find()); assertThat(results).containsExactly(json("_id: 1, n: 1"), json("_id: 2, n: 1")); }