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:serwlety.edytuj.OsobaServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from w  w  w. j av a  2  s. co m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    DBCollection collection = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
    BasicDBObject rekord = new BasicDBObject();
    rekord.append("imie", request.getParameter("imie")).append("nazwisko", request.getParameter("nazwisko"))
            .append("partie", request.getParameterValues("partie"))
            .append("wyksztalcenie", request.getParameterValues("wyksztalcenie"));

    int i = 0;
    List<BasicDBObject> lista = new ArrayList<>();
    if (request.getParameterValues("dodatkowy") != null) {
        for (String dodatkowy : request.getParameterValues("dodatkowy")) {
            lista.add(new BasicDBObject(dodatkowy, request.getParameterValues("dodatkowy_wartosc")[i++]));
        }

    }
    rekord.append("dodatkowe", lista);
    collection.update(new BasicDBObject("_id", new ObjectId(request.getParameter("id"))), rekord);
    response.sendRedirect(request.getContextPath() + "/lista/osoba");
}

From source file:simple.crawler.mongo.CrawlingDB.java

License:Open Source License

public boolean update(CrawlingDBObject obj, Collection collection) {
    DB db = client.getDB(dbName);/*  www  .j a v  a 2s .  c  om*/
    DBCollection con = db.getCollection(collection.toString());
    BasicDBObject query = new BasicDBObject("_id", obj.getID());
    WriteResult result = con.update(query, obj);
    if (result.getError() == null) {
        return true;
    } else {
        LOG.error(result.getError());
        return false;
    }
}

From source file:spntoolsdata.crud.servispnCrud.java

public void addOrdenClient(MongoClient mongo, Cliente us, Orden or) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Clientes");
    DBObject query = new BasicDBObject();
    query.put("Codigo", us.getCodigo());
    DBObject update = new BasicDBObject();
    update.put("$push", new BasicDBObject("Ordenes", or.getDBObjectOrden()));
    DBCliente.update(query, update);

}

From source file:spntoolsdata.crud.servispnCrud.java

public void addCantonsProv(MongoClient mongo, Provincia prv, Canton cnt) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Provincias");
    DBObject query = new BasicDBObject();
    query.put("Provincia", prv.getProvincia());
    DBObject update = new BasicDBObject();
    update.put("$push", new BasicDBObject("Cantones", cnt.getDBObjectCanton()));
    DBCliente.update(query, update);

}

From source file:spntoolsdata.crud.servispnCrud.java

public void addHistoOrdenClient(MongoClient mongo, Cliente us, Orden or, Historial hi) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Clientes");
    DBObject query = new BasicDBObject();
    query.put("Codigo", us.getCodigo());
    query.put("Ordenes.CodigoOrden", or.getCodigoOrden());
    DBObject update = new BasicDBObject();
    update.put("$push", new BasicDBObject("Ordenes.$.Historial", hi.getDBObjectHistorial()));
    DBCliente.update(query, update);

}

From source file:spntoolsdata.crud.servispnCrud.java

public void addRepotoOrdenClient(MongoClient mongo, Cliente us, Orden or, Repuesto ri) {
    DB db = mongo.getDB("dbservispntools");
    DBCollection DBCliente = db.getCollection("Clientes");
    DBObject query = new BasicDBObject();
    query.put("Codigo", us.getCodigo());
    query.put("Ordenes.CodigoOrden", or.getCodigoOrden());
    DBObject update = new BasicDBObject();
    update.put("$push", new BasicDBObject("Ordenes.$.Repuestos", ri.getDBObjectRepuesto()));
    DBCliente.update(query, update);

}

From source file:tourapi.TourAPI.java

private void MLUpdate(String from, String to) {
    // ?//from  w  ww. ja v  a  2s  .  com
    //fromt->to 
    MongoClient mongoClient = getMongoClient();
    String result = null;
    try {
        DB db = mongoClient.getDB("my_database");
        DBCollection coll = db.getCollection("ML_Result");
        WriteConcern w = new WriteConcern(1, 0);
        mongoClient.setWriteConcern(w);

        DBObject doc = new BasicDBObject();
        // string?  select
        BasicDBObject query = new BasicDBObject();
        query.put("from", from);
        query.put("to", to);

        DBCursor cursor = coll.find(query);

        if (cursor.hasNext())// ?? 
        {
            DBObject mapObj = cursor.next();
            int num = ((Number) mapObj.get("num")).intValue();
            num++;
            BasicDBObject newDocument = new BasicDBObject();
            newDocument.append("$set", new BasicDBObject().append("num", num));
            BasicDBObject searchQuery = new BasicDBObject().append("from", from).append("to", to);
            coll.update(searchQuery, newDocument);
            cursor.close();
        } else {// ??  ? -> ?? insert
            doc.put("from", from);
            doc.put("to", to);
            doc.put("num", 1);
            coll.insert(doc);
            cursor.close();
        }
        System.out.println(coll.getCount());
        // close resources
        mongoClient.close();
    } catch (Exception e) {
        System.err.println("error evoke");
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:uk.ac.susx.tag.classificationframework.datastructures.CacheManager.java

License:Apache License

/**
 * Add a (hash, stringvalue) pair to the hashing index.
 * @param hashValue The hash of a pipelines tokeniser and docprocessor config.
 * @param stringValue The string value of the config
 * @param overwrite If true, and the hash collides with another hash (i.e. the string value differs), then overwrite the old
 *///from  w w  w .  java 2  s  . c o  m
private void addToHashingIndex(int hashValue, String stringValue, boolean overwrite) {
    DBCollection hashingIndex = client.getDB("cacheManagerMetadata").getCollection("hashingIndex");

    DBObject currentIndex = hashingIndex.findOne(new BasicDBObject("hashValue", hashValue));

    BasicDBObject hash = new BasicDBObject();
    hash.put("hashValue", hashValue);
    hash.put("stringValue", stringValue);

    if (currentIndex == null) { // No current index for this hash, so just add this one
        hashingIndex.insert(hash);
    } else if (!((String) currentIndex.get("stringValue")).equals(stringValue)) { // Problem! The hashes match, but the strings representations are different!
        if (overwrite) { // Overwrite the old
            hashingIndex.update(currentIndex, hash);
        } else {
            throw new CachingException(
                    "The hash configuration of this pipeline has been seen before, but their string representations are different.");
        }
    }
}

From source file:usermanager.Main.java

private void btnUpdateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateUserActionPerformed
    // TODO add your handling code here:
    User newUser = new User();
    newUser.setId(Integer.parseInt(txtId.getText()));
    newUser.setFirstName(txtFirstName.getText());
    newUser.setLastName(txtLastName.getText());
    newUser.setEmail(txtEmail.getText());

    // Search/*w w  w.j  av  a2  s . c  o  m*/
    BasicDBObject query = new BasicDBObject();
    query.put("_id", newUser.getId());

    // Set DB
    DB userDB = DBManager.getDatabase();
    DBCollection col = userDB.getCollection("user");

    // Update object
    DBObject doc = updateDBObject(newUser);

    /*
    // Method A
    // This will update all values, including all fields and values
    WriteResult result = col.update(query, doc);
    */

    // Method B
    // Update object only with the relevant fields using $set
    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", doc);
    WriteResult result = col.update(query, updateObj);
}

From source file:usermanager.Main2.java

private void btnUpdateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateUserActionPerformed
    // TODO add your handling code here:
    // Create user object
    User newUser = new User();
    newUser.setId(Integer.parseInt(txtId.getText()));
    newUser.setFirstName(txtFirstName.getText());
    newUser.setLastName(txtLastName.getText());
    newUser.setEmail(txtEmail.getText());

    // Get connection to the Collection
    DB userDB = DBManager.getDatabase();
    DBCollection col = userDB.getCollection("user");

    // Search/*from ww w.  j a  v  a  2 s.  co  m*/
    BasicDBObject query = new BasicDBObject();
    query.put("_id", newUser.getId());

    // Update values
    BasicDBObject doc = new BasicDBObject();
    doc.put("firstName", newUser.getFirstName());
    doc.put("lastName", newUser.getLastName());
    doc.put("email", newUser.getEmail());

    /* 
    // Method A
    // This will update all values, including all fields and values
    WriteResult result = col.update(query, doc);
    */

    //Method B
    // Update object only with the relevant fields using $set
    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", doc);
    WriteResult result = col.update(query, updateObj);

    System.out.println("Updated...");
}