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:mx.edu.cide.justiciacotidiana.v1.mongo.MongoInterface.java

License:Open Source License

/**
 * @param collectionName Nombre de la coleccin donde se actualizar el elemento.
 * @param query Objeto de consulta para recuperar el elemento a actualizar.
 * @param newData Objeto con los nuevos datos.
 * @return true si la insercin fue exitosa. false en otro caso.
 *//*from ww w  .jav  a  2s.  c  o  m*/
public boolean updateItem(String collectionName, BasicDBObject query, BasicDBObject newData) {
    DBCollection tCol = mongoDB.getCollection(collectionName);
    newData.put(FIELD_CREATED, query.get(FIELD_CREATED));
    newData.put(FIELD_UPDATED, new Date());

    //Eliminar id, si es que viene en el documento.
    newData.remove(FIELD_ID);

    try {
        tCol.update(query, newData);
    } catch (MongoException ex) {
        return false;
    }
    return true;
}

From source file:mx.org.cedn.avisosconagua.mongo.MongoInterface.java

License:Open Source License

/**
 * Flags te advice as succesfully generated (completed)
 * @param adviceID ID for the current advice
 * @param previous ID for the previous advice (if any)
 * @param title title of the advice/*from  w  w  w  .j av a2 s  . c om*/
 * @param type type of the advice. One of pacdp|atldp|pacht|atlht.
 * @param date issue date of the advice
 */
public void setGenerated(String adviceID, String previous, String title, String type, String date) {
    DBCollection col = mongoDB.getCollection(GENERATED_COL);
    String isodate = Utils.getOrderDate(date);
    BasicDBObject nuevo = new BasicDBObject(INTERNAL_FORM_ID, adviceID).append(GENERATED_TITLE, title)
            .append("previousIssue", previous).append("generationTime", Utils.sdf.format(new Date()))
            .append("adviceType", type).append("issueDate", isodate);
    BasicDBObject query = new BasicDBObject(INTERNAL_FORM_ID, adviceID);
    BasicDBObject old = (BasicDBObject) col.findOne(query);
    if (null == old) {
        col.insert(nuevo);
    } else {
        col.update(old, nuevo);
    }
}

From source file:mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java

License:Open Source License

public static void main(String[] arg) throws Exception {
    MongoClientURI mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL"));
    MongoClient mongoClient = new MongoClient(mongoClientURI);
    DB mongoDB = mongoClient.getDB(mongoClientURI.getDatabase());
    String GENERATED_COL = "GeneratedFiles";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd HH:mm");
    if (null != mongoClientURI.getUsername()) {
        mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword());
    }//from  w  w w . j  a  v  a 2  s .  c o m
    DBCollection col = mongoDB.getCollection(GENERATED_COL);
    DBCursor cursor = col.find();
    for (DBObject obj : cursor) {
        String date = (String) obj.get("issueDate");
        Date fec = null;
        try {
            fec = sdf.parse(date);
        } catch (ParseException npe) {

        }
        if (null != fec) {
            date = isoformater.format(fec);
            DBObject act = col.findOne(obj);
            obj.put("issueDate", date);
            col.update(act, obj);
        }
    }
}

From source file:mypackage.ManupulateData.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        String ukey = request.getParameter("ukey");
        String docname = request.getParameter("doc");
        String db = request.getParameter("db");
        String coll = request.getParameter("coll");

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB mongoDatabase = mongoClient.getDB(db);
        DBCollection dbcoll = mongoDatabase.getCollection(coll);
        if (ukey.equals("Update Document")) {
            String key = request.getParameter("dockey");
            String value = request.getParameter("docval");
            BasicDBObject basicDBObject = new BasicDBObject();
            basicDBObject.append("$set", new BasicDBObject().append(key, value));
            BasicDBObject serchQuery = new BasicDBObject("name", docname);
            dbcoll.update(serchQuery, basicDBObject);
            out.print("true");

        }//  w w  w .  j av  a2 s.  c o m
        if (ukey.equals("New Document")) {
            BasicDBObject bObject = new BasicDBObject("name", docname);
            dbcoll.insert(bObject);
            out.print("true");

        }

    }
}

From source file:net.autosauler.ballance.server.model.User.java

License:Apache License

/**
 * Trash user./*from   w ww. j  a  v  a2s  .c  om*/
 * 
 * @param loginanddomain
 *            the loginanddomain
 * @return true, if successful
 */
public static boolean trashUser(String login, String domain) {
    boolean result = false;
    Database.retain();
    DB db = Database.get(domain);
    if (db != null) {
        DBCollection coll = db.getCollection(USERSTABLE);

        BasicDBObject query = new BasicDBObject();
        query.put("login", login);
        query.put("domain", domain);

        BasicDBObject obj = new BasicDBObject();
        obj.put("istrash", true);
        obj.put("isactive", false);

        coll.update(query, new BasicDBObject("$set", obj));

        result = true;

    }
    Database.release();

    return result;
}

From source file:net.autosauler.ballance.server.model.User.java

License:Apache License

/**
 * Update user./*  w ww .ja  v  a  2  s. c  o  m*/
 * 
 * @param proxy
 *            the proxy
 * @return true, if successful
 */
public static boolean updateUser(net.autosauler.ballance.shared.User proxy) {
    boolean result = false;
    Database.retain();
    DB db = Database.get(proxy.getDomain());
    if (db != null) {
        DBCollection coll = db.getCollection(USERSTABLE);

        BasicDBObject query = new BasicDBObject();
        query.put("login", proxy.getLogin());
        query.put("domain", proxy.getDomain());

        BasicDBObject obj = new BasicDBObject();
        obj.put("isactive", proxy.isActive());
        obj.put("fullname", proxy.getUsername());
        obj.put("roles", proxy.getUserrole().getRole());
        String password = proxy.getPassword();
        if (password != null) {
            password = password.trim();
        }
        if (!password.isEmpty()) {
            obj.put("hash", genHash(password));
        }

        coll.update(query, new BasicDBObject("$set", obj));

        result = true;

    }
    Database.release();

    return result;
}

From source file:net.ion.framework.db.mongo.jdbc.Executor.java

License:Apache License

private int update(Update up) throws MongoSQLException {

    DBObject query = parseWhere(up.getWhere());

    BasicDBObject set = new BasicDBObject();

    for (int i = 0; i < up.getColumns().size(); i++) {
        String k = up.getColumns().get(i).toString();
        Expression v = (Expression) (up.getExpressions().get(i));
        set.put(k.toString(), toConstant(v));
    }//from w w  w  . j  ava  2  s . c o  m

    DBObject mod = new BasicDBObject("$set", set);

    DBCollection coll = getCollection(up.getTable());
    coll.update(query, mod);
    return 1; // TODO
}

From source file:net.onrc.openvirtex.db.DBManager.java

License:Apache License

/**
 * Remove persistable object obj//w  w  w  .ja  v  a  2 s .c  o  m
 * @param obj
 * @param coll
 */
public void remove(Persistable obj) {
    BasicDBObject query = new BasicDBObject();
    query.putAll(obj.getDBIndex());
    BasicDBObject update = new BasicDBObject("$pull", new BasicDBObject(obj.getDBKey(), obj.getDBObject()));
    PrintStream ps = System.err;
    System.setErr(null);
    try {
        DBCollection collection = this.collections.get(obj.getDBName());
        collection.update(query, update);
    } catch (Exception e) {
        log.error("Failed to remove from db: {}", e.getMessage());
    } finally {
        System.setErr(ps);
    }
}

From source file:net.onrc.openvirtex.db.DBManager.java

License:Apache License

/**
 * Remove all routes of switch for specified tenant
 *//*from   www.j ava2  s  .c o  m*/
public void removeSwitchPath(int tenantId, long switchId) {
    BasicDBObject query = new BasicDBObject();
    query.put(TenantHandler.TENANT, tenantId);
    BasicDBObject pull = new BasicDBObject("$pull",
            new BasicDBObject(SwitchRoute.DB_KEY, new BasicDBObject(TenantHandler.DPID, switchId)));
    PrintStream ps = System.err;
    System.setErr(null);
    try {
        DBCollection collection = this.collections.get(DB_VNET);
        collection.update(query, pull);
    } catch (Exception e) {
        log.error("Failed to remove from db: {}", e.getMessage());
    } finally {
        System.setErr(ps);
    }
}

From source file:net.onrc.openvirtex.db.DBManager.java

License:Apache License

/**
 * Remove stored path of vlink for specified tenant
 *//*w w w  . ja va 2 s  .c  o m*/
public void removeLinkPath(int tenantId, int linkId) {
    BasicDBObject query = new BasicDBObject();
    query.put(TenantHandler.TENANT, tenantId);
    BasicDBObject pull = new BasicDBObject("$pull",
            new BasicDBObject(OVXLink.DB_KEY, new BasicDBObject(TenantHandler.LINK, linkId)));
    PrintStream ps = System.err;
    System.setErr(null);
    try {
        DBCollection collection = this.collections.get(DB_VNET);
        collection.update(query, pull);
    } catch (Exception e) {
        log.error("Failed to remove from db: {}", e.getMessage());
    } finally {
        System.setErr(ps);
    }
}