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.scheduling.MongoScheduleManagement.java

License:Open Source License

/**
 * Returns the {@link DBObject} for the scheduled job with the given token. Returns
 * null if not found.//from   w w  w. ja v  a 2s.c  o  m
 * 
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getScheduledJobDBObjectByToken(String token) throws SiteWhereException {
    try {
        DBCollection collection = getMongoClient().getScheduledJobsCollection(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);
    }
}

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

License:Open Source License

@Override
public ITenant getTenantByAuthenticationToken(String token) throws SiteWhereException {
    try {/*from ww  w .j a v a2 s .c o  m*/
        DBCollection tenants = getMongoClient().getTenantsCollection();
        BasicDBObject query = new BasicDBObject(MongoTenant.PROP_AUTH_TOKEN, token);
        DBObject match = tenants.findOne(query);
        if (match == null) {
            return null;
        }
        return MongoTenant.fromDBObject(match);
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

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

License:Open Source License

/**
 * Get the DBObject for a Tenant given unique id.
 * //  w ww.j a va2s  .co m
 * @param id
 * @return
 * @throws SiteWhereException
 */
protected DBObject getTenantObjectById(String id) throws SiteWhereException {
    try {
        DBCollection tenants = getMongoClient().getTenantsCollection();
        BasicDBObject query = new BasicDBObject(MongoTenant.PROP_ID, id);
        return tenants.findOne(query);
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

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

License:Open Source License

/**
 * Get the DBObject for a TenantGroup given token.
 * /* w w  w. j a va  2 s.  c om*/
 * @param token
 * @return
 * @throws SiteWhereException
 */
protected DBObject getTenantGroupObjectByToken(String token) throws SiteWhereException {
    try {
        DBCollection tgroups = getMongoClient().getTenantGroupsCollection();
        BasicDBObject query = new BasicDBObject(MongoTenantGroup.PROP_TOKEN, token);
        return tgroups.findOne(query);
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.sitewhere.mongodb.user.MongoUserManagement.java

License:Open Source License

/**
 * Get the DBObject for a User given unique username.
 * /*from  w w w. j  av  a 2  s . c  om*/
 * @param username
 * @return
 * @throws SiteWhereException
 */
protected DBObject getUserObjectByUsername(String username) throws SiteWhereException {
    DBCollection users = getMongoClient().getUsersCollection();
    BasicDBObject query = new BasicDBObject(MongoUser.PROP_USERNAME, username);
    return users.findOne(query);
}

From source file:com.sitewhere.mongodb.user.MongoUserManagement.java

License:Open Source License

/**
 * Get the DBObject for a GrantedAuthority given unique name.
 * /*w w  w.  j  a v a 2s . c  om*/
 * @param name
 * @return
 * @throws SiteWhereException
 */
protected DBObject getGrantedAuthorityObjectByName(String name) throws SiteWhereException {
    DBCollection auths = getMongoClient().getAuthoritiesCollection();
    BasicDBObject query = new BasicDBObject(MongoGrantedAuthority.PROP_AUTHORITY, name);
    return auths.findOne(query);
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Returns the collection properties document.
 *
 * @param dbName the database name of the collection
 * @param collName the collection name//from  w w  w.  ja v  a  2  s . c o  m
 * @return the collection properties document
 */
public static DBObject getCollectionProps(String dbName, String collName) {
    DBCollection coll = CollectionDAO.getCollection(dbName, collName);

    DBObject properties = coll.findOne(PROPS_QUERY);

    if (properties != null) {
        properties.put("_id", collName);

        Object etag = properties.get("_etag");

        if (etag != null && ObjectId.isValid("" + etag)) {
            ObjectId oid = new ObjectId("" + etag);

            properties.put("_lastupdated_on", Instant.ofEpochSecond(oid.getTimestamp()).toString());
        }
    }

    return properties;
}

From source file:com.softinstigate.restheart.db.DBDAO.java

License:Open Source License

/**
 * @param dbName// w  ww.j a  va2 s.co m
 * @return the db props
 *
 */
public static DBObject getDbProps(String dbName) {
    if (!DBDAO.doesDbExists(dbName)) // this check is important, otherwise the db would get created if not existing after the query
    {
        return null;
    }

    DBCollection propscoll = CollectionDAO.getCollection(dbName, "_properties");

    DBObject row = propscoll.findOne(METADATA_QUERY);

    if (row != null) {
        row.put("_id", dbName);

        Object etag = row.get("_etag");

        if (etag != null && ObjectId.isValid("" + etag)) {
            ObjectId oid = new ObjectId("" + etag);

            row.put("_lastupdated_on", Instant.ofEpochSecond(oid.getTimestamp()).toString());
        }
    }

    return row;
}

From source file:com.softlyinspired.jlw.menus.mongoMenu.java

License:Open Source License

int readMenu(int menuId) throws UnknownHostException {

    DBCollection coll = repoConnection.getScriptCollection();

    // get all the documents in the collection and print them out
    BasicDBObject query = new BasicDBObject();
    BasicDBObject fields = new BasicDBObject();
    fields.put("menuTitle", 1);
    fields.put("menuId", 1);
    fields.put("concernId", 1);
    fields.put("_id", 0);

    try {/*from w w  w  .j a  v a  2 s . c o m*/
        DBObject myDoc = coll.findOne(query);
        String menuIdString = null;

        menuIdString = myDoc.get("menuId").toString();
        menuId = Integer.parseInt(menuIdString);
        menuTitle = myDoc.get("menuTitle").toString();
    } catch (Exception e) {
        return 1;
    }

    return 0;

}

From source file:com.spring.tutorial.controllers.DefaultController.java

@RequestMapping(value = "/files/{id}", method = RequestMethod.GET)
public String getFile(@PathVariable("id") String id, ModelMap map, HttpServletRequest request,
        HttpServletResponse response) throws IOException, Exception {

    String _id = request.getSession().getAttribute("id").toString();
    MongoData data = new MongoData();
    GridFS collection = new GridFS(data.getDB(), _id + "_files");

    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(id));
    GridFSDBFile file = collection.findOne(query);

    DBCollection metaFileCollection = data.getDB().getCollection(_id + "_files_meta");
    BasicDBObject metaQuery = new BasicDBObject();
    metaQuery.put("file-id", new ObjectId(id));
    DBObject metaFileDoc = metaFileCollection.findOne(metaQuery);
    MongoFile metaFile = new MongoFile(metaFileDoc);

    ServletOutputStream out = response.getOutputStream();
    String mimeType = metaFile.getType();
    response.setContentType(mimeType);// w ww .  j  a  va  2s  .  c  o m
    response.setContentLength((int) file.getLength());
    String headerKey = "Content-Disposition";

    File f = File.createTempFile(file.getFilename(),
            metaFile.getType().substring(metaFile.getType().indexOf("/") + 1));
    String headerValue = String.format("attachment; filename=\"%s\"", file.getFilename());
    response.setHeader(headerKey, headerValue);
    file.writeTo(f);

    FileInputStream inputStream = new FileInputStream(f);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;

    while (inputStream.read(buffer, 0, 4096) != -1) {
        out.write(buffer, 0, 4096);
    }
    inputStream.close();
    out.flush();
    out.close();

    return "mydrive/temp";
}