Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

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

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

Usage

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 {//from  w  w  w .  j  a  v  a2s . c  o m
        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:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java

License:Open Source License

public List<Object> readMongoChunksGrp(Mongo mongo, String collName, List<Object> dboLst)
        throws MongoException, Exception {
    try {//from w w  w . ja  v  a  2  s . co  m
        DB db = mongo.getDB("config");
        DBCollection dbcollection = db.getCollectionFromString("chunks");
        DBObject dbo = dbcollection.group(new BasicDBObject("shard", true), new BasicDBObject("ns", collName),
                new BasicDBObject("nChunks", 0), "function (doc, out) {out.nChunks++;}");
        Iterator<String> keys = dbo.keySet().iterator();
        BasicDBObject getDbo;
        while (keys.hasNext()) {
            String key = keys.next();
            getDbo = (BasicDBObject) dbo.get(key);
            getDbo.put("collName", collName);
            dboLst.add(getDbo);
        }
    } catch (DataAccessResourceFailureException e) {
        e.printStackTrace();
        throw new MongoException(e.getMessage());
    } catch (MongoException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {

    }
    return dboLst;
}

From source file:net.joinedminds.tools.evet.DbEvent.java

License:Open Source License

public DbEvent(DBObject db) {
    map = new HashMap<>();
    this.start = (Date) db.get(START);
    map.put(START, start);/*from   ww w  .  ja  v a2 s .  com*/
    this.end = (Date) db.get(END);
    map.put(END, end);
    this.durationEvent = (Boolean) db.get(DURATION_EVENT);
    map.put(DURATION_EVENT, durationEvent);
    this.title = (String) db.get(TITLE);
    map.put(TITLE, title);
    this.classname = (String) db.get(CLASSNAME);
    map.put(CLASSNAME, classname);
    this.color = (String) db.get(COLOR);
    this.description = (String) db.get(DESCRIPTION);
    map.put(DESCRIPTION, description);
    this.link = (String) db.get(LINK);
    this.id = db.get(ID).toString();
    map.put("id", id);
    this.system = (String) db.get(SYSTEM);
    map.put(SYSTEM, system);
    BasicDBList tags = (BasicDBList) db.get(TAGS);
    if (tags != null) {
        this.tags = tags.toArray(new String[tags.size()]);
    }
    map.put(TAGS, tags);
    this.node = (String) db.get(NODE);
    map.put(NODE, node);
    this.caption = (String) db.get(CAPTION);
    map.put(CAPTION, caption);

    //Extra properties.
    for (String key : db.keySet()) {
        if (!Db.RESERVED_NAMES.contains(key)) {
            Object o = db.get(key);
            if (o != null) {
                map.put(key, o.toString());
            }
        }
    }

    if (end == null && durationEvent) {
        map.put(END, new Date());
        map.put("tapeImage", Functions.getSafeRootUrl() + "/img/gray-striped.png");
        map.put("tapeRepeat", "repeat-x");
    }
}

From source file:net.luminis.useradmin.mongodb.MongoSerializerHelper.java

License:Apache License

/**
 * Deserializes the given {@link DBObject} into the given {@link Dictionary}.
 * /*  w  w w.  j a  v a 2 s .co m*/
 * @param dictionary the dictionary to fill;
 * @param object the {@link DBObject} to deserialize.
 */
private void deserializeDictionary(Dictionary<String, Object> dictionary, DBObject object) {
    for (String key : object.keySet()) {
        dictionary.put(decodeKey(key), object.get(key));
    }
}

From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java

License:Apache License

@Override
public Map<String, String> getUserData(String survey_id, String user_id) throws DataStoreException {
    DBCollection usersCollection = getUsersCollection(survey_id);

    DBCursor find = usersCollection.find(new BasicDBObject(FIELD_USERNAME, user_id));

    if (!find.hasNext())
        throw new DataStoreException("No user record for " + user_id + " in " + survey_id);

    DBObject userData = (DBObject) find.next().get(FIELD_USERDATA);

    HashMap<String, String> result = new HashMap<String, String>();

    for (String k : userData.keySet())
        result.put(k, (String) userData.get(k));

    return result;
}

From source file:net.scran24.datastore.mongodb.MongoDbDeserializer.java

License:Apache License

private Map<Long, Double> parseNutrients(DBObject obj) {
    HashMap<Long, Double> result = new HashMap<Long, Double>();
    for (String k : obj.keySet())
        result.put(LegacyNutrientTypes.legacyKeyToId.get(k), (Double) obj.get(k));
    return result;
}

From source file:net.scran24.datastore.mongodb.MongoDbDeserializer.java

License:Apache License

private Map<String, String> parseData(DBObject dbObject) {
    HashMap<String, String> result = new HashMap<String, String>();

    // FIXME: this is a hack to support legacy database entries, should not
    // be here/*from   w  ww . j a  va 2s .com*/
    if (dbObject == null)
        return result;

    for (String k : dbObject.keySet()) {
        result.put(k, dbObject.get(k).toString());
    }

    return result;
}

From source file:net.tbnr.gearz.activerecord.GModel.java

License:Open Source License

/**
 * Turns this into a DBObject//ww  w  .j a v  a 2s .c  o m
 *
 * @return DBObject
 */
DBObject getObjectValue(boolean includeNulls) {
    updateObjects();
    DBObject dbObject = basicDBObjectBuilder.get();
    if (includeNulls)
        return dbObject;
    DBObject object2 = cloneObject(dbObject);
    for (String s : dbObject.keySet()) {
        Object o = dbObject.get(s);
        if (o == null) {
            object2.removeField(s);
            continue;
        }
        if (o instanceof BasicDBObject && ((BasicDBObject) o).size() == 0) {
            object2.removeField(s);
            continue;
        }
        if (o instanceof BasicDBList && ((BasicDBList) o).size() == 0) {
            object2.removeField(s);
        }
    }
    return object2;
}

From source file:net.tbnr.gearz.activerecord.GModel.java

License:Open Source License

private BasicDBObject cloneObject(DBObject object) {
    BasicDBObject basicDBObject = new BasicDBObject();
    for (String s : object.keySet()) {
        basicDBObject.put(s, object.get(s));
    }/*from  ww  w . java 2  s  .  c om*/
    return basicDBObject;

}

From source file:net.vz.mongodb.jackson.internal.util.SerializationUtils.java

License:Apache License

/**
 * Serialize the fields of the given object using the given object mapper.  This will convert POJOs to DBObjects
 * where necessary./* w  w w.  j a v  a2  s .co  m*/
 *
 * @param objectMapper The object mapper to use to do the serialization
 * @param object       The object to serialize the fields of
 * @return The DBObject, safe for serialization to MongoDB
 */
public static DBObject serializeFields(ObjectMapper objectMapper, DBObject object) {
    BasicDBObject serialised = null;
    for (String field : object.keySet()) {
        Object value = object.get(field);
        Object serialisedValue = serializeField(objectMapper, value);
        if (value != serialisedValue) {
            // It's changed
            if (serialised == null) {
                // Make a shallow copy of the object
                serialised = new BasicDBObject();
                for (String f : object.keySet()) {
                    serialised.put(f, object.get(f));
                }
            }
            serialised.put(field, serialisedValue);
        }
    }
    if (serialised != null) {
        return serialised;
    } else {
        return object;
    }
}