MixedBulkWriteOperation updateOne(final Bson filter, final Bson update, final UpdateOptions updateOptions) { return bulkWrite(singletonList(new UpdateOneModel<TDocument>(filter, update, updateOptions)), new BulkWriteOptions().bypassDocumentValidation(updateOptions.getBypassDocumentValidation())); }
new ArrayList<UpdateOneModel<Document>>(bulkInserts.size()); for (Document doc : bulkInserts) { updates.add(new UpdateOneModel<Document>( new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
/** * Performs update and upsert bulk operations. * * @param query the {@link Query} to determine documents to update. * @param update the {@link Update} to perform, must not be {@literal null}. * @param upsert whether to upsert. * @param multi whether to issue a multi-update. * @return the {@link BulkOperations} with the update registered. */ private BulkOperations update(Query query, Update update, boolean upsert, boolean multi) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(update, "Update must not be null!"); UpdateOptions options = new UpdateOptions(); options.upsert(upsert); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); if (multi) { models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } else { models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } return this; }
private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) { if (writeModel instanceof UpdateOneModel) { UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel; return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof UpdateManyModel) { UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel; return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof DeleteOneModel) { DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel; return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } if (writeModel instanceof DeleteManyModel) { DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel; return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } return writeModel; }
/** * Performs update and upsert bulk operations. * * @param query the {@link Query} to determine documents to update. * @param update the {@link Update} to perform, must not be {@literal null}. * @param upsert whether to upsert. * @param multi whether to issue a multi-update. * @return the {@link BulkOperations} with the update registered. */ private BulkOperations update(Query query, Update update, boolean upsert, boolean multi) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(update, "Update must not be null!"); UpdateOptions options = new UpdateOptions(); options.upsert(upsert); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); if (multi) { models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } else { models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } return this; }
private UpdateOneModel<Document> createRevisionUpdate(final Bson thingFilter, final long targetRevision) { final Document update = new Document(PersistenceConstants.SET, new Document(PersistenceConstants.FIELD_REVISION, targetRevision)); return new UpdateOneModel<>(thingFilter, update); }
private UpdateOneModel<Document> createRevisionUpdate(final Bson thingFilter, final long targetRevision) { final Document update = new Document(PersistenceConstants.SET, new Document(PersistenceConstants.FIELD_REVISION, targetRevision)); return new UpdateOneModel<>(thingFilter, update); }
private static List<UpdateOneModel<Document>> createThingIndexModels(final Bson filter, final PolicyUpdate update) { final List<UpdateOneModel<Document>> updates = new ArrayList<>(3); final Bson pullGlobalReads = update.getPullGlobalReads(); if (pullGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pullGlobalReads)); } final Bson pushGlobalReads = update.getPushGlobalReads(); if (pushGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pushGlobalReads)); } final Bson pullAclEntries = update.getPullAclEntries(); if (pullAclEntries != null) { updates.add(new UpdateOneModel<>(filter, pullAclEntries)); } return updates; }
private static List<UpdateOneModel<Document>> createThingIndexModels(final Bson filter, final PolicyUpdate update) { final List<UpdateOneModel<Document>> updates = new ArrayList<>(3); final Bson pullGlobalReads = update.getPullGlobalReads(); if (pullGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pullGlobalReads)); } final Bson pushGlobalReads = update.getPushGlobalReads(); if (pushGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pushGlobalReads)); } final Bson pullAclEntries = update.getPullAclEntries(); if (pullAclEntries != null) { updates.add(new UpdateOneModel<>(filter, pullAclEntries)); } return updates; }
private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) { if (writeModel instanceof UpdateOneModel) { UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel; return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof UpdateManyModel) { UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel; return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof DeleteOneModel) { DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel; return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } if (writeModel instanceof DeleteManyModel) { DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel; return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } return writeModel; }
@Override public void saveStates(List<State> list) { MongoDbOperations.doDbOperation(() -> { List<WriteModel<Document>> operations = new ArrayList<>(); for (State state : list) { operations.add(new UpdateOneModel<>(new Document("clz", state.getSagaStateId()).append("sid", state.getId()), new Document("$set",new Document("s", state.getState())),new UpdateOptions().upsert(true))); } final BulkWriteResult bulkWriteResult = states.bulkWrite(operations); return bulkWriteResult; }); } }
private void insertUpdateInBulkNoMatch(boolean ordered) { collection.insertOne(json("foo: 'bar'")); List<WriteModel<Document>> ops = new ArrayList<>(); ops.add(new UpdateOneModel<Document>(ne("foo", "bar"), set("field", "y"))); BulkWriteResult result = collection.bulkWrite(ops, new BulkWriteOptions().ordered(ordered)); assertThat(result.getInsertedCount()).isEqualTo(0); assertThat(result.getDeletedCount()).isEqualTo(0); assertThat(result.getModifiedCount()).isEqualTo(0); assertThat(result.getMatchedCount()).isEqualTo(0); }
private <T extends ThingEvent> List<WriteModel<Document>> createThingUpdates(final Bson filter, final T thingEvent) { final List<Bson> updates = persistenceStrategyFactory .getStrategy(thingEvent) .thingUpdates(thingEvent, IndexLengthRestrictionEnforcer.newInstance(log, thingEvent.getThingId())); return updates .stream() .map(update -> new UpdateOneModel<Document>(filter, update, new UpdateOptions().upsert(true))) .collect(Collectors.toList()); }
public <T extends Document> void bulkUpsertSessions(Collection<SessionEvent> events) { final BulkWriteResult res = sessions().bulkWrite(events.stream().map(event -> { if (event instanceof DeleteSessionEvent) { return new DeleteOneModel<T>(new Document("_id", event.getSessionId())); } else if (event instanceof UpdateSessionEvent) { return new UpdateOneModel<T>( new Document("_id", event.getSessionId()).append("object.sessionId", event.getSessionId()), new Document("$set", new Document("object.$.timestamp", event.getTimestamp())) ); } else { return new UpdateOneModel<T>( new Document("_id", event.getSessionId()), new Document("$set", new Document("object", serializer.toDBObject(event).get(OBJECT_FIELD))), new UpdateOptions().upsert(true) ); } }).collect(toList())); LOGGER.info("Sessions update in bulk results: {} created, {} updated, {} deleted, {} upserted", res.getInsertedCount(), res.getModifiedCount(), res.getDeletedCount(), res.getUpserts().size()); }
MixedBulkWriteOperation updateOne(final Bson filter, final Bson update, final UpdateOptions updateOptions) { return bulkWrite(singletonList(new UpdateOneModel<TDocument>(filter, update, updateOptions)), new BulkWriteOptions().bypassDocumentValidation(updateOptions.getBypassDocumentValidation())); }
private <T extends ThingEvent> List<WriteModel<Document>> createThingUpdates(final Bson filter, final T thingEvent) { final List<Bson> updates = persistenceStrategyFactory .getStrategy(thingEvent) .thingUpdates(thingEvent, IndexLengthRestrictionEnforcer.newInstance(log, thingEvent.getThingId())); return updates .stream() .map(update -> new UpdateOneModel<Document>(filter, update, new UpdateOptions().upsert(true))) .collect(Collectors.toList()); }
public void Update(Document doc, boolean upsert) { Document find; UpdateOptions uo = new UpdateOptions(); uo.upsert(upsert); if(doc.containsKey("$find")) { find = (Document)doc.get("$find"); doc.remove("$find"); ops.add(new UpdateOneModel<Document>(find,doc,uo)); } else { logger.error("No $find section defined"); System.exit(1); } FlushOpsIfFull(); }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); BsonDateTime dateTime = new BsonDateTime(Instant.now().toEpochMilli()); return new UpdateOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, vd.get(DBCollection.ID_FIELD_NAME)), new BsonDocument("$set", vd.append(FIELDNAME_MODIFIED_TS, dateTime)) .append("$setOnInsert", new BsonDocument(FIELDNAME_INSERTED_TS, dateTime)), UPDATE_OPTIONS ); } }
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument vd = document.getValueDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the value document was missing unexpectedly") ); BsonDateTime dateTime = new BsonDateTime(Instant.now().toEpochMilli()); return new UpdateOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME, vd.get(DBCollection.ID_FIELD_NAME)), new BsonDocument("$set", vd.append(FIELDNAME_MODIFIED_TS, dateTime)) .append("$setOnInsert", new BsonDocument(FIELDNAME_INSERTED_TS, dateTime)), UPDATE_OPTIONS ); } }
@Override public void accept(BsonValue _document) { BsonDocument document = _document.asDocument(); // generate new id if missing, will be an insert if (!document.containsKey("_id")) { document .put("_id", new BsonObjectId(new ObjectId())); } // add the _etag document.put("_etag", new BsonObjectId(etag)); Bson _filter = eq("_id", document.get("_id")); if (shardKeys != null) { _filter = and(_filter, shardKeys); } if (filter != null && !filter.isEmpty()) { _filter = and(_filter, filter); } updates.add(new UpdateOneModel<>( _filter, getUpdateDocument(document), new UpdateOptions().upsert(true) )); } });