Example usage for com.mongodb.client.result UpdateResult getModifiedCount

List of usage examples for com.mongodb.client.result UpdateResult getModifiedCount

Introduction

In this page you can find the example usage for com.mongodb.client.result UpdateResult getModifiedCount.

Prototype

public abstract long getModifiedCount();

Source Link

Document

Gets the number of documents modified by the update.

Usage

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public Integer unknownReplaceOne(String database, String collection, Bson builder, Document docUpdate) {
    Integer documentosModificados = 0;

    try {/*  w ww  .j a  v a2 s.c  o m*/
        MongoDatabase db = getMongoClient().getDatabase(database);
        UpdateResult updateResult = db.getCollection(collection).replaceOne(builder, docUpdate);
        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName() + "unknownUpdate()").log(Level.SEVERE, null, e);
        exception = new Exception("unknownUpdate() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

private Integer update(T t2, Document doc) {
    Integer documentosModificados = 0;
    Document search = new Document();

    try {/* w ww. ja va2 s .c  om*/
        search = findDocPrimaryKey(t2);

        UpdateResult updateResult = getMongoDatabase().getCollection(collection).updateOne(search, doc);
        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "updateOne()").log(Level.SEVERE, null, e);
        exception = new Exception("updateOne() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

public Integer update(Document docSearch, Document docUpdate) {
    Integer documentosModificados = 0;

    try {/*from   w ww  .  java2s .  com*/

        UpdateResult updateResult = getMongoDatabase().getCollection(collection).updateOne(docSearch,
                docUpdate);
        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "updateOne()").log(Level.SEVERE, null, e);
        exception = new Exception("updateOne() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 * Actualiza multiples documentos//from www  .j  av a2  s  . c  o m
 *
 * @param docSearch
 * @param docUpdate
 * @return
 */
public Integer updateMany(Document docSearch, Document docUpdate) {
    Integer documentosModificados = 0;

    try {

        UpdateResult updateResult = getMongoDatabase().getCollection(collection).updateMany(docSearch,
                docUpdate);
        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "updateMany()").log(Level.SEVERE, null, e);
        exception = new Exception("updateMany() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 * implementa replaceOne//from ww w  . j a  v a 2  s.c  o  m
 *
 * @param key
 * @param value
 * @param docUpdate
 * @return
 */
public Integer replaceOne(String key, String value, Document docUpdate) {
    Integer documentosModificados = 0;

    try {
        UpdateResult updateResult = getMongoDatabase().getCollection(collection)
                .replaceOne(Filters.eq(key, value), docUpdate);

        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "replaceOne()").log(Level.SEVERE, null, e);
        exception = new Exception("replaceOne() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param key/*www.j av  a  2 s  .c  o  m*/
 * @param value
 * @param docUpdate
 * @return
 */
public Integer replaceOne(Bson search, Document docUpdate) {
    Integer documentosModificados = 0;

    try {
        UpdateResult updateResult = getMongoDatabase().getCollection(collection).replaceOne(search, docUpdate);

        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "replaceOne()").log(Level.SEVERE, null, e);
        exception = new Exception("replaceOne() ", e);
    }
    return 0;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param docSearch//from   w ww.j  a  va  2 s  .c o  m
 * @param docUpdate
 * @param options
 * @return
 */
public Integer replaceOne(Document docSearch, Document docUpdate, String... options) {
    Integer documentosModificados = 0;

    try {

        UpdateResult updateResult = getMongoDatabase().getCollection(collection).replaceOne(docSearch,
                docUpdate);
        return (int) updateResult.getModifiedCount();

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName() + "updateOne()").log(Level.SEVERE, null, e);
        exception = new Exception("updateOne() ", e);
    }
    return 0;
}

From source file:com.dilmus.dilshad.scabi.db.DBackFile.java

License:Open Source License

public long updateMetaData(String fileName, ObjectId fileID, String type, String contentType)
        throws IOException, DScabiException, ParseException {
    long n = 0;//from  ww  w. j a  v  a  2s.  c  om
    String uploadDate = null;
    Date datefromDB = null;

    Document documentWhere = new Document();
    documentWhere.put("_id", fileID);

    FindIterable<Document> cursor = m_table.find(documentWhere);
    MongoCursor<Document> cursorExist = cursor.iterator();
    n = m_table.count(documentWhere);
    if (1 == n) {
        log.debug("updateMetaData() Inside 1 == n");
        while (cursorExist.hasNext()) {
            Document ob = cursorExist.next();
            log.debug("updateMetaData() result from ob {}", ob.toString());
            //datefromDB = (String) ((BasicBSONObject) ob).getString("uploadDate");
            datefromDB = ob.getDate("uploadDate");
            if (null == datefromDB) {
                throw new DScabiException("updateMetaData() Unable to get uploadDate for file : " + fileName
                        + " fileID : " + fileID.toHexString(), "DBF.UMD.1");
            }
            log.debug("datefromDB : {}", datefromDB);

        }

    } else if (0 == n) {
        log.debug("updateMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString());
        throw new DScabiException(
                "updateMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString(),
                "DBF.UMD.2");
    } else {
        log.debug("updateMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString());
        throw new DScabiException("updateMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString(), "DBF.UMD.3");
    }

    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    dateFormat.setTimeZone(TimeZone.getTimeZone("ISO"));
    String putClientDateTime = dateFormat.format(date);
    // To parse from string : Date date2 = dateFormat.parse(putDateTime);
    // Uses java.time java 8 : ZonedDateTime now = ZonedDateTime.now( ZoneOffset.UTC );           
    String millisTime = "" + System.currentTimeMillis();
    String nanoTime = "" + System.nanoTime();

    /* If datefromDB is String
    SimpleDateFormat dateFormatFromDB = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    dateFormatFromDB.setTimeZone(TimeZone.getTimeZone("ISO"));
            
    CharSequence cs1 = "T";
    CharSequence cs2 = "Z";
    String s1 = datefromDB.replace(cs1, " ");
    String s2 = s1.replace(cs2, "");
            
    Date date2 = dateFormatFromDB.parse(s2);
    uploadDate = dateFormat.format(date2);
    */

    uploadDate = dateFormat.format(datefromDB);
    log.debug("uploadDate : {}", uploadDate);

    Document documentUpdate = new Document();
    documentUpdate.append("PutFileName", fileName);
    documentUpdate.append("PutServerFileID", fileID.toHexString());
    documentUpdate.append("PutServerUploadDateTime", uploadDate);
    documentUpdate.append("PutType", type);
    documentUpdate.append("PutContentType", contentType);
    documentUpdate.append("PutClientDateTime", putClientDateTime);
    documentUpdate.append("PutClientDateTimeInMillis", millisTime);
    documentUpdate.append("PutClientDateTimeInNano", nanoTime);
    documentUpdate.append("PutStatus", "Completed");
    documentUpdate.append("PutLatestNumber", "1");

    Document updateObj = new Document();
    updateObj.put("$set", documentUpdate);

    UpdateResult result = m_table.updateMany(documentWhere, updateObj);
    if (1 != result.getModifiedCount())
        throw new DScabiException(
                "Update meta data failed for file : " + fileName + " fileID : " + fileID.toHexString(),
                "DBF.UMD.4");

    handlePreviousVersions(fileName, fileID.toHexString(), uploadDate);

    return result.getModifiedCount();

}

From source file:com.dilmus.dilshad.scabi.db.DBackFile.java

License:Open Source License

private int handlePreviousVersions(String fileName, String strFileID, String strPutServerUploadDateTime)
        throws IOException, DScabiException {
    long m = 0;/*from  w  w  w.ja  v a  2s.c  o m*/
    long n = 0;

    // It is better to call this only after meta data is updated for currently uploaded file
    // This will skip checking for given input strFileID, file ID of currently uploaded file
    removeFilesIncompleteMetaData(fileName, strFileID);

    Document documentFind = new Document();
    documentFind.put("PutFileName", fileName);
    documentFind.append("PutServerFileID", strFileID);
    documentFind.append("PutStatus", "Completed");
    documentFind.append("PutLatestNumber", "1");

    FindIterable<Document> cursor = m_table.find(documentFind);
    m = m_table.count(documentFind);

    if (1 == m) {
        log.debug("handlePreviousVersions() Inside 1 == n");
    } else if (0 == m) {
        log.debug("handlePreviousVersions() No matches for file : " + fileName + " strFileID : " + strFileID);
        throw new DScabiException(
                "handlePreviousVersions() No matches for file : " + fileName + " strFileID : " + strFileID,
                "DBF.HPV.1");
    } else {
        log.debug("handlePreviousVersions() Multiple matches for file : " + fileName + " strFileID : "
                + strFileID);
        throw new DScabiException("handlePreviousVersions() Multiple matches for file : " + fileName
                + " strFileID : " + strFileID, "DBF.HPV.2");
    }

    Document documentQuery = new Document();
    documentQuery.put("PutFileName", fileName);
    documentQuery.append("PutStatus", "Completed");

    FindIterable<Document> cursor2 = m_table.find(documentQuery);
    MongoCursor<Document> cursorExist = cursor2.iterator();
    n = m_table.count(documentQuery);
    if (1 == n) {
        log.debug(
                "handlePreviousVersions() Information only : Inside 1 == n. Only one file / current file is found. No previous versions for file : "
                        + fileName + " with PutStatus=Completed");
        return 0;
    } else if (0 == n) {
        log.debug("handlePreviousVersions() No matches for file : " + fileName + " with PutStatus=Completed");
        throw new DScabiException(
                "handlePreviousVersions()() No matches for file : " + fileName + " with PutStatus=Completed",
                "DBF.HPV.3");
    } else {
        long lf1 = Long.parseLong(strPutServerUploadDateTime);
        while (cursorExist.hasNext()) {
            Document ob = cursorExist.next();
            log.debug("handlePreviousVersions() result from ob {}", ob.toString());

            String fid = ob.getString("PutServerFileID");
            if (null == fid) {
                throw new DScabiException("PutServerFileID is missing for one version of file : " + fileName,
                        "DBF.HPV.4");
            }
            /* Don't use. It should be based on date-time and not on file ID
            if (f.equals(strFileID)) {
               // proceed with other versions
               continue;
            }
            */
            String f = ob.getString("PutServerUploadDateTime");
            if (null == f) {
                throw new DScabiException("PutServerUploadDateTime is missing for one version of file : "
                        + fileName + " file ID : " + fid, "DBF.HPV.5");
            }
            String f2 = ob.getString("PutLatestNumber");
            if (null == f2) {
                throw new DScabiException("PutLatestNumber is missing for one version of file : " + fileName
                        + " file ID : " + fid, "DBF.HPV.6");
            }
            if (f.equals(strPutServerUploadDateTime) && f2.equals("1")) {
                // proceed with other versions
                continue;
            }
            long lf2 = Long.parseLong(f);
            if (lf1 < lf2 && f2.equals("1")) {
                // proceed with other versions
                continue;
            }
            if (f2.equals("1")) {
                // all file entries here have PutServerUploadDateTime < strPutServerUploadDateTime
                // there can be multiple previous versions with PutLatestNumber=1
                Document documentWhere = new Document();
                documentWhere.put("PutServerFileID", fid);

                Document documentUpdate = new Document();
                documentUpdate.append("PutLatestNumber", "2");

                Document updateObj = new Document();
                updateObj.put("$set", documentUpdate);
                // there should be only one entry for file ID fid
                UpdateResult result = m_table.updateMany(documentWhere, updateObj);
                if (result.getModifiedCount() <= 0)
                    throw new DScabiException("Update meta data to PutLatestNumber=2 failed for file : "
                            + fileName + " file ID : " + fid, "DBF.HPV.7");
            } else {
                // remove all other versions
                m_gridFSBucket.delete(new ObjectId(fid));
            }

        }
    }
    return 0;
}

From source file:com.dilmus.dilshad.scabi.db.DTable.java

License:Open Source License

public long update(DDocument dob, DDocument updateObj) throws DScabiException {
    UpdateResult result = m_table.updateMany(dob.getDocument(), updateObj.getDocument());
    log.debug("update() result is : {}", result.getModifiedCount());
    if (result.getModifiedCount() < 0)
        throw new DScabiException(
                "Update failed for dob : " + dob.toString() + " updateObj : " + updateObj.toString(),
                "DBT.UPE.1");
    return result.getModifiedCount();
}