/** * Modify an existing document or documents in collection. By default the method updates a single document. The query parameter employs * the same query selectors, as used in {@link DBCollection#find(DBObject)}. * * @param query the selection criteria for the update * @param update the modifications to apply * @param upsert when true, inserts a document if no document matches the update query criteria * @param multi when true, updates all documents in the collection that match the update query criteria, otherwise only updates one * @param concern {@code WriteConcern} to be used during operation * @param bypassDocumentValidation whether to bypass document validation. * @param encoder the DBEncoder to use * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/ Modify * @since 2.14 */ public WriteResult update(final DBObject query, final DBObject update, final boolean upsert, final boolean multi, final WriteConcern concern, @Nullable final Boolean bypassDocumentValidation, @Nullable final DBEncoder encoder) { return update(query, update, new DBCollectionUpdateOptions().upsert(upsert).multi(multi) .writeConcern(concern).bypassDocumentValidation(bypassDocumentValidation).encoder(encoder)); }
/** * Gets the the bypass document level validation flag * * @return the bypass document level validation flag * @mongodb.server.release 3.2 */ public Boolean getBypassDocumentValidation() { return options.getBypassDocumentValidation(); }
/** * Returns the collation options * * @return the collation options * @mongodb.server.release 3.4 */ public Collation getCollation() { return options.getCollation(); }
/** * Modify an existing document or documents in collection. * * @param query the selection criteria for the update * @param update the modifications to apply * @param options the options to apply to the update operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/ Modify * @since 3.4 */ public WriteResult update(final DBObject query, final DBObject update, final DBCollectionUpdateOptions options) { notNull("query", query); notNull("update", update); notNull("options", options); WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern(); com.mongodb.bulk.WriteRequest.Type updateType = !update.keySet().isEmpty() && update.keySet().iterator().next().startsWith("$") ? com.mongodb.bulk.WriteRequest.Type.UPDATE : com.mongodb.bulk.WriteRequest.Type.REPLACE; UpdateRequest updateRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), updateType) .upsert(options.isUpsert()).multi(options.isMulti()) .collation(options.getCollation()) .arrayFilters(wrapAllowNull(options.getArrayFilters(), options.getEncoder())); return executeWriteOperation(new UpdateOperation(getNamespace(), true, writeConcern, retryWrites, singletonList(updateRequest)).bypassDocumentValidation(options.getBypassDocumentValidation())); }
private WriteResult saveDocument(final DBCollection dbColl, final DBObject document, final InsertOptions options) { if (document.get(ID_FIELD_NAME) == null) { return dbColl.insert(singletonList(document), options.getOptions()); } else { return dbColl.update(new BasicDBObject(ID_FIELD_NAME, document.get(ID_FIELD_NAME)), document, new DBCollectionUpdateOptions() .bypassDocumentValidation(options.getBypassDocumentValidation()) .writeConcern(options.getWriteConcern()) .upsert(true)); } }
/** * Set to true if a new document should be inserted if there are no matches to the query filter. * * @param isUpsert true if a new document should be inserted if there are no matches to the query filter * @return this */ public UpdateOptions upsert(final boolean isUpsert) { options.upsert(isUpsert); return this; }
/** * Sets the write concern * * @param writeConcern the write concern * @return this */ public UpdateOptions writeConcern(final WriteConcern writeConcern) { options.writeConcern(writeConcern); return this; } }
/** * Gets whether all documents matching the query filter will be removed. The default is true. * * @return whether all documents matching the query filter will be removed */ public boolean isMulti() { return options.isMulti(); }
/** * The write concern to use for the insertion. By default the write concern configured for the DBCollection instance will be used. * * @return the write concern, or null if the default will be used. */ public WriteConcern getWriteConcern() { return options.getWriteConcern(); }
/** * Sets the bypass document level validation flag. * * @param bypassDocumentValidation If true, allows the write to opt-out of document level validation. * @return this * @mongodb.server.release 3.2 */ public UpdateOptions bypassDocumentValidation(final Boolean bypassDocumentValidation) { options.bypassDocumentValidation(bypassDocumentValidation); return this; }
/** * Returns true if a new document should be inserted if there are no matches to the query filter. The default is false. * * @return true if a new document should be inserted if there are no matches to the query filter */ public boolean isUpsert() { return options.isUpsert(); }
/** * Sets whether all documents matching the query filter will be removed. * * @param multi true if all documents matching the query filter will be removed * @return this */ public UpdateOptions multi(final boolean multi) { options.multi(multi); return this; }
/** * Modify an existing document or documents in collection. * * @param query the selection criteria for the update * @param update the modifications to apply * @param options the options to apply to the update operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/ Modify * @since 3.4 */ public WriteResult update(final DBObject query, final DBObject update, final DBCollectionUpdateOptions options) { notNull("query", query); notNull("update", update); notNull("options", options); WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern(); com.mongodb.bulk.WriteRequest.Type updateType = !update.keySet().isEmpty() && update.keySet().iterator().next().startsWith("$") ? com.mongodb.bulk.WriteRequest.Type.UPDATE : com.mongodb.bulk.WriteRequest.Type.REPLACE; UpdateRequest updateRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), updateType) .upsert(options.isUpsert()).multi(options.isMulti()) .collation(options.getCollation()) .arrayFilters(wrapAllowNull(options.getArrayFilters(), options.getEncoder())); return executeWriteOperation(new UpdateOperation(getNamespace(), true, writeConcern, retryWrites, singletonList(updateRequest)).bypassDocumentValidation(options.getBypassDocumentValidation())); }
private WriteResult saveDocument(final DBCollection dbColl, final DBObject document, final InsertOptions options) { if (document.get(ID_FIELD_NAME) == null) { return dbColl.insert(singletonList(document), options.getOptions()); } else { return dbColl.update(new BasicDBObject(ID_FIELD_NAME, document.get(ID_FIELD_NAME)), document, new DBCollectionUpdateOptions() .bypassDocumentValidation(options.getBypassDocumentValidation()) .writeConcern(options.getWriteConcern()) .upsert(true)); } }
/** * Set to true if a new document should be inserted if there are no matches to the query filter. * * @param isUpsert true if a new document should be inserted if there are no matches to the query filter * @return this */ public UpdateOptions upsert(final boolean isUpsert) { options.upsert(isUpsert); return this; }
/** * Sets the write concern * * @param writeConcern the write concern * @return this */ public UpdateOptions writeConcern(final WriteConcern writeConcern) { options.writeConcern(writeConcern); return this; } }
/** * Gets whether all documents matching the query filter will be removed. The default is true. * * @return whether all documents matching the query filter will be removed */ public boolean isMulti() { return options.isMulti(); }
/** * The write concern to use for the insertion. By default the write concern configured for the DBCollection instance will be used. * * @return the write concern, or null if the default will be used. */ public WriteConcern getWriteConcern() { return options.getWriteConcern(); }
/** * Sets the bypass document level validation flag. * * @param bypassDocumentValidation If true, allows the write to opt-out of document level validation. * @return this * @mongodb.server.release 3.2 */ public UpdateOptions bypassDocumentValidation(final Boolean bypassDocumentValidation) { options.bypassDocumentValidation(bypassDocumentValidation); return this; }
/** * Returns true if a new document should be inserted if there are no matches to the query filter. The default is false. * * @return true if a new document should be inserted if there are no matches to the query filter */ public boolean isUpsert() { return options.isUpsert(); }