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.sitewhere.mongodb.device.MongoDeviceEventManagement.java

License:Open Source License

/**
 * Get the {@link DBObject} for an {@link IDeviceStreamData} chunk based on assignment
 * token, stream id, and sequence number.
 * /*from ww w .  j a va  2  s.  com*/
 * @param assignmentToken
 * @param streamId
 * @param sequenceNumber
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceStreamDataDBObject(String assignmentToken, String streamId, long sequenceNumber)
        throws SiteWhereException {
    try {
        DBCollection events = getMongoClient().getEventsCollection(getTenant());
        BasicDBObject query = new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_TOKEN, assignmentToken)
                .append(MongoDeviceStreamData.PROP_STREAM_ID, streamId)
                .append(MongoDeviceStreamData.PROP_SEQUENCE_NUMBER, sequenceNumber);
        DBObject result = events.findOne(query);
        return result;
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Return the {@link DBObject} for the device specification with the given token.
 * /*  ww  w  .j a  va 2 s  . com*/
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceSpecificationDBObjectByToken(String token) throws SiteWhereException {
    DBCollection specs = getMongoClient().getDeviceSpecificationsCollection();
    BasicDBObject query = new BasicDBObject(MongoDeviceSpecification.PROP_TOKEN, token);
    DBObject result = specs.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Return the {@link DBObject} for the device command with the given token.
 * /*from   www .  j a  v  a  2s  . co m*/
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceCommandDBObjectByToken(String token) throws SiteWhereException {
    DBCollection specs = getMongoClient().getDeviceCommandsCollection();
    BasicDBObject query = new BasicDBObject(MongoDeviceCommand.PROP_TOKEN, token);
    DBObject result = specs.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Get the DBObject containing site information that matches the given token.
 * /*from  w  ww  . j  a  v a 2 s  .  co  m*/
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceDBObjectByHardwareId(String hardwareId) throws SiteWhereException {
    DBCollection devices = getMongoClient().getDevicesCollection();
    BasicDBObject query = new BasicDBObject(MongoDevice.PROP_HARDWARE_ID, hardwareId);
    DBObject result = devices.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Find the DBObject for a device assignment based on unique token.
 * //  w w w .j av a2 s.  co m
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceAssignmentDBObjectByToken(String token) throws SiteWhereException {
    DBCollection assignments = getMongoClient().getDeviceAssignmentsCollection();
    BasicDBObject query = new BasicDBObject(MongoDeviceAssignment.PROP_TOKEN, token);
    DBObject result = assignments.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Get the DBObject containing site information that matches the given token.
 * //ww  w . ja  va 2s . c o  m
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getSiteDBObjectByToken(String token) throws SiteWhereException {
    if (getCacheProvider() != null) {
        ISite cached = getCacheProvider().getSiteCache().get(token);
        if (cached != null) {
            return MongoSite.toDBObject(cached);
        }
    }
    DBCollection sites = getMongoClient().getSitesCollection();
    BasicDBObject query = new BasicDBObject(MongoSite.PROP_TOKEN, token);
    DBObject result = sites.findOne(query);
    if ((getCacheProvider() != null) && (result != null)) {
        ISite site = MongoSite.fromDBObject(result);
        getCacheProvider().getSiteCache().put(token, site);
    }
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Return the {@link DBObject} for the zone with the given token.
 * /* w w  w . ja  v a  2 s. c o m*/
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getZoneDBObjectByToken(String token) throws SiteWhereException {
    DBCollection zones = getMongoClient().getZonesCollection();
    BasicDBObject query = new BasicDBObject(MongoZone.PROP_TOKEN, token);
    DBObject result = zones.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

/**
 * Returns the {@link DBObject} for the device group with the given token. Returns
 * null if not found./*from www.  ja v a2  s  . com*/
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getDeviceGroupDBObjectByToken(String token) throws SiteWhereException {
    DBCollection networks = getMongoClient().getDeviceGroupsCollection();
    BasicDBObject query = new BasicDBObject(MongoDeviceGroup.PROP_TOKEN, token);
    DBObject result = networks.findOne(query);
    return result;
}

From source file:com.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * Get a single entity by unique id./*from  w  w w.j a va  2s  .  co  m*/
 * 
 * @param id
 * @param api
 * @param collection
 * @return
 */
public static <T> T get(String id, Class<T> api, DBCollection collection) {
    DBObject searchById = new BasicDBObject("_id", new ObjectId(id));
    DBObject found = collection.findOne(searchById);
    if (found != null) {
        MongoConverter<T> converter = MongoConverters.getConverterFor(api);
        return converter.convert(found);
    }
    return null;
}

From source file:com.sitewhere.mongodb.scheduling.MongoScheduleManagement.java

License:Open Source License

/**
 * Returns the {@link DBObject} for the schedule with the given token. Returns null if
 * not found./* w w w  . j  av a2 s . co  m*/
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getScheduleDBObjectByToken(String token) throws SiteWhereException {
    try {
        DBCollection collection = getMongoClient().getSchedulesCollection(getTenant());
        BasicDBObject query = new BasicDBObject(MongoSchedule.PROP_TOKEN, token);
        DBObject result = collection.findOne(query);
        return result;
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}