Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private double min(String key, DBObject query) {
    if (!this.exists(query)) {
        return 0;
    }/*  w w w.j  ava  2s . co  m*/
    StringBuilder map = new StringBuilder("function(){emit('");
    map.append(key);
    map.append("', {'value':this.");
    map.append(key);
    map.append("});}");
    String reduce = "function(key, values){var min=values[0].value; for(var i=1;i<values.length; i++){if(values[i].value<min){min=values[i].value;}} return {'value':min}}";
    Iterable<DBObject> results = null;
    try {
        results = mapReduce(map.toString(), reduce, query);
    } catch (MapReduceException ex) {
        logger.error(ex.getMessage(), ex);
    }
    DBObject result = results.iterator().next();
    DBObject dbo = (DBObject) result.get("value");
    return Double.parseDouble(dbo.get("value").toString());
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private double sum(String key, DBObject query) {
    if (!this.exists(query)) {
        return 0;
    }/* ww  w.  ja  va 2  s  .  com*/
    StringBuilder map = new StringBuilder("function(){emit('");
    map.append(key);
    map.append("', {'value':this.");
    map.append(key);
    map.append("});}");
    String reduce = "function(key, values){var sum=0; for(var i=0;i<values.length; i++){sum+=values[i].value;} return {'value':sum}}";
    Iterable<DBObject> results = null;
    try {
        results = mapReduce(map.toString(), reduce, query);
    } catch (MapReduceException ex) {
        logger.error(ex.getMessage(), ex);
    }
    DBObject result = results.iterator().next();
    DBObject dbo = (DBObject) result.get("value");
    return Double.parseDouble(dbo.get("value").toString());
}

From source file:com.bugull.mongo.BuguDao.java

License:Apache License

/**
 * Insert an entity to mongoDB./*from w w w  .j  a  va  2 s  . c o  m*/
 * @param t
 * @return 
 */
public WriteResult insert(T t) {
    DBObject dbo = MapperUtil.toDBObject(t);
    WriteResult wr = coll.insert(dbo, concern);
    String id = dbo.get(Operator.ID).toString();
    BuguEntity ent = (BuguEntity) t;
    ent.setId(id);
    if (luceneListener != null) {
        luceneListener.entityInsert(ent);
    }
    return wr;
}

From source file:com.bugull.mongo.decoder.EmbedDecoder.java

License:Apache License

public EmbedDecoder(Field field, DBObject dbo) {
    super(field);
    String fieldName = field.getName();
    Embed embed = field.getAnnotation(Embed.class);
    String name = embed.name();/*from   www. j  av  a  2s .  co m*/
    if (!name.equals(Default.NAME)) {
        fieldName = name;
    }
    value = dbo.get(fieldName);
}

From source file:com.bugull.mongo.decoder.EmbedListDecoder.java

License:Apache License

public EmbedListDecoder(Field field, DBObject dbo) {
    super(field);
    String fieldName = field.getName();
    EmbedList embedList = field.getAnnotation(EmbedList.class);
    String name = embedList.name();
    if (!name.equals(Default.NAME)) {
        fieldName = name;//  w  w w.  java  2  s  .c om
    }
    value = dbo.get(fieldName);
}

From source file:com.bugull.mongo.decoder.IdDecoder.java

License:Apache License

public IdDecoder(Field field, DBObject dbo) {
    super(field);
    value = dbo.get(Operator.ID);
}

From source file:com.bugull.mongo.decoder.PropertyDecoder.java

License:Apache License

public PropertyDecoder(Field field, DBObject dbo) {
    super(field);
    String fieldName = field.getName();
    Property property = field.getAnnotation(Property.class);
    if (property != null) {
        String name = property.name();
        if (!name.equals(Default.NAME)) {
            fieldName = name;//from w ww  .java2s .co  m
        }
    }
    value = dbo.get(fieldName);
}

From source file:com.bugull.mongo.decoder.RefDecoder.java

License:Apache License

public RefDecoder(Field field, DBObject dbo) {
    super(field);
    ref = field.getAnnotation(Ref.class);
    String fieldName = field.getName();
    String name = ref.name();//  w  w w  .j a  v  a  2  s  . co m
    if (!name.equals(Default.NAME)) {
        fieldName = name;
    }
    value = dbo.get(fieldName);
}

From source file:com.bugull.mongo.decoder.RefListDecoder.java

License:Apache License

public RefListDecoder(Field field, DBObject dbo) {
    super(field);
    refList = field.getAnnotation(RefList.class);
    String fieldName = field.getName();
    String name = refList.name();
    if (!name.equals(Default.NAME)) {
        fieldName = name;/*  w  w w .  j  av a 2 s .  c  o m*/
    }
    value = dbo.get(fieldName);
}

From source file:com.buzz.buzzdata.MongoBuzz.java

private String getBuzz(BasicDBObject query, Double lat, Double lng) {
    String retval = "";
    DBCollection buzzCollection = mongoDB.getCollection("BuzzInfo");
    DBObject sort = new BasicDBObject();
    sort.put("modified", -1);
    DBCursor cursor = buzzCollection.find(query).sort(sort);
    try {/*from   w  w  w.j  a  va 2  s .co m*/
        while (cursor.hasNext()) {
            //get buzzid
            DBObject buzz_obj = cursor.next();
            ObjectId buzz_id = (ObjectId) buzz_obj.get("_id");
            //get images for buzzid
            GridFS gridFS = new GridFS(mongoDB);
            BasicDBObject check_images = new BasicDBObject("BuzzID", buzz_id);
            List<GridFSDBFile> dbfiles = gridFS.find(check_images);
            String image_links = "";
            for (GridFSDBFile file : dbfiles) {
                String _buzz_id = buzz_id.toString();
                String pic_num = file.get("PicNum").toString();

                if (!image_links.equals("")) {
                    image_links += ",http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                } else {
                    image_links += "http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                }
            }
            String imgs = "\"Images\": " + "\"" + image_links + "\"";
            retval += buzz_obj;
            retval = retval.substring(0, retval.length() - 1);
            retval += ", " + imgs;
            double lat2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(0);
            double lng2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(1);
            double dist = this.haversine(lat, lng, lat2, lng2);
            retval += ", \"Distance\": " + dist;
            String directions_url = "https://maps.google.com/maps?saddr=" + lat + "," + lng + "&daddr=" + lat2
                    + "," + lng2 + "&hl=en&sll=" + lat + "," + lng + "&sspn=" + lat2 + "," + lng2
                    + "&t=m&mra=mift&mrsp=1&sz=5&z=18";
            retval += ", \"Directions\": \"" + directions_url + "\"";
            retval += "},";
        }
    } catch (Exception exc) {
    } finally {
        cursor.close();
    }
    retval = retval.substring(0, retval.length() - 1);
    retval = "callback({\"Buzzes\": [" + retval + "]})";
    return retval;
}