protected Mono<Object> saveDocument(String collectionName, Document document, Class<?> entityClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Saving Document containing fields: " + document.keySet()); } return createMono(collectionName, collection -> { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, document, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(document); MongoCollection<Document> collectionToUse = writeConcernToUse == null // ? collection // : collection.withWriteConcern(writeConcernToUse); Publisher<?> publisher = !mapped.hasId() // ? collectionToUse.insertOne(document) // : collectionToUse.replaceOne(mapped.getIdFilter(), document, new ReplaceOptions().upsert(true)); return Mono.from(publisher).map(o -> mapped.getId()); }); }
/** * Creates replace options from updateOptions. * * @param updateOptions the updateOptions * @return replace options */ public static ReplaceOptions createReplaceOptions(final UpdateOptions updateOptions) { notNull("updateOptions", updateOptions); List<? extends Bson> arrayFilters = updateOptions.getArrayFilters(); isTrue("ArrayFilters should be empty.", arrayFilters == null || arrayFilters.isEmpty()); return new ReplaceOptions() .bypassDocumentValidation(updateOptions.getBypassDocumentValidation()) .collation(updateOptions.getCollation()) .upsert(updateOptions.isUpsert()); }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(dbDoc); if (!mapped.hasId()) { if (writeConcernToUse == null) { collection.insertOne(dbDoc); } else { collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc); } } else if (writeConcernToUse == null) { collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } else { collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } return mapped.getId(); } });
replaceOptions.upsert(updateOptions.isUpsert()); replaceOptions.collation(updateOptions.getCollation());
replaceOptions.upsert(opts.isUpsert());
protected Mono<Object> saveDocument(String collectionName, Document document, Class<?> entityClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Saving Document containing fields: " + document.keySet()); } return createMono(collectionName, collection -> { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, document, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(document); MongoCollection<Document> collectionToUse = writeConcernToUse == null // ? collection // : collection.withWriteConcern(writeConcernToUse); Publisher<?> publisher = !mapped.hasId() // ? collectionToUse.insertOne(document) // : collectionToUse.replaceOne(mapped.getIdFilter(), document, new ReplaceOptions().upsert(true)); return Mono.from(publisher).map(o -> mapped.getId()); }); }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); MappedDocument mapped = MappedDocument.of(dbDoc); if (!mapped.hasId()) { if (writeConcernToUse == null) { collection.insertOne(dbDoc); } else { collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc); } } else if (writeConcernToUse == null) { collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } else { collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true)); } return mapped.getId(); } });
replaceOptions.upsert(updateOptions.isUpsert()); replaceOptions.collation(updateOptions.getCollation());
replaceOptions.upsert(opts.isUpsert());
/** * Performs an update operation, replacing the entire document. * * @param query * search query for old object to replace * @param object * object with which to replace <tt>query</tt> * @param upsert * if the database should create the element if it does not exist * @param concern * the write concern * @return The write result * @throws MongoWriteException * If the write failed due some other failure specific to the update command * @throws MongoWriteConcernException * If the write failed due being unable to fulfill the write concern * @throws MongoException * If an error occurred */ public UpdateResult replaceOne(Document query, T object, boolean upsert, WriteConcern concern) throws MongoException, MongoWriteException, MongoWriteConcernException { query = serializeFields(query); if (concern != null) { return mongoCollection.withWriteConcern(concern).replaceOne(query, object, new ReplaceOptions().upsert( upsert)); } else { return mongoCollection.replaceOne(query, object, new ReplaceOptions().upsert(upsert)); } }
/** * Performs an update operation, replacing the entire document. * * @param query * search query for old object to replace * @param object * object with which to replace <tt>query</tt> * @param upsert * if the database should create the element if it does not exist * @param concern * the write concern * @return The write result * @throws MongoWriteException * If the write failed due some other failure specific to the update command * @throws MongoWriteConcernException * If the write failed due being unable to fulfill the write concern * @throws MongoException * If an error occurred */ public UpdateResult replaceOne(DBQuery.Query query, T object, boolean upsert, WriteConcern concern) throws MongoException, MongoWriteException, MongoWriteConcernException { if (concern != null) { return mongoCollection.withWriteConcern(concern).replaceOne(serializeQuery(query), object, new ReplaceOptions().upsert( upsert)); } else { return mongoCollection.replaceOne(serializeQuery(query), object, new ReplaceOptions().upsert(upsert)); } }
@Test public void testCompoundDateIdUpserts() { Document query = json("_id: {$lt: {n: 'a', t: 10}, $gte: {n: 'a', t: 1}}"); List<Document> toUpsert = Arrays.asList( json("_id: {n: 'a', t: 1}"), json("_id: {n: 'a', t: 2}"), json("_id: {n: 'a', t: 3}"), json("_id: {n: 'a', t: 11}")); for (Document dbo : toUpsert) { collection.replaceOne(dbo, new Document(dbo).append("foo", "bar"), new ReplaceOptions().upsert(true)); } List<Document> results = toArray(collection.find(query)); assertThat(results).containsExactly( json("_id: {n: 'a', t: 1}, foo: 'bar'"), json("_id: {n: 'a', t: 2}, foo: 'bar'"), json("_id: {n: 'a', t: 3}, foo: 'bar'")); }
public V createOrUpdate(final V value, final long maxTime, final TimeUnit timeUnit) { final Document doc = encode(value); collectionWithWriteTimeout(maxTime, timeUnit) .replaceOne(byId(keyOf(value)), doc, new ReplaceOptions().upsert(true)); return decode(doc); }
/** * Creates replace options from updateOptions. * * @param updateOptions the updateOptions * @return replace options */ public static ReplaceOptions createReplaceOptions(final UpdateOptions updateOptions) { notNull("updateOptions", updateOptions); List<? extends Bson> arrayFilters = updateOptions.getArrayFilters(); isTrue("ArrayFilters should be empty.", arrayFilters == null || arrayFilters.isEmpty()); return new ReplaceOptions() .bypassDocumentValidation(updateOptions.getBypassDocumentValidation()) .collation(updateOptions.getCollation()) .upsert(updateOptions.isUpsert()); }
@Test public void testBulkUpsert() throws Exception { List<ReplaceOneModel<Document>> models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 1"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 1"), new ReplaceOptions().upsert(true)) ); BulkWriteResult result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(1), new BsonInt32(2)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder(json("_id: 1, a: 1"), json("_id: 2, a: 1")); models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 3), json("_id: 3, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 2"), new ReplaceOptions().upsert(true)) ); result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(3)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder( json("_id: 1, a: 2"), json("_id: 2, a: 2"), json("_id: 3, a: 2") ); }
private static int doReplaceOne(final MongoDBQueryDescriptor queryDescriptor, final MongoCollection<Document> collection) { final Document query = queryDescriptor.getCriteria(); final Document update = queryDescriptor.getUpdateOrInsertOne(); final Document options = queryDescriptor.getOptions(); Boolean upsert = FALSE; Collation collation = null; WriteConcern writeConcern = null; if ( options != null ) { upsert = (Boolean) options.get( "upsert" ); upsert = ( upsert != null ) ? upsert : FALSE; final Document wc = (Document) options.get( "writeConcern" ); writeConcern = ( wc != null ) ? getWriteConcern( wc ) : null; final Document col = (Document) options.get( "collation" ); collation = ( col != null ) ? getCollation( col ) : null; } final ReplaceOptions replaceOptions = new ReplaceOptions().upsert( upsert ).collation( collation ); final UpdateResult result = collection .withWriteConcern( ( writeConcern != null ? writeConcern : collection.getWriteConcern() ) ) .replaceOne( query, update, replaceOptions ); if ( result.wasAcknowledged() ) { return (int) result.getModifiedCount(); } return -1; }
private static int doReplaceOne(final MongoDBQueryDescriptor queryDescriptor, final MongoCollection<Document> collection) { final Document query = queryDescriptor.getCriteria(); final Document update = queryDescriptor.getUpdateOrInsertOne(); final Document options = queryDescriptor.getOptions(); Boolean upsert = FALSE; Collation collation = null; WriteConcern writeConcern = null; if ( options != null ) { upsert = (Boolean) options.get( "upsert" ); upsert = ( upsert != null ) ? upsert : FALSE; final Document wc = (Document) options.get( "writeConcern" ); writeConcern = ( wc != null ) ? getWriteConcern( wc ) : null; final Document col = (Document) options.get( "collation" ); collation = ( col != null ) ? getCollation( col ) : null; } final ReplaceOptions replaceOptions = new ReplaceOptions().upsert( upsert ).collation( collation ); final UpdateResult result = collection .withWriteConcern( ( writeConcern != null ? writeConcern : collection.getWriteConcern() ) ) .replaceOne( query, update, replaceOptions ); if ( result.wasAcknowledged() ) { return (int) result.getModifiedCount(); } return -1; }