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.mycompany.Farmerama.getAllAccounts.java

public String getProfileImage(String user) {
    String profileImage = "";
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.append("user", user);
    DBCursor cursor = account.find(searchQuery);
    while (cursor.hasNext()) {
        profileImage = cursor.next().get("profileImage").toString();
    }//from w w  w.j a v a2s  . c  o  m

    return profileImage;

}

From source file:com.mycompany.Farmerama.getAllAccounts.java

public ArrayList<String> getSearchedAccounts(String inputedS) {
    ArrayList<String> allFoundUsers = new ArrayList<String>();
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.append("user", new BasicDBObject("$regex", inputedS));
    DBCursor cursor = account.find(searchQuery);
    while (cursor.hasNext()) {
        allFoundUsers.add(cursor.next().get("user").toString());
    }//from   ww w .jav  a 2 s.c  o m
    return allFoundUsers;
}

From source file:com.mycompany.Farmerama.getAllAccounts.java

public HashMap<String, String> getSearchedAccountsByNumber(String inputedS) {
    HashMap<String, String> allFoundUsersByNumber = new HashMap<String, String>();
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.append("number", new BasicDBObject("$regex", inputedS));
    DBCursor cursor = account.find(searchQuery);
    while (cursor.hasNext()) {
        allFoundUsersByNumber.put(cursor.next().get("number").toString(), cursor.curr().get("user").toString());
    }//ww  w .j  a v a 2s.  c o m
    return allFoundUsersByNumber;
}

From source file:com.mycompany.model.MarkerDAOImpl.java

@Override
public void saveOrUpdate(Marker marker) {
    DBCollection markerCollection = MongoConfig.getMongoConnection().getCollection("markers");
    BasicDBObject document = new BasicDBObject();
    document.put("lat", marker.getLat());
    document.put("lng", marker.getLng());
    document.put("title", marker.getTitle());
    document.put("icon", marker.getIcon());
    document.put("content", marker.getContent());

    if (marker.getId().length() > 0) {

        BasicDBObject query = new BasicDBObject();
        query.append("_id", new ObjectId(marker.getId()));
        markerCollection.update(query, document);
    } else {//www . j  a v  a2s .com
        markerCollection.insert(document);
    }
}

From source file:com.mycompany.model.MarkerDAOImpl.java

@Override
public void delete(String markerId) {

    DBCollection markerCollection = MongoConfig.getMongoConnection().getCollection("markers");
    BasicDBObject query = new BasicDBObject();
    query.append("_id", new ObjectId(markerId));
    markerCollection.remove(query);//from w w  w.j a  v  a2  s.c  o m

}

From source file:com.mysema.query.mongodb.MongodbSerializer.java

License:Apache License

public DBObject toSort(List<OrderSpecifier<?>> orderBys) {
    BasicDBObject sort = new BasicDBObject();
    for (OrderSpecifier<?> orderBy : orderBys) {
        Object key = orderBy.getTarget().accept(this, null);
        sort.append(key.toString(), orderBy.getOrder() == Order.ASC ? 1 : -1);
    }//from   w  w w  . j  a v  a2 s .  c  o m
    return sort;
}

From source file:com.mysema.query.mongodb.MongodbSerializer.java

License:Apache License

@Override
public Object visit(Operation<?> expr, Void context) {
    Operator<?> op = expr.getOperator();
    if (op == Ops.EQ) {
        return asDBObject(asDBKey(expr, 0), asDBValue(expr, 1));

    } else if (op == Ops.STRING_IS_EMPTY) {
        return asDBObject(asDBKey(expr, 0), "");

    } else if (op == Ops.AND) {
        BasicDBObject left = (BasicDBObject) handle(expr.getArg(0));
        left.putAll((BSONObject) handle(expr.getArg(1)));
        return left;

    } else if (op == Ops.NOT) {
        //Handle the not's child
        BasicDBObject arg = (BasicDBObject) handle(expr.getArg(0));

        //Only support the first key, let's see if there
        //is cases where this will get broken
        String key = arg.keySet().iterator().next();

        Operator<?> subOp = ((Operation<?>) expr.getArg(0)).getOperator();
        if (subOp != Ops.EQ && subOp != Ops.STRING_IS_EMPTY) {
            return asDBObject(key, asDBObject("$not", arg.get(key)));
        } else {/*from   w w w .ja  v a  2 s .co m*/
            return asDBObject(key, asDBObject("$ne", arg.get(key)));
        }

    } else if (op == Ops.OR) {
        BasicDBList list = new BasicDBList();
        list.add(handle(expr.getArg(0)));
        list.add(handle(expr.getArg(1)));
        return asDBObject("$or", list);

    } else if (op == Ops.NE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$ne", asDBValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile("^" + regexValue(expr, 1), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.ENDS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$"));

    } else if (op == Ops.ENDS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.EQ_IGNORE_CASE) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile("^" + regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.STRING_CONTAINS) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*"));

    } else if (op == Ops.STRING_CONTAINS_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(".*" + regexValue(expr, 1) + ".*", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.MATCHES) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString()));

    } else if (op == Ops.MATCHES_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(asDBValue(expr, 1).toString(), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.LIKE) {
        String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regex));

    } else if (op == Ops.BETWEEN) {
        BasicDBObject value = new BasicDBObject("$gte", asDBValue(expr, 1));
        value.append("$lte", asDBValue(expr, 2));
        return asDBObject(asDBKey(expr, 0), value);

    } else if (op == Ops.IN) {
        int constIndex = 0;
        int exprIndex = 1;
        if (expr.getArg(1) instanceof Constant<?>) {
            constIndex = 1;
            exprIndex = 0;
        }
        if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
            Collection<?> values = (Collection<?>) ((Constant<?>) expr.getArg(constIndex)).getConstant();
            return asDBObject(asDBKey(expr, exprIndex), asDBObject("$in", values.toArray()));
        } else {
            return asDBObject(asDBKey(expr, exprIndex), asDBValue(expr, constIndex));
        }

    } else if (op == Ops.LT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lt", asDBValue(expr, 1)));

    } else if (op == Ops.GT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gt", asDBValue(expr, 1)));

    } else if (op == Ops.LOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lte", asDBValue(expr, 1)));

    } else if (op == Ops.GOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gte", asDBValue(expr, 1)));

    } else if (op == Ops.IS_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", false));

    } else if (op == Ops.IS_NOT_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", true));

    } else if (op == MongodbOps.NEAR) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$near", asDBValue(expr, 1)));

    } else if (op == MongodbOps.ELEM_MATCH) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$elemMatch", asDBValue(expr, 1)));
    }

    throw new UnsupportedOperationException("Illegal operation " + expr);
}

From source file:com.nec.strudel.tkvs.store.mongodb.MongodbTransaction.java

License:Apache License

@Override
public boolean commit() {
    prof.commitStart(gName);/* w  ww .ja  va 2s  .c  o m*/
    BasicDBObject setdoc = new BasicDBObject();
    BasicDBObject unsetdoc = new BasicDBObject();
    for (CollectionBuffer b : buffers()) {
        Map<Key, Record> writes = b.getWrites();
        String name = b.getName();
        for (Map.Entry<Key, Record> e : writes.entrySet()) {
            Key key = e.getKey();
            Record r = e.getValue();
            //for put
            if (r != null) {
                setdoc.append(key.toStringKey(name), r.toBytes());
            } else { // for delete
                unsetdoc.append(key.toStringKey(name), "");
            }
        }
    }
    if (setdoc.isEmpty() && unsetdoc.isEmpty()) {
        //read only
        long newvnum = getVnum(coll, docName);
        if (this.vnum == newvnum) {
            prof.commitSuccess(gName);
            return true;
        } else {
            prof.commitFail(gName);
            return false;
        }
    } else {
        setdoc.append(MongoDbServer.VERSIONFIELD, vnum + 1);
        WriteResult res = null;
        if (vnum == 0) {
            //upsert if this docName is not in the db yet,
            //else we should receive a duplicateKey exception, meaning
            //this docName is inserted by an other transaction, then fail it
            if (unsetdoc.isEmpty()) {
                res = insert(new BasicDBObject(MongoDbServer.DOCNAME, docName),
                        new BasicDBObject("$set", setdoc));
            } else {
                res = insert(new BasicDBObject(MongoDbServer.DOCNAME, docName),
                        new BasicDBObject("$set", setdoc).append("$unset", unsetdoc));
            }
            if (res == null) {
                prof.commitFail(gName);
                return false;
            }
            prof.commitSuccess(gName);
            return true;
        } else {
            if (unsetdoc.isEmpty()) {
                res = update(new BasicDBObject(MongoDbServer.DOCNAME, docName)
                        .append(MongoDbServer.VERSIONFIELD, this.vnum), new BasicDBObject("$set", setdoc));
            } else {
                res = update(new BasicDBObject(MongoDbServer.DOCNAME, docName)
                        .append(MongoDbServer.VERSIONFIELD, this.vnum),
                        new BasicDBObject("$set", setdoc).append("$unset", unsetdoc));
            }
            if (res.isUpdateOfExisting()) {
                prof.commitSuccess(gName);
                return true;
            } else {
                prof.commitFail(gName);
                return false;
            }
        }
    }
}

From source file:com.norconex.collector.core.data.store.impl.mongo.BaseMongoSerializer.java

License:Apache License

protected final void ensureIndex(DBCollection coll, boolean unique, String... fields) {
    BasicDBObject fieldsObject = new BasicDBObject();
    for (String field : fields) {
        fieldsObject.append(field, 1);
    }/*from w w w . ja v  a2  s  .c o  m*/
    coll.createIndex(fieldsObject, null, unique);
}

From source file:com.openbravo.data.loader.MongoDBBatchSentence.java

/**
 *
 * @param params/*www . j  a v  a  2  s.  c  o  m*/
 * @return
 * @throws BasicException
 */
@Override
public DataResultSet openExec(Object params) throws BasicException {
    try {
        BufferedReader br = new BufferedReader(getReader());

        String sLine;
        while ((sLine = br.readLine()) != null) {
            sLine = sLine.trim();
            if (!sLine.equals("") && !sLine.startsWith("--")) {

                String collection = sLine.split(";")[0].split("=")[1];
                String insertStatement = sLine.split(";")[1].replace('{', ' ').replace('}', ' ');
                insertStatement = insertStatement.trim();

                String[] mapSplit = insertStatement.split(",");
                BasicDBObject insertObject = new BasicDBObject();
                for (int i = 0; i < mapSplit.length; ++i) {
                    String[] keyValue = mapSplit[i].split(":");
                    if (keyValue[1].contains("$APP_ID"))
                        keyValue[1] = m_parameters.get("APP_ID");
                    else if (keyValue[1].contains("$APP_NAME"))
                        keyValue[1] = m_parameters.get("APP_NAME");
                    else if (keyValue[1].contains("$APP_VERSION"))
                        keyValue[1] = m_parameters.get("APP_VERSION");
                    if (keyValue[1].contains("$FILE")) {
                        String filepath = keyValue[1].trim().substring(6);
                        insertObject.append(keyValue[0].trim(), ImageUtils.getBytesFromResource(filepath));
                    } else {
                        insertObject.append(keyValue[0].trim(), keyValue[1].trim());
                    }
                }
                DBCollection dbCollection = m_s.getMongoDBDatabase().getCollection(collection);
                dbCollection.insert(insertObject);
            }
        }
    } catch (IOException ex) {

    }
    return new ExceptionsResultSet(new ArrayList());
}