protected Replacer( Repository<T> repository, T document, Constraints.ConstraintHost criteria, Constraints.Constraint ordering) { super(repository); this.document = checkNotNull(document, "document"); this.criteria = checkNotNull(criteria, "criteria"); this.ordering = checkNotNull(ordering, "ordering"); this.options = new FindOneAndReplaceOptions(); }
@Override @Nullable public TDocument findOneAndReplace(final Bson filter, final TDocument replacement) { return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions()); }
@Override @Nullable public TDocument findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) { return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions()); }
private FindOneAndReplaceOptions convertToFindOneAndReplaceOptions(FindAndReplaceOptions options, Document fields, Document sort) { FindOneAndReplaceOptions result = new FindOneAndReplaceOptions().collation(collation); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } return result; } }
@Override public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndReplaceOptions opts = new FindOneAndReplaceOptions(); opts.sort(sort); opts.collation(collation); opts.projection(fields); if (options.isUpsert()) { opts.upsert(true); } if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } return collection.findOneAndReplace(query, update, opts); } }
public void update(ServerConfiguration configuration, final MongoCollection mongoCollection, final Document mongoDocument) { 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 (!mongoDocument.containsKey("_id")) { collection.insertOne(mongoDocument); } else { collection.findOneAndReplace( new Document("_id", mongoDocument.get("_id")), mongoDocument, new FindOneAndReplaceOptions().upsert(true)); } }; executeTask(configuration, task); }
private FindOneAndReplaceOptions convertToFindOneAndReplaceOptions(FindAndReplaceOptions options, Document fields, Document sort) { FindOneAndReplaceOptions result = new FindOneAndReplaceOptions().collation(collation); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } return result; } }
@Override public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndReplaceOptions opts = new FindOneAndReplaceOptions(); opts.sort(sort); opts.collation(collation); opts.projection(fields); if (options.isUpsert()) { opts.upsert(true); } if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } return collection.findOneAndReplace(query, update, opts); } }
protected Replacer( Repository<T> repository, T document, Constraints.ConstraintHost criteria, Constraints.Constraint ordering) { super(repository); this.document = checkNotNull(document, "document"); this.criteria = checkNotNull(criteria, "criteria"); this.ordering = checkNotNull(ordering, "ordering"); this.options = new FindOneAndReplaceOptions(); }
@Override public Publisher<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement) { return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions()); }
@Override public Publisher<TDocument> findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) { return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions()); }
@Override @Nullable public TDocument findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) { return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions()); }
@Override @Nullable public TDocument findOneAndReplace(final Bson filter, final TDocument replacement) { return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions()); }
@Override @Nullable public TDocument findOneAndReplace(final Bson filter, final TDocument replacement) { return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions()); }
@Override @Nullable public TDocument findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) { return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions()); }
@Override public void saveObject(T instance) { // Null check if (instance == null) { plugin.logError("MongoDB database request to store a null. "); return; } if (!(instance instanceof DataObject)) { plugin.logError("This class is not a DataObject: " + instance.getClass().getName()); return; } DataObject dataObj = (DataObject)instance; try { Gson gson = getGson(); String toStore = gson.toJson(instance); // Change uniqueId to _id toStore = toStore.replaceFirst(UNIQUEID, MONGO_ID); // This parses JSON to a Mongo Document Document document = Document.parse(toStore); // Filter based on the id Bson filter = new Document(MONGO_ID, dataObj.getUniqueId()); // Set the options to upsert (update or insert if doc is not there) FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().upsert(true); // Do the deed collection.findOneAndReplace(filter, document, options); } catch (Exception e) { plugin.logError("Could not save object " + instance.getClass().getName() + " " + e.getMessage()); } }
public void updateIfMatch(final V value, final String eTag) { Bson query = and(eq(AbstractMongoRepository.ID, keyOf(value)), eq(ETAG, eTag)); Document updatedETaggable = collection().findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { Optional<V> findById = findOne(keyOf(value)); if (findById.isPresent()) { throw new ConcurrentModificationException("Entity concurrently modified: " + keyOf(value)); } throw new NotFoundException("Entity does not exist: " + keyOf(value)); } }
@Override public void appendSnapshot(MongoCollection<Document> snapshotCollection, DomainEventMessage<?> snapshot, Serializer serializer) { snapshotCollection.findOneAndReplace(new BsonDocument(eventConfiguration.aggregateIdentifierProperty(), new BsonString(snapshot.getAggregateIdentifier())), createSnapshotDocument(snapshot, serializer), new FindOneAndReplaceOptions().upsert(true)); }
@Override public void appendSnapshot(MongoCollection<Document> snapshotCollection, DomainEventMessage<?> snapshot, Serializer serializer) { snapshotCollection.findOneAndReplace(new BsonDocument(eventConfiguration.aggregateIdentifierProperty(), new BsonString(snapshot.getAggregateIdentifier())), createSnapshotDocument(snapshot, serializer), new FindOneAndReplaceOptions().upsert(true)); }
FindOneAndReplaceOptions findOneAndReplaceOptions = new FindOneAndReplaceOptions() .sort(sort) .projection(projection)