Search in sources :

Example 1 with MongoDBWhere

use of net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere in project Utility by anweisen.

the class MongoUtils method applyWhere.

public static void applyWhere(@Nonnull FindIterable<Document> iterable, @Nonnull Map<String, MongoDBWhere> where) {
    for (Entry<String, MongoDBWhere> entry : where.entrySet()) {
        MongoDBWhere value = entry.getValue();
        iterable.filter(value.toBson());
        Collation collation = value.getCollation();
        if (collation != null)
            iterable.collation(collation);
    }
}
Also used : MongoDBWhere(net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere) Collation(com.mongodb.client.model.Collation)

Example 2 with MongoDBWhere

use of net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere in project Utility by anweisen.

the class MongoDBDeletion method execute.

@Override
public Void execute() throws DatabaseException {
    try {
        MongoCollection<Document> collection = database.getCollection(this.collection);
        Document filter = new Document();
        DeleteOptions options = new DeleteOptions();
        for (MongoDBWhere where : where.values()) {
            Bson whereBson = where.toBson();
            BsonDocument asBsonDocument = BsonUtils.convertBsonToBsonDocument(whereBson);
            filter.putAll(asBsonDocument);
            Collation collation = where.getCollation();
            if (collation != null)
                options.collation(collation);
        }
        collection.deleteMany(filter, options);
        return null;
    } catch (Exception ex) {
        throw new DatabaseException(ex);
    }
}
Also used : MongoDBWhere(net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere) DeleteOptions(com.mongodb.client.model.DeleteOptions) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Collation(com.mongodb.client.model.Collation) DatabaseException(net.anweisen.utilities.database.exceptions.DatabaseException) DatabaseException(net.anweisen.utilities.database.exceptions.DatabaseException) Bson(org.bson.conversions.Bson)

Example 3 with MongoDBWhere

use of net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere in project Utility by anweisen.

the class MongoDBInsertionOrUpdate method execute.

@Override
public Void execute() throws DatabaseException {
    if (database.query(collection, where).execute().isSet()) {
        return super.execute();
    } else {
        Document document = new Document(values);
        for (Entry<String, MongoDBWhere> entry : where.entrySet()) {
            BsonDocument bson = BsonUtils.convertBsonToBsonDocument(entry.getValue().toBson());
            document.putAll(bson);
        }
        database.insert(collection, document).execute();
        return null;
    }
}
Also used : MongoDBWhere(net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 4 with MongoDBWhere

use of net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere in project Utility by anweisen.

the class MongoDBUpdate method execute.

@Override
public Void execute() throws DatabaseException {
    try {
        MongoCollection<Document> collection = database.getCollection(this.collection);
        Document filter = new Document();
        UpdateOptions options = new UpdateOptions();
        for (MongoDBWhere where : where.values()) {
            Bson whereBson = where.toBson();
            BsonDocument asBsonDocument = BsonUtils.convertBsonToBsonDocument(whereBson);
            filter.putAll(asBsonDocument);
            Collation collation = where.getCollation();
            if (collation != null)
                options.collation(collation);
        }
        Document newDocument = new Document();
        for (Entry<String, Object> entry : values.entrySet()) {
            newDocument.put(entry.getKey(), MongoUtils.packObject(entry.getValue()));
        }
        BasicDBObject update = new BasicDBObject();
        update.put("$set", newDocument);
        collection.updateMany(filter, update, options);
        return null;
    } catch (Exception ex) {
        throw new DatabaseException(ex);
    }
}
Also used : MongoDBWhere(net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Collation(com.mongodb.client.model.Collation) UpdateOptions(com.mongodb.client.model.UpdateOptions) DatabaseException(net.anweisen.utilities.database.exceptions.DatabaseException) Bson(org.bson.conversions.Bson) BasicDBObject(com.mongodb.BasicDBObject) BsonDocument(org.bson.BsonDocument) BasicDBObject(com.mongodb.BasicDBObject) DatabaseException(net.anweisen.utilities.database.exceptions.DatabaseException)

Aggregations

MongoDBWhere (net.anweisen.utilities.database.internal.mongodb.where.MongoDBWhere)4 Collation (com.mongodb.client.model.Collation)3 BsonDocument (org.bson.BsonDocument)3 Document (org.bson.Document)3 DatabaseException (net.anweisen.utilities.database.exceptions.DatabaseException)2 Bson (org.bson.conversions.Bson)2 BasicDBObject (com.mongodb.BasicDBObject)1 DeleteOptions (com.mongodb.client.model.DeleteOptions)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1