Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection find.

Prototype

FindIterable<TDocument> find(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

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

License:Open Source License

/**
 * Return the {@link Document} for the zone with the given id.
 * //w w w .j  a  va2s  . com
 * @param id
 * @return
 * @throws SiteWhereException
 */
protected Document getZoneDocumentById(UUID id) throws SiteWhereException {
    try {
        MongoCollection<Document> zones = getMongoClient().getZonesCollection();
        Document query = new Document(MongoZone.PROP_ID, id);
        return zones.find(query).first();
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}

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

License:Open Source License

/**
 * Get the {@link Document} for an {@link IDeviceStream} based on assignment
 * token and stream id./*w w w  .ja  v  a  2 s .  c o  m*/
 * 
 * @param assignmentToken
 * @param streamId
 * @return
 * @throws SiteWhereException
 */
protected Document getDeviceStreamDocument(UUID assignmentId, String streamId) throws SiteWhereException {
    try {
        MongoCollection<Document> streams = getMongoClient().getStreamsCollection();
        Document query = new Document(MongoDeviceStream.PROP_ASSIGNMENT_ID, assignmentId)
                .append(MongoDeviceStream.PROP_STREAM_ID, streamId);
        return streams.find(query).first();
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}

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

License:Open Source License

/**
 * Returns the {@link Document} for the device group with the given token.
 * Returns null if not found./*from   w ww .j a v  a  2  s.c  om*/
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected Document getDeviceGroupDocumentByToken(String token) throws SiteWhereException {
    try {
        MongoCollection<Document> groups = getMongoClient().getDeviceGroupsCollection();
        Document query = new Document(MongoDeviceGroup.PROP_TOKEN, token);
        return groups.find(query).first();
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}

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

License:Open Source License

protected Document getDeviceGroupDocumentById(UUID id) throws SiteWhereException {
    try {//from ww  w .j  a v  a  2  s . c o  m
        MongoCollection<Document> groups = getMongoClient().getDeviceGroupsCollection();
        Document query = new Document(MongoDeviceGroup.PROP_ID, id);
        return groups.find(query).first();
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}

From source file:com.sitewhere.event.persistence.mongodb.MongoDeviceEventManagement.java

License:Open Source License

@Override
public ISearchResults<IDeviceEvent> listDeviceEvents(IDeviceAssignment assignment,
        IDateRangeSearchCriteria criteria) throws SiteWhereException {
    MongoCollection<Document> events = getMongoClient().getEventsCollection();
    Document query = new Document(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, assignment.getId());
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    Document sort = new Document(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);

    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    FindIterable<Document> found = events.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    MongoCursor<Document> cursor = found.iterator();

    List<IDeviceEvent> matches = new ArrayList<IDeviceEvent>();
    SearchResults<IDeviceEvent> results = new SearchResults<IDeviceEvent>(matches);
    try {/*from  www  . ja  va  2  s  .c o m*/
        results.setNumResults(events.count(query));
        while (cursor.hasNext()) {
            Document match = cursor.next();
            matches.add(MongoDeviceEventManagementPersistence.unmarshalEvent(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:com.sitewhere.event.persistence.mongodb.MongoDeviceEventManagement.java

License:Open Source License

/**
 * Get the {@link Document} for an {@link IDeviceStreamData} chunk based on
 * assignment token, stream id, and sequence number.
 * /* w ww . j a  va  2 s . com*/
 * @param assignmentId
 * @param streamId
 * @param sequenceNumber
 * @return
 * @throws SiteWhereException
 */
protected Document getDeviceStreamDataDocument(UUID assignmentId, String streamId, long sequenceNumber)
        throws SiteWhereException {
    try {
        MongoCollection<Document> events = getMongoClient().getEventsCollection();
        Document query = new Document(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, assignmentId)
                .append(MongoDeviceStreamData.PROP_STREAM_ID, streamId)
                .append(MongoDeviceStreamData.PROP_SEQUENCE_NUMBER, sequenceNumber);
        return events.find(query).first();
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.sitewhere.schedule.persistence.mongodb.MongoScheduleManagement.java

License:Open Source License

/**
 * Returns the {@link Document} for the schedule with the given token. Returns
 * null if not found./*from w  w w. j  av a 2 s .  com*/
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected Document getScheduleDocumentByToken(String token) throws SiteWhereException {
    try {
        MongoCollection<Document> collection = getMongoClient().getSchedulesCollection();
        Document query = new Document(MongoSchedule.PROP_TOKEN, token);
        return collection.find(query).first();
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.sitewhere.schedule.persistence.mongodb.MongoScheduleManagement.java

License:Open Source License

/**
 * Returns the {@link Document} for the scheduled job with the given token.
 * Returns null if not found.//from w ww.  j a v  a  2s .  c o  m
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected Document getScheduledJobDocumentByToken(String token) throws SiteWhereException {
    try {
        MongoCollection<Document> collection = getMongoClient().getScheduledJobsCollection();
        Document query = new Document(MongoSchedule.PROP_TOKEN, token);
        return collection.find(query).first();
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.sitewhere.tenant.persistence.mongodb.MongoTenantManagement.java

License:Open Source License

@Override
public ITenant getTenantByToken(String token) throws SiteWhereException {
    try {//  w  w  w  . j  a v a 2  s . c  om
        MongoCollection<Document> tenants = getMongoClient().getTenantsCollection();
        Document query = new Document(MongoTenant.PROP_TOKEN, token);
        Document dbTenant = tenants.find(query).first();
        if (dbTenant != null) {
            return MongoTenant.fromDocument(dbTenant);
        }
        return null;
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}

From source file:com.sitewhere.tenant.persistence.mongodb.MongoTenantManagement.java

License:Open Source License

/**
 * Get the {@link Document} for a Tenant given unique id.
 * /*from   ww w . j  av  a2  s  . c  o  m*/
 * @param id
 * @return
 * @throws SiteWhereException
 */
protected Document getTenantDocumentById(UUID id) throws SiteWhereException {
    try {
        MongoCollection<Document> tenants = getMongoClient().getTenantsCollection();
        Document query = new Document(MongoTenant.PROP_ID, id);
        return tenants.find(query).first();
    } catch (MongoClientException e) {
        throw MongoPersistence.handleClientException(e);
    }
}