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:com.streamreduce.core.dao.GenericCollectionDAO.java

License:Apache License

public BasicDBObject updateCollectionEntry(DAODatasourceType datasourceType, String collectionName, ObjectId id,
        String json) throws CollectionObjectNotFoundException {
    DB db = getDatabase(datasourceType);
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject newPayloadObject = (BasicDBObject) JSON.parse(json);
    BasicDBObject oldPayloadObject = (BasicDBObject) collection.findOne(new BasicDBObject("_id", id));

    if (oldPayloadObject == null) {
        throw new CollectionObjectNotFoundException(datasourceType, collectionName, id);
    }/*from w w  w .  ja  va  2s .c  om*/

    newPayloadObject.put("_id", id);

    collection.save(newPayloadObject);

    return newPayloadObject;
}

From source file:com.streamreduce.core.dao.GenericCollectionDAO.java

License:Apache License

public BasicDBObject getById(DAODatasourceType datasourceType, String collectionName, ObjectId id) {
    DB db = getDatabase(datasourceType);
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject searchById = new BasicDBObject("_id", id);
    return (BasicDBObject) collection.findOne(searchById);
}

From source file:com.streamreduce.core.dao.MetricDAO.java

License:Apache License

public Metric get(ObjectId id, String accountId) {
    DBCollection collection = getCollection(accountId);
    BasicDBObject result = (BasicDBObject) collection.findOne(new BasicDBObject("_id", id));
    return new Metric(result);
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Returns the event with the given id.//from w  ww.ja  v  a2s.c o m
 *
 * @param eventId the event id
 * @return the {@link BasicDBObject} representing the event or null if not found
 */
public BasicDBObject getEvent(String eventId) {
    DB connectionsDb = getDB("nodeablemsgdb");
    DBCollection eventCollection = connectionsDb.getCollection("eventStream");
    return (BasicDBObject) eventCollection.findOne(new ObjectId(eventId));
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Returns a single connection with the given id.
 *
 * @param connectionId the id of the connection to be returned
 * @return {@link BasicDBObject}//from  ww  w  .  j a  v  a  2  s.  c o  m
 */
public BasicDBObject getConnection(String connectionId) {
    DB connectionsDb = getDB("nodeablemsgdb");
    DBCollection eventCollection = connectionsDb.getCollection("connections");
    return (BasicDBObject) eventCollection.findOne(new ObjectId(connectionId));
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Reads the last processed event date of the spout.
 *
 * @param spoutName name of the spout// w w  w  .j  a  v  a  2 s  . co m
 * @return last processed event date
 */
public long readLastProcessedEventDate(String spoutName) {
    DB connectionsDb = getDB("nodeablemsgdb");
    DBCollection eventCollection = connectionsDb.getCollection("spoutLastProcessedDate");
    BasicDBObject query = new BasicDBObject();
    query.put("spoutName", spoutName);
    DBObject obj = eventCollection.findOne(query);
    if (obj != null) {
        return (Long) obj.get("lastProcessedEventDate");
    } else {
        return -1;
    }
}

From source file:com.sube.daos.mongodb.CardMongoDaoImpl.java

License:Apache License

private Double getBalanceByQuery(DBObject query) {
    DBCollection collection = getCardCollection();
    DBObject subeCardDBObject = collection.findOne(query);
    return (Double) subeCardDBObject.get("balance");
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.AlgorithmDao.java

License:Open Source License

/**
 * Checks if the selected owner exists in users, departments, or companies collection; if so,
 * returns the related ObjectId./*  w  ww .j a  va  2  s . c  om*/
 *
 * @param algorithm             the selected algorithm.
 * @param usersCollection       the collection of registered users.
 * @param departmentsCollection the collection of registered departments.
 * @param companiesCollection   the collection of registered companies.
 * @return the Id of the owner of the algorithm if exists, null otherwise.
 */
private ObjectId checkOwner(Algorithm algorithm, DBCollection usersCollection,
        DBCollection departmentsCollection, DBCollection companiesCollection) {

    BasicDBObject query = new BasicDBObject();

    if (algorithm.getOwner() instanceof ObjectId) { // already stored as ObjectId
        ObjectId ownerId = algorithm.getOwnerId();

        log.debug("Inserted owner Id: " + ownerId.toHexString());

        query.put("_id", ownerId);
    } else if (algorithm.getOwner() instanceof String) { // stored as a String
        String ownerId = algorithm.getOwner().toString();

        if (!ObjectId.isValid(ownerId)) {
            log.error(MSG_ERR_NOT_VALID_OWNER_ID);
            throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
        }

        log.debug("Inserted owner Id: " + ownerId);

        query.put("_id", new ObjectId(ownerId));
    } else { // unknown type
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        return null;
    }

    ObjectId res;

    // Check if the owner exists in users
    User ownerUser = (User) usersCollection.findOne(query);
    if (ownerUser == null) { // owner is not a user
        // Check if the owner exists in department
        Department ownerDepartment = (Department) departmentsCollection.findOne(query);
        if (ownerDepartment == null) { // owner is not a department
            // Check if the owner exists in company
            Company ownerCompany = (Company) companiesCollection.findOne(query);
            if (ownerCompany == null) { // owner is not a company
                res = null;
            } else { // the owner is a company
                res = new ObjectId(ownerCompany.getId());
            }
        } else { // the owner is a department
            res = new ObjectId(ownerDepartment.getId());
        }
    } else { // the owner is a user
        res = new ObjectId(ownerUser.getId());
    }

    // Send back the Id of the owner (can be null)
    return res;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DatasetDao.java

License:Open Source License

/**
 * Checks if the selected owner exists in users, departments, or companies collection; if so,
 * returns the related ObjectId.//from   w w  w  .j ava 2 s .  c o m
 *
 * @param dataset               the selected dataset.
 * @param usersCollection       the collection of registered users.
 * @param departmentsCollection the collection of registered departments.
 * @param companiesCollection   the collection of registered companies.
 * @return the Id of the owner of the dataset.
 */
private ObjectId checkOwner(Dataset dataset, DBCollection usersCollection, DBCollection departmentsCollection,
        DBCollection companiesCollection) {

    BasicDBObject query = new BasicDBObject();

    if (dataset.getOwner() instanceof ObjectId) { // already stored as ObjectId
        ObjectId ownerId = dataset.getOwnerId();

        log.debug("Inserted owner Id: " + ownerId.toString());

        query.put("_id", ownerId);
    } else if (dataset.getOwner() instanceof String) { // stored as a String
        String ownerId = dataset.getOwner().toString();

        if (!ObjectId.isValid(ownerId)) {
            log.error(MSG_ERR_NOT_VALID_OWNER_ID);
            throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
        }

        log.debug("Inserted owner Id: " + ownerId);

        query.put("_id", new ObjectId(ownerId));
    } else { // unknown type
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        return null;
    }

    ObjectId res;

    // Check if the owner exists in users
    User ownerUser = (User) usersCollection.findOne(query);
    if (ownerUser == null) { // owner is not a user
        // Check if the owner exists in departments
        Department ownerDepartment = (Department) departmentsCollection.findOne(query);
        if (ownerDepartment == null) { // owner is not a department
            // Check if the owner exists in companies
            Company ownerCompany = (Company) companiesCollection.findOne(query);
            if (ownerCompany == null) { // owner is not a company
                res = null;
            } else { // the owner is a company
                res = new ObjectId(ownerCompany.getId());
            }
        } else { // the owner is a department
            res = new ObjectId(ownerDepartment.getId());
        }
    } else { // the owner is a company
        res = new ObjectId(ownerUser.getId());
    }

    // Send back the Id of the owner (can be null);
    return res;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DataSourceDao.java

License:Open Source License

/**
 * Checks if the selected owner exists in users, departments, or companies collection; if so,
 * returns the related ObjectId./*from   ww w . j  a  v a 2 s  .  c o  m*/
 *
 * @param datasource            the selected datasource.
 * @param usersCollection       the collection of registered users.
 * @param departmentsCollection the collection of registered departments.
 * @param companiesCollection   the collection of registered companies.
 * @return the Id of the owner of the datasource if exists, null otherwise.
 */
private ObjectId checkOwner(DataSource datasource, DBCollection usersCollection,
        DBCollection departmentsCollection, DBCollection companiesCollection) {
    BasicDBObject query = new BasicDBObject();

    if (datasource.getOwner() instanceof ObjectId) { // already stored as ObjectId
        ObjectId ownerId = datasource.getOwnerId();

        log.debug("Inserted owner Id: " + ownerId.toHexString());

        query.put("_id", ownerId);
    } else if (datasource.getOwner() instanceof String) { // stored as a String
        String ownerId = datasource.getOwner().toString();

        if (!ObjectId.isValid(ownerId)) {
            log.error(MSG_ERR_NOT_VALID_OWNER_ID);
            throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
        }

        log.debug("Inserted owner Id: " + ownerId);

        query.put("_id", new ObjectId(ownerId));
    } else { // unknown type -> error somehow
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        return null;
    }

    ObjectId res;

    // Check if the owner exists in users
    User ownerUser = (User) usersCollection.findOne(query);
    if (ownerUser == null) { // owner is not a user
        // Check if the owner exists in department
        Department ownerDepartment = (Department) departmentsCollection.findOne(query);
        if (ownerDepartment == null) { // owner is not a department
            Company ownerCompany = (Company) companiesCollection.findOne(query);
            if (ownerCompany == null) { // owner is not a company
                res = null; // the owner doesn't exist
            } else { // the owner is a company
                res = new ObjectId(ownerCompany.getId());
            }
        } else { // the owner is a department
            res = new ObjectId(ownerDepartment.getId());
        }
    } else { // the owner is a user
        res = new ObjectId(ownerUser.getId());
    }

    // Send back the Id of the owner (it can be null if the owner doesn't exist)
    return res;
}