Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:org.graylog2.hipchatalarmcallback.callback.GraylogMongoClient.java

License:Open Source License

/**
 * Gets the ID of the Graylog stream. //from w  ww. j  a v a2  s . co m
 * The stream is identified by the alarm's topic. 
 * @param alarm Object of the alarm that was triggered.
 * @return The stream id. 
 */
public String getStreamId(Alarm alarm) {

    String streamId = null;
    try {
        Mongo mongo = new Mongo();
        // the streams collection
        DBCollection col = mongo.getDB("graylog2").getCollection("streams");
        // get the name of the current stream
        String title = getStreamName(alarm);
        // find the stream by name (title = stream name)
        BasicDBObject query = new BasicDBObject("title", title);
        LOG.debug("query: " + query);
        DBObject message = col.findOne(query);
        if (message != null) {
            // get the stream id
            ObjectId id = (ObjectId) message.get("_id");
            streamId = id.toString();
        } else {
            LOG.debug("query did not return any documents.");
        }

    } catch (UnknownHostException e) {
        LOG.error("unable to query mongodb", e);
        streamId = null;
    } catch (MongoException e) {
        LOG.error("unable to query mongodb", e);
        streamId = null;
    }

    return streamId;
}

From source file:org.graylog2.hostgroups.Hostgroup.java

License:Open Source License

public static Hostgroup getById(ObjectId id) throws Exception {
    DBObject query = new BasicDBObject();
    query.put("_id", id);

    DBCollection coll = MongoConnection.getInstance().getDatabase().getCollection("hostgroups");
    DBObject group = coll.findOne(query);

    return new Hostgroup(group);
}

From source file:org.graylog2.plugins.PluginConfiguration.java

License:Open Source License

public static Map<String, String> load(Core server, String className) {
    Map<String, String> configuration = Maps.newHashMap();

    try {// w w  w.ja  v a2  s  .  c  o m
        DBCollection coll = server.getMongoConnection().getDatabase().getCollection("plugin_configurations");

        DBObject query = new BasicDBObject();
        query.put("typeclass", className);

        DBObject res = coll.findOne(query);

        if (res == null) {
            return configuration;
        }

        DBObject rawConfig = (BasicDBObject) res.get("configuration");
        Map<String, String> configs = rawConfig.toMap();

        return configs;
    } catch (Exception e) {
        LOG.error("Could not fetch plugin configuration for <" + className + ">.", e);
    }

    return configuration;
}

From source file:org.graylog2.streams.StreamImpl.java

License:Open Source License

public void setLastAlarm(int timestamp, Core server) {
    DBCollection coll = server.getMongoConnection().getDatabase().getCollection("streams");
    DBObject query = new BasicDBObject();
    query.put("_id", this.id);

    DBObject stream = coll.findOne(query);
    stream.put("last_alarm", timestamp);

    coll.update(query, stream);//from   www  . j a  va  2  s. c om
}

From source file:org.graylog2.SystemSetting.java

License:Open Source License

public boolean getBoolean(String key) {
    DBCollection coll = getCollection();

    DBObject query = new BasicDBObject();
    query.put("key", key);

    DBObject result = coll.findOne(query);
    if (result == null) {
        return false;
    }//from  w w w  . j  a  v a  2  s  .  c  o m

    if (result.get("value").equals(true)) {
        return true;
    }

    return false;
}

From source file:org.graylog2.SystemSetting.java

License:Open Source License

public BasicDBList getList(String key) {
    DBCollection coll = getCollection();
    DBObject query = new BasicDBObject();
    query.put("key", key);

    DBObject result = coll.findOne(query);

    return (BasicDBList) result.get("value");
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

public String updateDocument(String json, String dbname, String collectionName, Mongo mongo, String id) {
    logger.info("start update");
    String result = null;// ww w  .j a  v  a2 s  .  com
    DB db = mongo.getDB(dbname);
    DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id));

    // id ="509e55b844aefa5410c8e656";
    // orgDbObject.put("_id", id);
    logger.info("update id " + id);
    logger.info("Object to update" + orgDbObject.toString());

    DBCollection coll = db.getCollection(collectionName);
    DBObject found = coll.findOne(orgDbObject);
    DBObject dbObject = (DBObject) JSON.parse(json);
    coll.update(found, dbObject);

    result = "update document : _id=  " + id;
    return result;
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

public String deleteDocument(String json, String dbname, String collectionName, Mongo mongo, String id) {

    String result = null;/* w  w w .  jav a2 s.  c  o  m*/
    DB db = mongo.getDB(dbname);
    DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id));
    logger.info("id " + id);
    logger.info("k object " + orgDbObject.toString());

    DBCollection coll = db.getCollection(collectionName);
    DBObject found = coll.findOne(orgDbObject);
    coll.remove(found);
    result = "delete document : _id=  " + id;
    return result;

}

From source file:org.gsafeproject.audit.dao.EventDao.java

License:Open Source License

public Event load(String id) throws ParseException {
    BasicDBObject query = new BasicDBObject("_id", new ObjectId(id));
    DBCollection coll = db.getCollection(COLLECTION_NAME);
    DBObject finded = coll.findOne(query);
    return buildEvent(finded);
}

From source file:org.hibernate.ogm.dialect.mongodb.MongoDBDialect.java

License:Open Source License

private DBObject getObject(EntityKey key) {
    DBCollection collection = this.getCollection(key);
    DBObject searchObject = new BasicDBObject(ID_FIELDNAME, key.getColumnValues()[0]);
    return collection.findOne(searchObject);
}