/** * Create an acknowledged DeleteResult * * @param deletedCount the number of documents deleted * @return an acknowledged DeleteResult */ public static DeleteResult acknowledged(final long deletedCount) { return new AcknowledgedDeleteResult(deletedCount); }
/** * Create an unacknowledged DeleteResult * * @return an unacknowledged DeleteResult */ public static DeleteResult unacknowledged() { return new UnacknowledgedDeleteResult(); }
/** * Create an unacknowledged UpdateResult * * @return an unacknowledged UpdateResult */ public static UpdateResult unacknowledged() { return new UnacknowledgedUpdateResult(); }
private boolean deleteTableMetadata(SchemaTableName schemaTableName) { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); if (!collectionExists(db, tableName)) { return false; } DeleteResult result = db.getCollection(schemaCollection) .deleteOne(new Document(TABLE_NAME_KEY, tableName)); return result.getDeletedCount() == 1; }
/** * Delete a record from the database. * * @param table * The name of the table * @param key * The record key of the record to delete. * @return Zero on success, a non-zero error code on error. See the {@link DB} * class's description for a discussion of error codes. */ @Override public Status delete(String table, String key) { try { MongoCollection<Document> collection = database.getCollection(table); Document query = new Document("_id", key); DeleteResult result = collection.withWriteConcern(writeConcern).deleteOne(query); if (result.wasAcknowledged() && result.getDeletedCount() == 0) { System.err.println("Nothing deleted for key " + key); return Status.NOT_FOUND; } return Status.OK; } catch (Exception e) { System.err.println(e.toString()); return Status.ERROR; } }
@Override public int updateStatus(final String id, final Integer status) { Query query = new Query(); query.addCriteria(new Criteria("transId").is(id)); Update update = new Update(); update.set("status", status); final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName); if (updateResult.getModifiedCount() <= 0) { throw new HmilyRuntimeException("update data exception!"); } return ROWS; }
private void executeRename(@Nullable final ClientSession clientSession, final BsonValue id, final String newFilename) { UpdateResult updateResult; if (clientSession != null) { updateResult = filesCollection.updateOne(clientSession, new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename)))); } else { updateResult = filesCollection.updateOne(new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename)))); } if (updateResult.wasAcknowledged() && updateResult.getMatchedCount() == 0) { throw new MongoGridFSException(format("No file found with the id: %s", id)); } }
private void removeConfigPath() { final FindIterable<Document> documentsWithConfigPath = collection.find(exists("configuration_path")); for (Document document : documentsWithConfigPath) { final ObjectId objectId = document.getObjectId("_id"); document.remove("configuration_path"); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } }
private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) { if (result.wasAcknowledged()) { Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null; BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId(); return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId); } else { return UpdateResult.unacknowledged(); } }
private DeleteResult executeDelete(@Nullable final ClientSession clientSession, final Bson filter, final DeleteOptions deleteOptions, final boolean multi) { com.mongodb.bulk.BulkWriteResult result = executeSingleWriteRequest(clientSession, multi ? operations.deleteMany(filter, deleteOptions) : operations.deleteOne(filter, deleteOptions), DELETE); if (result.wasAcknowledged()) { return DeleteResult.acknowledged(result.getDeletedCount()); } else { return DeleteResult.unacknowledged(); } }
@Override public long getMatchedCount() { throw getUnacknowledgedWriteException(); }
/** * Create an acknowledged UpdateResult * * @param matchedCount the number of documents matched * @param modifiedCount the number of documents modified * @param upsertedId if the replace resulted in an inserted document, the id of the inserted document * @return an acknowledged UpdateResult */ public static UpdateResult acknowledged(final long matchedCount, @Nullable final Long modifiedCount, @Nullable final BsonValue upsertedId) { return new AcknowledgedUpdateResult(matchedCount, modifiedCount, upsertedId); }
@Signature public boolean deleteOne(BasicDBObject filter) { DeleteResult result = getWrappedObject().deleteOne(filter); return result.getDeletedCount() > 0; }
private void executeDelete(@Nullable final ClientSession clientSession, final BsonValue id) { DeleteResult result; if (clientSession != null) { result = filesCollection.deleteOne(clientSession, new BsonDocument("_id", id)); chunksCollection.deleteMany(clientSession, new BsonDocument("files_id", id)); } else { result = filesCollection.deleteOne(new BsonDocument("_id", id)); chunksCollection.deleteMany(new BsonDocument("files_id", id)); } if (result.wasAcknowledged() && result.getDeletedCount() == 0) { throw new MongoGridFSException(format("No file found with the id: %s", id)); } }
@Override public int updateParticipant(final HmilyTransaction hmilyTransaction) { Query query = new Query(); query.addCriteria(new Criteria("transId").is(hmilyTransaction.getTransId())); Update update = new Update(); try { update.set("contents", objectSerializer.serialize(hmilyTransaction.getHmilyParticipants())); } catch (HmilyException e) { e.printStackTrace(); } final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName); if (updateResult.getModifiedCount() <= 0) { throw new HmilyRuntimeException("update data exception!"); } return ROWS; }
@Override public void upgrade() { final FindIterable<Document> documentsWithMissingFields = collection.find(or(not(exists(ContentPack.FIELD_META_ID)), not(exists(ContentPack.FIELD_META_REVISION)))); for (Document document : documentsWithMissingFields) { final ObjectId objectId = document.getObjectId("_id"); LOG.debug("Found document with missing \"id\" or \"rev\" field with ID <{}>", objectId); final String id = document.get("id", objectId.toHexString()); final int rev = document.get("rev", 0); document.put("id", id); document.put("rev", rev); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } } }
@Override public long getModifiedCount() { throw getUnacknowledgedWriteException(); }
@Signature public long deleteMany(BasicDBObject filter) { return getWrappedObject().deleteMany(filter).getDeletedCount(); }
@Override @Nullable public BsonValue getUpsertedId() { throw getUnacknowledgedWriteException(); }
@Override public Object execute(Query query) { String collectionName = method.getEntityInformation().getCollectionName(); Class<?> type = method.getEntityInformation().getJavaType(); if (method.isCollectionQuery()) { return operations.findAllAndRemove(query, type, collectionName); } DeleteResult writeResult = operations.remove(query, type, collectionName); return writeResult != null ? writeResult.getDeletedCount() : 0L; } }