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:net.jurre.edutil.persistence.MongoDB.java

License:Open Source License

private boolean updateStationMarketData(String stationName, String systemName, Object marketData) {
    logger.debug("updated market data for " + stationName + " in " + systemName);
    DBCollection stations = this.db.getCollection(STATIONS_COLLECTION);
    BasicDBObject filter = new BasicDBObject();
    filter.put("system_id", getSystemID(systemName));
    filter.append("name", stationName);

    DBObject station = stations.findOne(filter);

    if (station != null) {
        station.put("commodities", marketData);
        stations.save(station);//from   w  w  w .j a v  a  2 s .co m
        logger.info("updated market info for Station : " + stationName + " in System: " + systemName);
        return true;
    } else {
        logger.error("Unknown station " + stationName + " in " + systemName);
        // look up possible alternatives , for example fulltext search of stations in system high scorer
    }
    return false;
}

From source file:net.jurre.edutil.persistence.MongoDB.java

License:Open Source License

private int getSystemID(String systemName) {
    DBCollection systems = this.db.getCollection(SYSTEMS_COLLECTION);
    int systemID = -1;

    BasicDBObject system = (BasicDBObject) systems.findOne(new BasicDBObject("name", systemName));
    if (system != null)
        systemID = system.getInt("id");

    return systemID;
}

From source file:net.jurre.edutil.persistence.MongoDB.java

License:Open Source License

public PluginConfiguration loadPluginConfiguration(String pluginName) {
    DBCollection collection = this.db.getCollection(MongoDB.CONFIG_COLLECTION);

    BasicDBObject query = new BasicDBObject();
    query.put("pluginName", pluginName);
    query.put("docType", "PluginConfiguration");

    DBObject doc = collection.findOne(query);

    PluginConfiguration config = null;/*from ww  w .  j  a v  a  2  s. co  m*/
    if (doc != null) {
        config = new PluginConfiguration((String) doc.get("pluginName"));
        config.appendParameters((HashMap) doc.get("parameters"));
    }

    return config;
}

From source file:net.sf.okapi.lib.tmdb.mongodb.MongoHelper.java

License:Open Source License

/**
 * Find a specific coll entry/*  w  w w.  j  a  va  2 s  . co  m*/
 * @return
 */
static DBObject findCollEntry(DBCollection coll, String key, Object value) {
    BasicDBObject query = new BasicDBObject();
    query.put(key, value);
    return coll.findOne(query);
}

From source file:net.sf.okapi.lib.tmdb.mongodb.MongoHelper.java

License:Open Source License

/**
 * Find a specific coll entry value//from  www. j  a  va 2 s.  c  om
 * @return
 */
static String findCollEntryValue(DBCollection coll, String key, Object value, String field) {
    BasicDBObject query = new BasicDBObject();
    query.put(key, value);

    DBObject obj = coll.findOne(query);

    if (obj != null) {
        return (String) obj.get(field);
    } else {
        return null;
    }
}

From source file:net.sf.okapi.lib.tmdb.mongodb.Repository.java

License:Open Source License

public Repository(String connStr) {

    String host = "localhost";

    //--parse the url--
    String[] params = connStr.split("/");
    if (params.length == 1) {
        name = params[0];/*from   w w w.j  a va  2  s  . c om*/
    } else if (params.length > 1) {
        host = params[0];
        name = params[1];
    }

    //--verify--
    if (host == null || host.trim().length() == 0) {
        return;
    }
    if (name == null || name.trim().length() == 0) {
        return;
    }

    try {
        connection = new Mongo(host);
        repository = connection.getDB(name);

        DBCollection repo = repository.getCollection(Repository.REPO_COLL);

        BasicDBObject query = new BasicDBObject();
        query.put("name", name);

        DBObject dbObj = repo.findOne(query);
        if (dbObj == null) {
            BasicDBObject doc = new BasicDBObject();
            doc.put("name", name);
            doc.put("description", "Default Description");
            repo.insert(doc);
            //TODO: Unless description is not used this "REPO" table is not needed
        }

    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    } catch (MongoException e) {
        throw new RuntimeException(e);
    }
}

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

License:Open Source License

/**
 * This is used to take a DB object, and read it. It will convert linked objets, embedded objects, and lists of anything.
 *
 * @param o The object from the database
 * @return Processed data.//from   w  w  w. j  a v a  2  s.com
 */
private Object readObjectFromDB(Object o) {
    if (o == null)
        return null;
    if (o instanceof String) {
        String o1 = (String) o;
        if (o1.startsWith("_ENUM")) {
            String s;
            try {
                s = o1.split(":")[1];
            } catch (IndexOutOfBoundsException ex) {
                return null;
            }
            String[] split = s.split("_");
            if (split.length < 2)
                return null;
            Class<Enum> aClass;
            try {
                aClass = (Class<Enum>) Class.forName(split[0]);
            } catch (ClassNotFoundException e) {
                return null;
            } catch (ClassCastException e) {
                e.printStackTrace(); //TODO Remove
                return null;
            }
            String[] strings = Arrays.copyOfRange(split, 1, split.length);
            StringBuilder className = new StringBuilder();
            for (String string : strings) {
                className.append(string).append("_");
            }
            String s1 = className.toString();
            if (s1.length() == 0)
                return null;
            String cName = s1.substring(0, s1.length() - 1);
            return Enum.valueOf(aClass, cName);
        }
    }
    if (o instanceof DBObject) {
        if (o instanceof BasicDBList) {
            BasicDBList l = (BasicDBList) o;
            List list = new ArrayList();
            for (Object next : l) {
                list.add(readObjectFromDB(next));
            }
            o = list;
        } else {
            DBObject o1 = (DBObject) o;
            Object aClass = o1.get("_class");
            if (aClass == null) {
                HashMap<String, Object> m = new HashMap<>();
                for (String s : o1.keySet()) {
                    m.put(s, o1.get(s));
                }
                return m;
            }
            if (!(aClass instanceof String))
                return null;
            Class c;
            try {
                c = Class.forName((String) aClass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace(); //TODO remove this
                return null;
            }
            if (!c.isAssignableFrom(GModel.class))
                return null;
            ObjectId objectId = (ObjectId) o1.get("_id");
            String collectionName = getCollectionName(c);
            DBCollection collection1 = this.database.getCollection(collectionName);
            DBObject id = collection1.findOne(new BasicDBObject("_id", objectId));
            if (id == null)
                return null;
            GModel m = modelFromOne(c, id, this.database);
            if (o1.containsField("_link_flag")) {
                m = m.findOne();
            }
            o = m;
        }
    }
    return o;
}

From source file:net.tbnr.util.player.TPlayerManager.java

License:Open Source License

public static String getUsernameForID(ObjectId id) {
    DBCollection collection1 = instance.collection;
    DBObject id1 = collection1.findOne(new BasicDBObject("_id", id));
    if (id1 == null)
        return null;
    Object username = id1.get("username");
    if (!(username instanceof String))
        return null;
    return (String) username;
}

From source file:NexT.db.mongo.DataModel.java

License:GNU General Public License

/**
 * Attempt to retrieve one record from the specified collection using the
 * given query./*from w w w.  j a  v a  2  s.  c o m*/
 * @param collection The collection to query
 * @param query A DBObject representing the query parameters.
 * @return Null if no documents were found or the first record.
 * @throws MongoException Thrown if the MongoWrapper has not been
 * initialized yet or if there was an error reading the objects from the db.
 */
public static DataModel getFirst(String collection, DBObject query) throws MongoException {
    MongoWrapper wrapper = MongoWrapper.getInstance();
    if (wrapper == null)
        throw new MongoException("MongoWrapper has not been initiated.");
    DBCollection col = wrapper.getCollection(collection);
    DBObject obj = col.findOne(query);
    return (obj == null) ? null : new DataModel(col, obj);
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java

License:Open Source License

public DBObject findOne(DBCollection collection, DBObject query) throws StorageException {
    try {//from  w  ww  .  ja  v a2 s  .c  om
        return collection.findOne(query);
    } catch (MongoException e) {
        throw new StorageException(e);
    }
}