Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.itfsw.query.builder.support.parser.mongodb.NotContainsRuleParser.java

License:Apache License

public BasicDBObject parse(IRule rule, JsonRuleParser parser) {
    BasicDBObject operate = new BasicDBObject();
    operate.append("$regex", Pattern.compile("^((?!" + rule.getValue() + ").)*$"));
    operate.append("$options", "s");
    return new BasicDBObject(rule.getField(), operate);
}

From source file:com.jagornet.dhcp.db.MongoLeaseManager.java

License:Open Source License

public void updateIaAddr(final IaAddress iaAddr) {
    DBObject query = ipAddressQuery(iaAddr.getIpAddress());
    BasicDBObject update = new BasicDBObject("state", iaAddr.getState());
    if (iaAddr instanceof IaPrefix) {
        update.append("prefixLength", ((IaPrefix) iaAddr).getPrefixLength());
    }//from  w w  w.j a  v a2s.co m
    update.append("startTime", iaAddr.getStartTime()).append("preferredEndTime", iaAddr.getPreferredEndTime())
            .append("validEndTime", iaAddr.getValidEndTime());

    dhcpLeases.update(query, new BasicDBObject("$set", update));
}

From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java

License:Apache License

private static BasicDBObject toDBObject(Map<String, Object> map) {
    BasicDBObject dbObject = new BasicDBObject();
    Set<Entry<String, Object>> entrySet = map.entrySet();
    for (Entry<String, Object> entry : entrySet) {
        String key = entry.getKey();
        Object val = entry.getValue();
        dbObject.append(key, val);
    }//  w  w  w  . j ava 2  s. c om
    return dbObject;
}

From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java

License:Apache License

private static BasicDBObject toDbObject(Record record) {
    BasicDBObject object = new BasicDBObject();
    for (Entry<String, Object> e : record.getColumns().entrySet()) {
        object.append(e.getKey(), e.getValue());
    }/*from www  . j a v  a 2  s  . c  om*/
    return object;
}

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

private void update(String columnName, String newValue, String mobileNumber) {
    try {//from   w  ww .  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.jjorgemoura.hangmanz.model.ZDHangmanGame.java

public void update() {

    if (this.dbEngine == null) {

        return;/*w  w w.  ja  v a  2  s .c o  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.jopss.apostas.db.migration.CadastroPerfil.java

@ChangeSet(order = "001", id = "002.001", author = "apostas")
public void criarPerfil(DB db) {

    DBCollection perfil = db.getCollection("perfil");
    DBCollection permissao = db.getCollection("permissao");

    BasicDBObject filtro = new BasicDBObject();
    filtro.put("papel", new BasicDBObject("$in", Arrays.asList("ROLE_GERAL", "ROLE_ADMIN")));

    List listaPermissoes = new ArrayList();
    DBObject ob1 = permissao.findOne(new BasicDBObject("papel", "ROLE_ADMIN"));
    listaPermissoes.add(new DBRef("permissao", ob1.get("_id")));
    DBObject ob2 = permissao.findOne(new BasicDBObject("papel", "ROLE_GERAL"));
    listaPermissoes.add(new DBRef("permissao", ob2.get("_id")));

    BasicDBObject admin = new BasicDBObject();
    admin.append("dataatualizacao", null);
    admin.append("datacriacao", new Date());
    admin.append("nome", "Perfil Administrador");
    admin.append("descricao", "Perfil com permisso geral.");
    admin.append("permissoes", listaPermissoes);
    perfil.insert(admin);/*w  w w.  j a v a  2s  .com*/

    BasicDBObject visitante = new BasicDBObject();
    visitante.append("dataatualizacao", null);
    visitante.append("datacriacao", new Date());
    visitante.append("nome", "Perfil Visitante");
    visitante.append("descricao", "Perfil sem permisso para gerenciar uma aposta.");
    perfil.insert(visitante);
}

From source file:com.kspichale.kundera.course.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem
    // instructions
    // will tell you the schema that the documents must follow.

    BasicDBObject user = new BasicDBObject();
    user.append("_id", username);
    user.append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // if there is an email address specified, add it to the document
        // too./*from   w ww  . j a v a 2s . co m*/
        user.append("email", email);
    }

    try {
        // insert the document into the user collection here
        usersCollection.insert(user);
        return true;
    } catch (MongoException.DuplicateKey e) {
        System.out.println("Username already in use: " + username);
        return false;
    }
}

From source file:com.linuxbox.util.dbmigration.AbstractColletionMigrator.java

License:Open Source License

private void migrateFields() throws DbMigrationException {
    // Remove all dead fields
    if (deadFields != null) {
        for (String deadField : deadFields) {
            collection.update(new BasicDBObject(), new BasicDBObject("$unset", new BasicDBObject(deadField, 1)),
                    false, true);/*from w ww .  jav  a 2  s . c  om*/
        }
    }

    // Rename changed fields
    if (renamedFields != null) {
        BasicDBObject rename = new BasicDBObject();
        for (Map.Entry<String, String> entry : renamedFields.entrySet()) {
            rename.append(entry.getKey(), entry.getValue());
        }
        collection.update(new BasicDBObject(), new BasicDBObject("$rename", rename), true, true);
    }
}

From source file:com.linuxbox.util.queueservice.mongodb.MongoQueueService.java

License:Open Source License

@Override
public QueueEntry dequeue(String identifer) throws QueueServiceException {
    final BasicDBObject query = new BasicDBObject();
    query.putAll(QUERY_ENQUEUED_STATUS);
    query.append(IDENTIFIER_FIELD, identifer);
    return dequeueHelper(query);
}