/** * Remove documents from a collection. * * @param query the deletion criteria using query operators. Omit the query parameter or pass an empty document to delete all documents * in the collection. * @return the result of the operation * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the delete command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/remove-documents/ Remove Documents */ public WriteResult remove(final DBObject query) { return remove(query, getWriteConcern()); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. Collection wide {@code WriteConcern} will be used. * * @param documents {@code DBObject}'s to be inserted * @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 insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final DBObject... documents) { return insert(asList(documents), getWriteConcern()); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents list of {@code DBObject} to be inserted * @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 insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents) { return insert(documents, getWriteConcern()); }
/** * Modify an existing document or documents in collection. The query parameter employs the same query selectors, as used in {@code * find()}. * * @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 * @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 Documents */ public WriteResult update(final DBObject query, final DBObject update, final boolean upsert, final boolean multi) { return update(query, update, upsert, multi, getWriteConcern()); }
/** * Update an existing document or insert a document depending on the parameter. If the document does not contain an '_id' field, then * the method performs an insert with the specified fields in the document as well as an '_id' field with a unique objectId value. If * the document contains an '_id' field, then the method performs an upsert querying the collection on the '_id' field: * <ul> * <li>If a document does not exist with the specified '_id' value, the method performs an insert with the specified fields in * the document.</li> * <li>If a document exists with the specified '_id' value, the method performs an update, * replacing all field in the existing record with the fields from the document.</li> * </ul> * * @param document {@link DBObject} to save to the collection. * @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 insert or update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/#modify-a-document-with-save-method Save */ public WriteResult save(final DBObject document) { return save(document, getWriteConcern()); }
final long maxTime, final TimeUnit maxTimeUnit) { return findAndModify(query, fields, sort, remove, update, returnNew, upsert, bypassDocumentValidation, maxTime, maxTimeUnit, getWriteConcern());
/** * Drops the index with the given name from this collection. * * @param indexName name of index to drop * @throws MongoException if the index does not exist * @mongodb.driver.manual core/indexes/ Indexes */ public void dropIndex(final String indexName) { try { executor.execute(new DropIndexOperation(getNamespace(), indexName, getWriteConcern()), getReadConcern()); } catch (MongoWriteConcernException e) { throw createWriteConcernException(e); } }
/** * Drops (deletes) this collection from the database. Use with care. * * @throws MongoException if the operation failed * @mongodb.driver.manual reference/command/drop/ Drop Command */ public void drop() { try { executor.execute(new DropCollectionOperation(getNamespace(), getWriteConcern()), getReadConcern()); } catch (MongoWriteConcernException e) { throw createWriteConcernException(e); } }
/** * Drops an index from this collection. The DBObject index parameter must match the specification of the index to drop, i.e. correct * key name and type must be specified. * * @param index the specification of the index to drop * @throws MongoException if the index does not exist * @mongodb.driver.manual core/indexes/ Indexes */ public void dropIndex(final DBObject index) { try { executor.execute(new DropIndexOperation(getNamespace(), wrap(index), getWriteConcern()), getReadConcern()); } catch (MongoWriteConcernException e) { throw createWriteConcernException(e); } }
/** * <p>Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added.</p> * * <p>If the value of the continueOnError property of the given {@code InsertOptions} is true, * that value will override the value of the continueOnError property of the given {@code WriteConcern}. Otherwise, * the value of the continueOnError property of the given {@code WriteConcern} will take effect. </p> * * @param documents a list of {@code DBObject}'s to be inserted * @param insertOptions the options to use for the insert * @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 insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents, final InsertOptions insertOptions) { WriteConcern writeConcern = insertOptions.getWriteConcern() != null ? insertOptions.getWriteConcern() : getWriteConcern(); Encoder<DBObject> encoder = toEncoder(insertOptions.getDbEncoder()); List<InsertRequest> insertRequestList = new ArrayList<InsertRequest>(documents.size()); for (DBObject cur : documents) { if (cur.get(ID_FIELD_NAME) == null) { cur.put(ID_FIELD_NAME, new ObjectId()); } insertRequestList.add(new InsertRequest(new BsonDocumentWrapper<DBObject>(cur, encoder))); } return insert(insertRequestList, writeConcern, insertOptions.isContinueOnError(), insertOptions.getBypassDocumentValidation()); }
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param fields a subset of fields to return * @param sort determines which document the operation will modify if the query selects multiple documents * @param remove when true, removes the selected document * @param returnNew when true, returns the modified document rather than the original * @param update the modifications to apply * @param upsert when true, operation creates a new document if the query returns no documents * @param maxTime the maximum time that the server will allow this operation to execute before killing it. A non-zero value requires * a server version >= 2.6 * @param maxTimeUnit the unit that maxTime is specified in * @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document * after the changes were made * @throws 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 reference/command/findAndModify/ Find and Modify * @since 2.12.0 */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject fields, @Nullable final DBObject sort, final boolean remove, @Nullable final DBObject update, final boolean returnNew, final boolean upsert, final long maxTime, final TimeUnit maxTimeUnit) { return findAndModify(query, fields, sort, remove, update, returnNew, upsert, maxTime, maxTimeUnit, getWriteConcern()); }
/** * Remove documents from a collection. * * @param query the deletion criteria using query operators. Omit the query parameter or pass an empty document to delete all * documents in the collection. * @param options the options to apply to the delete operation * @return the result of the operation * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the delete command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/remove-documents/ Remove Documents * @since 3.4 */ public WriteResult remove(final DBObject query, final DBCollectionRemoveOptions options) { notNull("query", query); notNull("options", options); WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern(); DeleteRequest deleteRequest = new DeleteRequest(wrap(query, options.getEncoder())).collation(options.getCollation()); return executeWriteOperation(new DeleteOperation(getNamespace(), false, writeConcern, retryWrites, singletonList(deleteRequest))); }
/** * 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())); }
BulkWriteResult executeBulkWriteOperation(final boolean ordered, final Boolean bypassDocumentValidation, final List<WriteRequest> writeRequests, final WriteConcern writeConcern) { try { return translateBulkWriteResult(executor.execute(new MixedBulkWriteOperation(getNamespace(), translateWriteRequestsToNew(writeRequests), ordered, writeConcern, false) .bypassDocumentValidation(bypassDocumentValidation), getReadConcern()), getObjectCodec()); } catch (MongoBulkWriteException e) { throw BulkWriteHelper.translateBulkWriteException(e, MongoClient.getDefaultCodecRegistry().get(DBObject.class)); } }
/** * Change the name of an existing collection. * * @param newName specifies the new name of the collection * @param dropTarget If {@code true}, mongod will drop the collection with the target name if it exists * @return the collection with new name * @throws MongoException if target is the name of an existing collection and {@code dropTarget=false}. * @mongodb.driver.manual reference/command/renameCollection/ Rename Collection */ public DBCollection rename(final String newName, final boolean dropTarget) { try { executor.execute(new RenameCollectionOperation(getNamespace(), new MongoNamespace(getNamespace().getDatabaseName(), newName), getWriteConcern()) .dropTarget(dropTarget), getReadConcern()); return getDB().getCollection(newName); } catch (MongoWriteConcernException e) { throw createWriteConcernException(e); } }
AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getWriteConcern()) .maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS) .allowDiskUse(options.getAllowDiskUse())
notNull("options", options); WriteConcern optionsWriteConcern = options.getWriteConcern(); WriteConcern writeConcern = optionsWriteConcern != null ? optionsWriteConcern : getWriteConcern(); WriteOperation<DBObject> operation; if (options.isRemove()) {
new BsonJavaScript(command.getReduce()), command.getOutputTarget(), getWriteConcern()) .filter(wrapAllowNull(command.getQuery())) .limit(command.getLimit())
/** * Get the write concern for this collection. * * @return THe write concern */ public WriteConcern getWriteConcern() { return dbCollection.getWriteConcern(); }
@Override public WriteResult call() { DBCollection collection = collection(); return collection.remove( extractDbObject(criteria), collection.getWriteConcern()); } }).lazyTransform(GetN.FUNCTION);