use of com.mongodb.client.result.DeleteResult in project spring-data-mongodb by spring-projects.
the class MongoChangeSetPersister method persistState.
/*
* (non-Javadoc)
* @see org.springframework.data.crossstore.ChangeSetPersister#persistState(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet)
*/
public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
if (cs == null) {
log.debug("Flush: changeset was null, nothing to flush.");
return 0L;
}
if (log.isDebugEnabled()) {
log.debug("Flush: changeset: {}", cs.getValues());
}
String collName = getCollectionNameForEntity(entity.getClass());
if (mongoTemplate.getCollection(collName) == null) {
mongoTemplate.createCollection(collName);
}
for (String key : cs.getValues().keySet()) {
if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) {
Object value = cs.getValues().get(key);
final Document dbQuery = new Document();
dbQuery.put(ENTITY_ID, getPersistentId(entity, cs));
dbQuery.put(ENTITY_CLASS, entity.getClass().getName());
dbQuery.put(ENTITY_FIELD_NAME, key);
final Document dbId = mongoTemplate.execute(collName, new CollectionCallback<Document>() {
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
Document id = collection.find(dbQuery).first();
return id;
}
});
if (value == null) {
if (log.isDebugEnabled()) {
log.debug("Flush: removing: {}", dbQuery);
}
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
DeleteResult dr = collection.deleteMany(dbQuery);
return null;
}
});
} else {
final Document dbDoc = new Document();
dbDoc.putAll(dbQuery);
if (log.isDebugEnabled()) {
log.debug("Flush: saving: {}", dbQuery);
}
mongoTemplate.getConverter().write(value, dbDoc);
dbDoc.put(ENTITY_FIELD_CLASS, value.getClass().getName());
if (dbId != null) {
dbDoc.put("_id", dbId.get("_id"));
}
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
if (dbId != null) {
collection.replaceOne(Filters.eq("_id", dbId.get("_id")), dbDoc);
} else {
if (dbDoc.containsKey("_id") && dbDoc.get("_id") == null) {
dbDoc.remove("_id");
}
collection.insertOne(dbDoc);
}
return null;
}
});
}
}
}
return 0L;
}
use of com.mongodb.client.result.DeleteResult in project MtgDesktopCompanion by nicho92.
the class MongoDbDAO method removeEdition.
@Override
public void removeEdition(MagicEdition me, MagicCollection col) throws SQLException {
logger.debug("delete " + me + " from " + col);
BasicDBObject query = new BasicDBObject();
query.put(dbColIDField, col.getName());
query.put(dbEditionField, me.getId().toUpperCase());
DeleteResult dr = db.getCollection(colCards).deleteMany(query);
logger.debug(dr);
}
use of com.mongodb.client.result.DeleteResult in project MtgDesktopCompanion by nicho92.
the class MongoDbDAO method removeCollection.
@Override
public void removeCollection(MagicCollection c) throws SQLException {
logger.debug("remove collection " + c);
BasicDBObject query = new BasicDBObject();
query.put(dbColIDField, c.getName());
DeleteResult dr = db.getCollection(colCards).deleteMany(query);
logger.trace(dr);
MongoCollection<MagicCollection> collection = db.getCollection(colCollects, MagicCollection.class);
collection.deleteOne(Filters.eq("name", c.getName()));
}
use of com.mongodb.client.result.DeleteResult in project MtgDesktopCompanion by nicho92.
the class MongoDbDAO method removeCard.
@Override
public void removeCard(MagicCard mc, MagicCollection collection) throws SQLException {
logger.debug("removeCard " + mc + " from " + collection);
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<>();
obj.add(new BasicDBObject(dbIDField, IDGenerator.generate(mc)));
obj.add(new BasicDBObject(dbColIDField, collection.getName()));
andQuery.put("$and", obj);
DeleteResult dr = db.getCollection(colCards, BasicDBObject.class).deleteMany(andQuery);
logger.debug(dr.toString());
}
use of com.mongodb.client.result.DeleteResult in project core-ng-project by neowu.
the class MongoCollectionImpl method delete.
@Override
public long delete(Bson filter) {
StopWatch watch = new StopWatch();
long deletedRows = 0;
try {
DeleteResult result = collection().deleteMany(filter == null ? new BsonDocument() : filter);
deletedRows = result.getDeletedCount();
return deletedRows;
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, (int) deletedRows);
logger.debug("delete, collection={}, filter={}, deletedRows={}, elapsedTime={}", collectionName, new BsonParam(filter, mongo.registry), deletedRows, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations