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:controllers.Onto.java

License:Open Source License

public static Result getGroup(String database, String groupId) {
    try {//w  w  w  .j a  v a  2 s.co m
        DB db = mongoConnect(database);
        DBCollection groupsColl = db.getCollection("groups");
        BasicDBObject query = new BasicDBObject("_id", groupId);
        DBObject result = groupsColl.findOne(query);
        if (null == result) {
            mongoClose();
            return notFound("The group " + groupId + " could not be found");
        } else {
            mongoClose();
            return ok(result.toString());
        }
    } catch (Exception e) {
        mongoClose();
        return ok(e.getMessage());
    }
}

From source file:controllers.Onto.java

License:Open Source License

public static Result getProcess(String database, String processId) {
    try {//from   ww w  .  j  a v a2s  .c  o m
        DB db = mongoConnect(database);
        DBCollection processesColl = db.getCollection("processes");
        BasicDBObject query = new BasicDBObject("_id", processId);
        String response = processesColl.findOne(query).toString();
        mongoClose();
        return ok(response);
    } catch (Exception e) {
        return ok("error");
    }
}

From source file:controllers.Onto.java

License:Open Source License

public static Result getCoefficient(String database, String coeffId) {
    try {/*from ww  w .j av a2  s . c o m*/
        DB db = mongoConnect(database);
        DBCollection coefficientsColl = db.getCollection("coefficients");
        BasicDBObject query = new BasicDBObject("_id", coeffId);
        String response = coefficientsColl.findOne(query).toString();
        mongoClose();
        return ok(response);
    } catch (Exception e) {
        return ok(e.getMessage());
    }
}

From source file:DataBase.JavaMongoDB.java

/**
 *
 * @param db/*from  ww w . ja  v  a  2  s  .  c om*/
 * @param coleccion
 * @param Consulta
 * @return
 */
public DBObject Consultar(DB db, DBCollection coleccion, String Consulta) {
    long time_start, time_end;
    time_start = System.currentTimeMillis();
    DBObject a = coleccion.findOne(Consulta);
    time_end = System.currentTimeMillis();
    time = (time_end - time_start);
    return a;
}

From source file:de.belaso.mongolyn.ui.RepositoryConnector.java

License:Open Source License

@Override
public TaskData getTaskData(TaskRepository taskRepository, String taskId, IProgressMonitor monitor)
        throws CoreException {
    DBCollection dbCollection = MongolynUtils.getDBCollection(taskRepository);
    DBObject dbObject = dbCollection.findOne(new ObjectId(taskId));
    if (dbObject != null) {
        TaskData taskData = new TaskData(getTaskDataHandler().getAttributeMapper(taskRepository), KIND,
                taskRepository.getRepositoryUrl(), taskId);
        taskData.setPartial(false);/* w ww  .java  2  s .  com*/
        taskData.setVersion("1");
        getTaskDataHandler().initializeTaskData(taskRepository, taskData, null, monitor);
        for (String key : dbObject.keySet()) {
            if (!"_id".equals(key))
                taskData.getRoot().getAttribute(key.replace('_', '.')).setValue(dbObject.get(key).toString());
        }
        return taskData;
    } else {
        throw new CoreException(
                Activator.INSTANCE.getErrorStatus("MongoDB document " + taskId + " not found."));
    }
}

From source file:eu.delving.services.core.MetaRepoImpl.java

License:EUPL

@Override
public DataSet getDataSet(String spec) {
    DBCollection collection = db().getCollection(DATASETS_COLLECTION);
    DBObject object = collection.findOne(mob(DataSet.SPEC, spec));
    if (object == null) {
        return null;
    }// w ww . jav  a2s  . co  m
    return factory().createDataSet(object);
}

From source file:eu.delving.services.core.MetaRepoImpl.java

License:EUPL

@Override
public DataSet getDataSetForIndexing(int maxSimultaneous) {
    DBCollection collection = db().getCollection(DATASETS_COLLECTION);
    int indexingCount = collection.find(mob(DataSet.DATA_SET_STATE, DataSetState.INDEXING.toString())).count();
    if (indexingCount < maxSimultaneous) {
        DBObject object = collection.findOne(mob(DataSet.DATA_SET_STATE, DataSetState.QUEUED.toString()));
        if (object != null)
            return factory().createDataSet(object);
    }//from w  ww.j  a v a  2s  .c o m
    return null;
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Retrieves an object from a collection.
 * @param query - statement that is used to find the object in the collection
 * @param collection - collection where the object is searched
 * @return the object retrieved from the database or {@code null}
 *//* w  ww.j av a 2s  .  c om*/
public BasicDBObject get(final DBObject query, final String collection) {
    checkArgument(query != null, "Uninitialized query");
    checkArgument(isNotBlank(collection), "Uninitialized or invalid collection");
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final DBCollection dbcol = db.getCollection(collection);
    return (BasicDBObject) dbcol.findOne(query);
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Updates a object previously stored in a collection.
 * @param obj - value used to update the object
 * @param query - statement that is used to find the object in the collection
 * @param collection - collection where the object is searched
 *///  ww w .  ja v a2  s .c  o m
public void update(final DBObject obj, final DBObject query, final String collection) {
    checkArgument(obj != null, "Uninitialized object");
    checkArgument(isNotBlank(collection), "Uninitialized or invalid collection");
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final DBCollection dbcol = db.getCollection(collection);
    final BasicDBObject current = (BasicDBObject) dbcol.findOne(query);
    checkState(current != null, "Object not found");
    dbcol.save(BasicDBObjectBuilder.start(obj.toMap()).append("_id", current.get("_id")).get());
}

From source file:firesystem.model.FireSensor.java

public static DBObject findDocumentById(String id, DBCollection coll) {
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(id));
    DBObject dbObj = coll.findOne(query);
    return dbObj;
}