Example usage for com.mongodb DBCollection update

List of usage examples for com.mongodb DBCollection update

Introduction

In this page you can find the example usage for com.mongodb DBCollection update.

Prototype

public WriteResult update(final DBObject query, final DBObject update) 

Source Link

Document

Modify an existing document.

Usage

From source file:com.impetus.kundera.client.MongoDBClient.java

License:Apache License

@Override
public void writeColumns(EntityManagerImpl em, EnhancedEntity e, EntityMetadata m) throws Exception {
    String dbName = m.getKeyspaceName();
    String documentName = m.getColumnFamilyName();
    String key = e.getId();/*from   w  ww  . j  a v  a2  s. c  o  m*/

    log.debug("Checking whether record already exist for " + dbName + "." + documentName + " for " + key);
    Object entity = loadColumns(em, m.getEntityClazz(), dbName, documentName, key, m);
    if (entity != null) {
        log.debug("Updating data into " + dbName + "." + documentName + " for " + key);
        DBCollection dbCollection = mongoDb.getCollection(documentName);

        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put(m.getIdColumn().getName(), key);
        BasicDBObject updatedDocument = new MongoDBDataHandler().getDocumentFromEntity(em, m, e.getEntity());
        dbCollection.update(searchQuery, updatedDocument);

    } else {
        log.debug("Inserting data into " + dbName + "." + documentName + " for " + key);
        DBCollection dbCollection = mongoDb.getCollection(documentName);

        BasicDBObject document = new MongoDBDataHandler().getDocumentFromEntity(em, m, e.getEntity());
        dbCollection.insert(document);
    }

}

From source file:com.javamongodb.application.RecordsActivity.java

private void update(String columnName, String newValue, String mobileNumber) {
    try {//from w  w  w .j a va 2s  . c o  m
        DBCollection collection = DatabaseUtils.openDBConnection();
        BasicDBObject query = new BasicDBObject("MobileNumber", new BasicDBObject("$regex", mobileNumber));
        DBCursor cursor = collection.find(query);
        DBObject doc = cursor.next();
        Object id = doc.get("_id");
        Object prevData = doc.get(columnName);
        if (!(newValue.equals(prevData.toString()))) {
            BasicDBObject updateDocument = new BasicDBObject();
            updateDocument.append("$set", new BasicDBObject().append(columnName, newValue));
            BasicDBObject searchQuery = new BasicDBObject().append("_id", id);
            int i = JOptionPane.showConfirmDialog(null, messages.getString("question.to.save"),
                    messages.getString("title.save.confirm"), JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                collection.update(searchQuery, updateDocument); //update the document
                JOptionPane.showMessageDialog(null, messages.getString("notification.database.updated"),
                        messages.getString("title.update.confirm"), JOptionPane.INFORMATION_MESSAGE);
            }
        }
        logger.debug("Updating object with id: " + id + " succeeded");
    } catch (Exception exception) {
        logger.error("Updating object failed:\n" + exception.getMessage());
    } finally {
        logger.debug("Closing database connection");
        DatabaseUtils.closeDBConnection();
    }
}

From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java

License:Apache License

public void update(AbstractDocument updateCriteria, AbstractDocument updateTo) {
    DBCollection collection = getDBCollection(updateCriteria);
    WriteResult wr = collection.update(updateCriteria.resolveToBasicDBObject(),
            updateTo.resolveToBasicDBObject());
    processWriteResult(wr);/*from   w w  w  . ja v  a2 s .  c om*/
}

From source file:com.jjorgemoura.hangmanz.model.ZDHangmanGame.java

public void update() {

    if (this.dbEngine == null) {

        return;/*www .j ava  2  s .co  m*/
    }

    //Prepare Collection
    DBCollection dbCollection = this.dbEngine.getCollection(ZDHangmanGame.DB_COLLECTION_ID);

    //My doc to update
    BasicDBObject theDocument = new BasicDBObject();
    theDocument.append("hm_uuid", this.uniqueUUID);

    //The updates
    ZDAlphabet x = this.playsRecord.get(this.playsRecord.size());

    BasicDBObject lettersDocBuilder = new BasicDBObject();
    lettersDocBuilder.append("hm_letter", x.getLetter());
    lettersDocBuilder.append("hm_letter_order", this.playsRecord.size());

    BasicDBObject newData = new BasicDBObject();
    newData.append("$set", new BasicDBObject().append("hm_latest_date", this.latestDate.toString()));
    newData.append("$push", new BasicDBObject().append("hm_letters_played", lettersDocBuilder));

    //Save - Update
    WriteResult updateResult = dbCollection.update(theDocument, newData);
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoColumnLocalServiceImpl.java

License:Open Source License

@Override
public ExpandoColumn updateColumn(long columnId, String name, int type, Object defaultData)
        throws PortalException {

    ExpandoColumn expandoColumn = super.updateColumn(columnId, name, type, defaultData);

    ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(expandoColumn.getTableId());

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    DBObject operatorDBObject = new BasicDBObject(MongoOperator.RENAME,
            new BasicDBObject(expandoColumn.getName(), name));

    dbCollection.update(new BasicDBObject(), operatorDBObject);

    return expandoColumn;
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public ExpandoValue addValue(long classNameId, long tableId, long columnId, long classPK, String data)
        throws PortalException {

    ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId);

    ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(columnId);

    ExpandoValue expandoValue = ExpandoValueUtil.create(0);

    expandoValue.setCompanyId(expandoTable.getCompanyId());
    expandoValue.setTableId(tableId);//  w  w w . j a  va  2 s  .  co m
    expandoValue.setColumnId(columnId);
    expandoValue.setRowId(classPK);
    expandoValue.setClassNameId(classNameId);
    expandoValue.setClassPK(classPK);
    expandoValue.setData(data);

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    DBObject queryDBObject = new BasicDBObject();

    queryDBObject.put("companyId", expandoTable.getCompanyId());
    queryDBObject.put("tableId", tableId);
    queryDBObject.put("rowId", classPK);
    queryDBObject.put("classNameId", classNameId);
    queryDBObject.put("classPK", classPK);

    BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject);

    if (expandoValueDBObject != null) {
        expandoValue.setValueId(expandoValueDBObject.getLong("valueId"));

        DBObject operatorDBObject = new BasicDBObject();

        DBObject updateExpandoValueDBObject = new BasicDBObject(expandoColumn.getName(),
                getData(expandoColumn, expandoValue));

        operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject);

        dbCollection.update(expandoValueDBObject, operatorDBObject);
    } else {
        long valueId = CounterLocalServiceUtil.increment();

        expandoValue.setValueId(valueId);

        queryDBObject.put("valueId", valueId);
        queryDBObject.put(expandoColumn.getName(), getData(expandoColumn, expandoValue));

        dbCollection.insert(queryDBObject);
    }

    return expandoValue;
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public void addValues(long classNameId, long tableId, List<ExpandoColumn> expandoColumns, long classPK,
        Map<String, String> data) throws PortalException {

    ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId);

    ExpandoValue expandoValue = ExpandoValueUtil.create(0);

    expandoValue.setCompanyId(expandoTable.getCompanyId());
    expandoValue.setTableId(tableId);//from   w  w  w.  j  a v  a2 s . co m
    expandoValue.setRowId(classPK);
    expandoValue.setClassNameId(classNameId);
    expandoValue.setClassPK(classPK);

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    DBObject queryDBObject = new BasicDBObject();

    queryDBObject.put("companyId", expandoTable.getCompanyId());
    queryDBObject.put("tableId", tableId);
    queryDBObject.put("rowId", classPK);
    queryDBObject.put("classNameId", classNameId);
    queryDBObject.put("classPK", classPK);

    BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject);

    if (expandoValueDBObject != null) {
        expandoValue.setValueId(expandoValueDBObject.getLong("valueId"));

        DBObject operatorDBObject = new BasicDBObject();

        DBObject updateExpandoValueDBObject = new BasicDBObject();

        updateExpandoValueDBObject(updateExpandoValueDBObject, expandoColumns, data, expandoValue);

        operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject);

        dbCollection.update(expandoValueDBObject, operatorDBObject);
    } else {
        long valueId = CounterLocalServiceUtil.increment();

        expandoValue.setValueId(valueId);

        queryDBObject.put("valueId", valueId);

        updateExpandoValueDBObject(queryDBObject, expandoColumns, data, expandoValue);

        dbCollection.insert(queryDBObject);
    }
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public void deleteValue(long companyId, long classNameId, String tableName, String columnName, long classPK)
        throws PortalException {

    ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(companyId, classNameId, tableName);

    DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable);

    DBObject queryDBObject = new BasicDBObject();

    queryDBObject.put("companyId", expandoTable.getCompanyId());
    queryDBObject.put("tableId", expandoTable.getTableId());
    queryDBObject.put("rowId", classPK);
    queryDBObject.put("classNameId", expandoTable.getClassNameId());
    queryDBObject.put("classPK", classPK);

    DBObject valueDBObject = new BasicDBObject();

    valueDBObject.put(columnName, null);

    DBObject operatorDBObject = new BasicDBObject(MongoOperator.SET, valueDBObject);

    dbCollection.update(queryDBObject, operatorDBObject);
}

From source file:com.mycompany.bean.PlytaService.java

public String updateDocProducent(String tytulBase, String producentNew) throws Exception {
    try {/*from   w  ww.  j av a  2s. c  o  m*/
        DBCollection collection = getConnection("PlytyDB", "plyty");
        collection.update(new BasicDBObject("tytul", tytulBase),
                new BasicDBObject("$set", new BasicDBObject("producent", producentNew)));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad podczas edycji plyty");
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return "Edycja wykonana";
}

From source file:com.mycompany.bean.PlytaService.java

public String updateDocGatunek(String tytulBase, ArrayList<String> gatunekNew) {
    try {//  w  w w. j a v  a 2 s  . c o  m
        DBCollection collection = getConnection("PlytyDB", "plyty");
        collection.update(new BasicDBObject("tytul", tytulBase),
                new BasicDBObject("$set", new BasicDBObject("gatunek", gatunekNew)));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad podczas edycji plyty");
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return "Edycja wykonana";
}