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:io.github.apfelcreme.LitePortals.Bungee.Database.MongoController.java

License:Open Source License

/**
 * locks a portal and makes it available to only the builder
 *
 * @param portal a portal//  www .j  a v a 2 s .c  o  m
 */
@Override
public void lock(Portal portal) {
    DBCollection collection = MongoConnector.getInstance().getCollection();
    BasicDBObject query = new BasicDBObject("portal_id", portal.getId().toString());
    DBCursor dbCursor = collection.find(query);
    if (dbCursor.hasNext()) {
        DBObject portalObject = dbCursor.next();
        portalObject.put("locked", true);
        portal.setLocked(true);
        collection.update(query, portalObject);
    }
}

From source file:io.github.apfelcreme.LitePortals.Bungee.Database.MongoController.java

License:Open Source License

/**
 * unlocks a portal and makes it available to all players
 *
 * @param portal a portal/*from ww w. j  ava 2s.  co  m*/
 */
@Override
public void unlock(Portal portal) {
    DBCollection collection = MongoConnector.getInstance().getCollection();
    BasicDBObject query = new BasicDBObject("portal_id", portal.getId().toString());
    DBCursor dbCursor = collection.find(query);
    if (dbCursor.hasNext()) {
        DBObject portalObject = dbCursor.next();
        portalObject.put("locked", false);
        portal.setLocked(false);
        collection.update(query, portalObject);
    }
}

From source file:io.github.apfelcreme.LitePortals.Bungee.Database.MongoController.java

License:Open Source License

/**
 * disabled all portals on a given world
 *
 * @param worldName the name of the world
 * @param server    the server the sender is on
 * @return the number of portals that were disabled
 */// ww w .  j a v  a2 s.  c o m
public int disableWorld(String worldName, Server server) {
    DBCollection collection = MongoConnector.getInstance().getCollection();
    BasicDBObject query = new BasicDBObject("world", worldName);
    query.append("server", server.getAddress().getHostName() + "." + server.getAddress().getPort());
    query.append("enabled", true);
    DBCursor dbCursor = collection.find(query);
    int i = 0;
    while (dbCursor.hasNext()) {
        i++;
        DBObject portalObject = dbCursor.next();
        portalObject.put("enabled", false);
        PortalManager.getInstance().removePortal(UUID.fromString(portalObject.get("portal_id").toString()));
        collection.update(query, portalObject);
    }
    return i;
}

From source file:io.github.apfelcreme.LitePortals.Bungee.Database.MongoController.java

License:Open Source License

/**
 * enables all portals on a given world if they are disabled
 *
 * @param worldName the name of the world
 * @param server    the server the sender is on
 * @return the number of portals that were disabled
 *///from   ww w .  j  a v a2  s  .  com
public int enableWorld(String worldName, Server server) {
    DBCollection collection = MongoConnector.getInstance().getCollection();
    BasicDBObject query = new BasicDBObject("world", worldName);
    query.append("server", server.getAddress().getHostName() + "." + server.getAddress().getPort());
    query.append("enabled", false);
    DBCursor dbCursor = collection.find(query);
    int i = 0;
    while (dbCursor.hasNext()) {
        i++;
        DBObject portalObject = dbCursor.next();
        portalObject.put("enabled", true);
        collection.update(query, portalObject);
        PortalManager.getInstance().getPortals().add(createPortal(portalObject));
    }
    return i;
}

From source file:javaapplication1.InsertUser.java

public static void main(String[] args) {

    MongoClient mongo = new MongoClient("localhost", 27017);

    DB db = mongo.getDB("PaperTree");

    DBCollection user = db.getCollection("User");
    Document d = new Document();
    d.put("Name", new Document().append("First Name", "Akshay").append("Last Name", "Chandila"));
    d.put("Email", "akki@gmail.com");
    d.put("Interests", "Computer Science");
    d.put("Password", "shaolinp");
    d.put("Security Question", "a?");
    d.put("Security Answer", "b");
    DBObject ob = new BasicDBObject(d);
    user.insert(ob);//  w  w w .  j av  a 2  s  .  co  m

    DBObject ob1 = new BasicDBObject(new Document("Password", "shaolinp"));
    DBObject ob2 = new BasicDBObject(new Document("$set", new Document("Security Answer", "Akshay1234")));
    user.update(ob1, ob2);
    String tm = "shaolghjmkl;inp";
    DBObject ob3 = new BasicDBObject(new Document("Password", tm));
    DBCursor c2 = user.find(ob3);
    System.out.println(c2.count());
}

From source file:model.DocumentoDAO.java

public static void atualizarDocumento(Documento d) {
    Documento old = buscarPorNome(d.getNome());

    DB db = ConnectionFactory.create();/*from w  w w  . j  a v a 2 s .c om*/
    DBCollection coll = db.getCollection("documentos");
    coll.update(old.toJSON(), d.toJSON());
}

From source file:models.datasource.SingletonDataSource.java

public static void updateAllUserData(User user) {
    DBCollection collection = connectDB("mongo.usersCollection");
    BasicDBObject newDocument = new BasicDBObject();

    newDocument.put(Constants.USER_NAME, user.name);
    newDocument.put(Constants.USER_SURNAMES, user.surnames);
    newDocument.put(Constants.USER_EMAIL, user.email);
    newDocument.put(Constants.USER_PASSWORD, user.password);
    newDocument.put(Constants.USER_EMAIL_VERIFICATION_KEY, user.emailVerificationKey);
    newDocument.put(Constants.USER_CONNECTION_TIMESTAMP, user.connectionTimestamp);
    newDocument.put(Constants.USER_RESTORE_PASSWORD_TOKEN, user.restorePasswordToken);
    newDocument.put(Constants.USER_RESTORE_PASSWORD_TIMESTAMP, user.restorePasswordTimestamp);
    newDocument.put(Constants.USER_REGISTRATION_DATE, user.registrationDate);
    newDocument.put(Constants.USER_BIRTH_DATE, user.birthDate);
    newDocument.put(Constants.USER_RESIDENCE_CITY, user.residenceCity);
    newDocument.put(Constants.USER_RESIDENCE_ADDRESS, user.residenceAddress);
    newDocument.put(Constants.USER_RESIDENCE_NUMBER, user.residenceNumber);
    newDocument.put(Constants.USER_RESIDENCE_ZIP_CODE, user.residenceZipCode);
    newDocument.put(Constants.USER_PHONE_NUMBER, user.phoneNumber);
    newDocument.put(Constants.USER_STUDY_TITLE, user.studyTitle);
    newDocument.put(Constants.USER_STUDY_LOCATION, user.studyLocation);
    newDocument.put(Constants.USER_EDUCATION_LEVEL, user.educationLevel);
    newDocument.put(Constants.USER_DRIVING_LICENSE, user.drivingLicense);
    newDocument.put(Constants.USER_CERTIFICATE_OF_DISABILITY, user.certificateOfDisability);
    newDocument.put(Constants.USER_COURSES, JSON.parse(user.coursesToJson()));
    newDocument.put(Constants.USER_LANGUAGES, JSON.parse(user.languagesToJson()));
    newDocument.put(Constants.USER_SOFTWARE, JSON.parse(user.softwareToJson()));
    newDocument.put(Constants.USER_ORIENTATION_STEPS,
            JSON.parse(user.completedOrientationSteps.orientationStepsToJson()));
    newDocument.put(Constants.USER_CURRENT_SITUATION, JSON.parse(user.currentSituation.toJsonString()));
    newDocument.put(Constants.USER_SKILLS_LIST, JSON.parse(user.skillsToJson()));
    newDocument.put(Constants.USER_INTERESTS_LIST, user.interests);
    newDocument.put(Constants.USER_PERSONAL_CHARACTERISTICS_LIST, user.personalCharacteristics);
    newDocument.put(Constants.USER_PROFESSIONAL_VALUES_LIST, JSON.parse(user.professionalValuesToJson()));
    newDocument.put(Constants.USER_PHOTO, JSON.parse(user.photo.toJsonString()));
    newDocument.put(Constants.USER_NEXT_INTERVIEWS_LIST, JSON.parse(user.interviewScheduleListToJson()));

    collection.update(new BasicDBObject().append(Constants.USER_REGISTRATION_DATE, user.registrationDate),
            newDocument);//from w w  w.  j  ava2  s  . c  om

    mongoClient.close();
}

From source file:models.datasource.SingletonDataSource.java

public static void updateUserData(String email, String key, String newValue) {
    DBCollection collection = connectDB("mongo.usersCollection");
    BasicDBObject query = new BasicDBObject().append("email", email);
    DBObject user = collection.findOne(query);

    if (user != null) {
        BasicDBObject updateQuery = new BasicDBObject().append("$set",
                new BasicDBObject().append(key, newValue));
        collection.update(query, updateQuery);
    }/*from  ww  w .  j  a  v a 2s.c  o  m*/
    mongoClient.close();
}

From source file:mongodb.MongoDBOperations.java

License:Apache License

public void geoFindAndUpdate(DBCollection coll, BasicDBObject queryObject) {
    int query_count = 0;
    int processed_count = 0;
    int updated_count = 0;
    DBCursor cursor = coll.find(queryObject);
    try {// w ww  .  jav  a  2 s  .c om
        query_count = cursor.count();
        while (cursor.hasNext()) {
            BasicDBObject myDoc = (BasicDBObject) cursor.next();
            Object userLocationObj = myDoc.get("user_location");
            String location = String.valueOf(userLocationObj);

            ObjectId ID = (ObjectId) myDoc.get("_id");
            System.out.println("Queried Document: " + myDoc);

            GeoCodingClient gcc = new GeoCodingClient(this.conf);
            Thread.sleep(200);
            DBObject update = gcc.getGeoCode(location);
            //System.out.println(update);

            if (update == null) {
                System.out.print("No updates (update==null)");
                break;
            } else {
                Set<String> keyset = update.keySet();
                if (keyset == null) {
                    System.out.print("No updates (keyset==null)");
                } else if (keyset.contains("geocode")) {
                    updated_count++;
                    coll.update(new BasicDBObject().append("_id", ID),
                            myDoc.append("geocode", update.get("geocode")));
                    System.out.println(
                            "Updated Doc:\n" + coll.find(new BasicDBObject().append("_id", ID)).next());
                    System.out.println("In this run: (" + processed_count + " records processed, "
                            + updated_count + " records updated.)");
                } else if (keyset.contains("status")
                        && update.get("status").toString().contains("OVER_QUERY_LIMIT")) {
                    break;
                }
            }
            processed_count++;
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {
        cursor.close();
        System.out.println("In this run:");
        System.out.println("Total:" + query_count + " record(s) found.");
        System.out.println("Total:" + processed_count + " record(s) processed.");
        System.out.println("Total:" + updated_count + " record(s) updated.");
    }

}

From source file:mongoHandler.Handler.java

public boolean editarDocumento(BasicDBObject documentoAEditar, BasicDBObject nuevoDocumento,
        String nombreColeccion) {
    try {/*ww w  .j a va 2s.com*/
        DBCollection coleccion = dataBase.getCollection(nombreColeccion);
        coleccion.update(documentoAEditar, nuevoDocumento);
        System.out.println("DOCUMENTO EDITADO");
        return true;
    } catch (Exception e) {
        System.out.println("NO SE PUDO EDITAR EL DOCUMENTO : " + e.getMessage());
        return false;
    }
}