Search in sources :

Example 26 with DeleteResult

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;
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) DataAccessException(org.springframework.dao.DataAccessException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 27 with DeleteResult

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 28 with DeleteResult

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()));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DeleteResult(com.mongodb.client.result.DeleteResult) MagicCollection(org.magic.api.beans.MagicCollection)

Example 29 with DeleteResult

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());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 30 with DeleteResult

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);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) DeleteResult(com.mongodb.client.result.DeleteResult) StopWatch(core.framework.util.StopWatch)

Aggregations

DeleteResult (com.mongodb.client.result.DeleteResult)46 Document (org.bson.Document)20 UpdateResult (com.mongodb.client.result.UpdateResult)10 BsonDocument (org.bson.BsonDocument)9 Test (org.junit.jupiter.api.Test)9 BasicDBObject (com.mongodb.BasicDBObject)7 WriteConcern (com.mongodb.WriteConcern)6 Bson (org.bson.conversions.Bson)6 MongoException (com.mongodb.MongoException)5 ArrayList (java.util.ArrayList)5 DataAccessException (org.springframework.dao.DataAccessException)5 MongoGridFSException (com.mongodb.MongoGridFSException)4 MongoClient (com.mongodb.client.MongoClient)4 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)4 ReadPreference (com.mongodb.ReadPreference)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)3 ObjectId (org.bson.types.ObjectId)3