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:io.liveoak.mongo.gridfs.GridFSUserspaceResource.java

License:Open Source License

protected String getRootDirId(DBCollection col) {
    if (this.rootDirId == null) {
        DBObject rootObj = col.findOne(new BasicDBObject("parent", null).append("filename", ""));
        if (rootObj != null) {
            rootDirId = new GridFSDBObject(rootObj).getId().toString();
        }//from   www.  j a va  2 s .c om
    }
    return rootDirId;
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

public Map<String, Object> getDataset(Long idDataset, Object userQuery) {

    Map<String, Object> ret = null;

    DB db = mongoClient.getDB(mongoParams.get("MONGO_DB_META"));
    DBCollection colldataset = db.getCollection(mongoParams.get("MONGO_COLLECTION_DATASET"));

    DBCollection collapi = db.getCollection(mongoParams.get("MONGO_COLLECTION_API"));
    DBCollection collstream = db.getCollection(mongoParams.get("MONGO_COLLECTION_STREAM"));

    BasicDBObject query = (BasicDBObject) userQuery;
    query.put("idDataset", idDataset);

    query.put("configData.current", 1);
    query.put("configData.subtype", new BasicDBObject("$ne", "binaryDataset"));

    // DBObject searchById = new BasicDBObject("idDataset", idDataset);
    // searchById.put("configData.current", 1);
    // searchById.put("configData.subtype", new
    // BasicDBObject("$ne","binaryDataset"));
    ///* w w w  .  ja  va  2s . c  om*/

    // DBObject found = colldataset.findOne(searchById);
    DBObject found = colldataset.findOne(query);

    if (found != null) {
        Map<String, Object> cur = extractOdataPropertyFromMongo(collapi, collstream, found);

        ret = cur;
    }

    return ret;
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

public Map<String, Object> getDatasetSensor(Long datasetId) {
    DB db = mongoClient.getDB(mongoParams.get("MONGO_DB_META"));
    DBCollection collstream = db.getCollection(mongoParams.get("MONGO_COLLECTION_STREAM"));
    DBCollection colldataset = db.getCollection(mongoParams.get("MONGO_COLLECTION_DATASET"));

    DBObject searchActive = new BasicDBObject("idDataset", datasetId);
    searchActive.put("configData.current", 1);
    DBObject existingDatasetOfStream = colldataset.findOne(searchActive);
    if (existingDatasetOfStream == null)
        return null;

    DBObject searchById = new BasicDBObject("configData.idDataset", datasetId);

    searchById.put("configData.datasetVersion", existingDatasetOfStream.get("datasetVersion"));

    DBObject found = collstream.findOne(searchById);

    return extractDataFromStream(found);
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

public Map<String, Object> getStream(Long idStream, Object userQuery) {
    DB db = mongoClient.getDB(mongoParams.get("MONGO_DB_META"));
    DBCollection collstream = db.getCollection(mongoParams.get("MONGO_COLLECTION_STREAM"));
    DBCollection colldataset = db.getCollection(mongoParams.get("MONGO_COLLECTION_DATASET"));

    DBObject searchById = new BasicDBObject("idStream", idStream);
    DBObject found = collstream.findOne(searchById);

    // we have to find the stream with the version that has an dataset
    // current : 1

    DBObject configData = (DBObject) found.get("configData");

    BasicDBObject query = (BasicDBObject) userQuery;

    // DBObject searchActive = new
    // BasicDBObject("idDataset",configData.get("idDataset"));
    // searchActive.put("configData.current", 1);
    query.put("idDataset", configData.get("idDataset"));
    query.put("configData.current", 1);

    // DBObject existingDatasetOfStream = colldataset.findOne(searchActive);
    DBObject existingDatasetOfStream = colldataset.findOne(query);
    if (existingDatasetOfStream == null)
        return null;

    searchById = new BasicDBObject("idStream", idStream);
    searchById.put("configData.datasetVersion", existingDatasetOfStream.get("datasetVersion"));
    found = collstream.findOne(searchById);

    return extractDataFromStream(found);
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

public List<Map<String, Object>> getAllFilteredStreams(Object userQuery) {
    List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();
    DB db = mongoClient.getDB(mongoParams.get("MONGO_DB_META"));
    DBCollection collstream = db.getCollection(mongoParams.get("MONGO_COLLECTION_STREAM"));
    DBCollection colldataset = db.getCollection(mongoParams.get("MONGO_COLLECTION_DATASET"));

    BasicDBObject query = (BasicDBObject) userQuery;

    query.put("configData.subtype", new BasicDBObject("$ne", "binaryDataset"));

    DBCursor cursor = collstream.find(query);

    while (cursor.hasNext()) {
        DBObject found = cursor.next();//from w w  w .  j  a v  a  2 s  .  c  o m

        // if stream with that id and version has an active (current:1)
        // dataset document
        DBObject configData = (DBObject) found.get("configData");
        DBObject searchActive = new BasicDBObject("idDataset", configData.get("idDataset"));
        searchActive.put("datasetVersion", configData.get("datasetVersion"));
        searchActive.put("configData.current", 1);
        DBObject existingDatasetOfStream = colldataset.findOne(searchActive);
        if (existingDatasetOfStream != null) {
            Map<String, Object> cur = extractDataFromStream(found);
            if (cur != null) {
                if (ret.size() < MAX_RECORDS) {
                    ret.add(cur);
                } else {
                    break;
                }
            }
        }
    }
    return ret;
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

private Map<String, Object> extractOdataPropertyFromMongo(DBCollection collapi, DBCollection collstream,
        DBObject datasetFound) {/*from w w w .j  a  va  2  s  .  c om*/
    Long id = datasetFound.get("idDataset") == null ? null
            : ((Number) datasetFound.get("idDataset")).longValue();
    Long datasetVersion = datasetFound.get("datasetVersion") == null ? null
            : ((Number) datasetFound.get("datasetVersion")).longValue();
    String datasetCode = (String) datasetFound.get("datasetCode");
    DBObject configData = (DBObject) datasetFound.get("configData");
    String tenant = configData.get("tenantCode").toString();
    String datasetStatus = (String) configData.get("datasetStatus");

    DBObject info = (DBObject) datasetFound.get("info");

    String license = (String) info.get("license");
    String dataDomain = (String) info.get("dataDomain");
    String codSubDomain = (String) info.get("codSubDomain");
    String description = (String) info.get("description");
    Double fps = info.get("fps") == null ? null : ((Number) info.get("fps")).doubleValue();

    String datasetName = (String) info.get("datasetName");
    String visibility = (String) info.get("visibility");
    String registrationDate = (String) info.get("registrationDate");

    String startIngestionDate = (String) info.get("startIngestionDate");
    String endIngestionDate = (String) info.get("endIngestionDate");
    String importFileType = (String) info.get("importFileType");

    String disclaimer = (String) info.get("disclaimer");
    String copyright = (String) info.get("copyright");

    String externalReference = (String) info.get("externalReference");
    // opendata
    SimpleDateFormat sdp = new SimpleDateFormat("dd/MM/yyyy");
    DBObject opendata = (DBObject) datasetFound.get("opendata");
    Boolean isOpendata = null;
    String author = null;
    String dataUpdateDate = null;
    String language = null;
    if (opendata != null) {
        isOpendata = (Boolean) opendata.get("isOpendata");
        author = (String) opendata.get("author");
        //dataUpdateDate = sdp.format(new Date((Long) opendata.get("dataUpdateDate")));
        if (null != opendata.get("dataUpdateDate"))
            dataUpdateDate = sdp.format(new Date(new Long("" + opendata.get("dataUpdateDate"))));

        language = (String) opendata.get("language");
    }

    StringBuilder fieldsBuilder = new StringBuilder();
    BasicDBList fieldsList = (BasicDBList) info.get("fields");

    String prefix = "";
    for (int i = 0; i < fieldsList.size(); i++) {
        DBObject measure = (DBObject) fieldsList.get(i);
        String mis = measure.get("measureUnit") == null ? null : measure.get("measureUnit").toString();
        if (mis != null) {
            fieldsBuilder.append(prefix);
            prefix = ",";
            fieldsBuilder.append(mis);
        }
    }
    String unitaMisura = fieldsBuilder.toString();
    StringBuilder tagsBuilder = new StringBuilder();
    BasicDBList tagsList = (BasicDBList) info.get("tags");

    String tags = null;
    prefix = "";
    if (tagsList != null) {
        for (int i = 0; i < tagsList.size(); i++) {
            DBObject tagObj = (DBObject) tagsList.get(i);
            tagsBuilder.append(prefix);
            prefix = ",";
            tagsBuilder.append(tagObj.get("tagCode").toString());
        }
        tags = tagsBuilder.toString();
    }

    String tenantsharing = null;
    /*
     * if you want to return all the tenants that a dataset is shared with
     * decomment this code ;
     */
    DBObject tenantssharingDB = (DBObject) info.get("tenantssharing");
    StringBuilder tenantsBuilder = new StringBuilder();
    if (tenantssharingDB != null) {
        BasicDBList tenantsList = (BasicDBList) tenantssharingDB.get("tenantsharing");
        prefix = "";
        if (tenantsList != null) {
            for (int i = 0; i < tenantsList.size(); i++) {
                DBObject tenantsObj = (DBObject) tenantsList.get(i);
                tenantsBuilder.append(prefix);
                prefix = ",";
                tenantsBuilder.append(tenantsObj.get("tenantCode").toString());
            }
            tenantsharing = tenantsBuilder.toString();
        }
    }

    Map<String, Object> cur = new HashMap<String, Object>();
    cur.put("idDataset", id);
    cur.put("tenantCode", tenant);
    cur.put("tenantsharing", tenantsharing);
    cur.put("dataDomain", dataDomain);
    cur.put("codSubDomain", codSubDomain);
    cur.put("license", license);
    cur.put("description", description);

    cur.put("fps", fps);

    cur.put("measureUnit", unitaMisura);
    cur.put("tags", tags);

    // BasicDBObject findapi = new BasicDBObject();
    // findapi.append("dataset.idDataset", id);
    // findapi.append("dataset.datasetVersion", datasetVersion);

    StringBuilder apibuilder = new StringBuilder();
    // DBObject config = (DBObject) parent.get("configData");
    apibuilder.append(mongoParams.get("MONGO_API_ADDRESS"));
    apibuilder.append("name=" + datasetCode);
    apibuilder.append("_odata");
    apibuilder.append("&version=1.0&provider=admin");

    cur.put("API", apibuilder.toString());

    BasicDBObject findstream = new BasicDBObject();
    findstream.append("configData.idDataset", id);
    findstream.append("configData.datasetVersion", datasetVersion);
    DBObject streams = collstream.findOne(findstream);

    StringBuilder streambuilder = new StringBuilder();

    if (streams != null) {
        DBObject nx = streams;

        DBObject config = (DBObject) nx.get("configData");
        DBObject streamsObj = (DBObject) nx.get("streams");
        DBObject stream = (DBObject) streamsObj.get("stream");

        streambuilder.append(mongoParams.get("MONGO_API_ADDRESS"));
        streambuilder.append("name=" + config.get("tenantCode"));
        streambuilder.append(".");
        streambuilder.append(stream.get("virtualEntityCode"));
        streambuilder.append("_");
        streambuilder.append(nx.get("streamCode"));
        streambuilder.append("_stream");
        streambuilder.append("&version=1.0&provider=admin");
    }

    String download = mongoParams.get("MONGO_DOWNLOAD_ADDRESS") + "/" + tenant + "/" + datasetCode + "/csv";

    cur.put("STREAM", streambuilder.toString());

    cur.put("download", download);

    cur.put("datasetName", datasetName);
    cur.put("visibility", visibility);
    cur.put("registrationDate", registrationDate);
    cur.put("startIngestionDate", startIngestionDate);
    cur.put("endIngestionDate", endIngestionDate);
    cur.put("importFileType", importFileType);
    cur.put("datasetStatus", datasetStatus);

    cur.put("datasetVersion", (datasetVersion == null) ? null : datasetVersion.toString());
    cur.put("datasetCode", datasetCode);
    cur.put("disclaimer", disclaimer);
    cur.put("copyright", copyright);

    cur.put("externalReference", externalReference);
    // opendata
    cur.put("isOpendata", isOpendata);
    cur.put("author", author);
    cur.put("dataUpdateDate", dataUpdateDate);
    cur.put("language", language);

    return cur;
}

From source file:it.csi.smartdata.odata.datadiscovery.MongoDbStore.java

public Map<String, Object> getDatasetFromStream(Integer idStream) {
    Map<String, Object> ret = new HashMap<String, Object>();

    DB db = mongoClient.getDB(mongoParams.get("MONGO_DB_META"));
    DBCollection colldataset = db.getCollection(mongoParams.get("MONGO_COLLECTION_DATASET"));

    DBCollection collapi = db.getCollection(mongoParams.get("MONGO_COLLECTION_API"));
    DBCollection collstream = db.getCollection(mongoParams.get("MONGO_COLLECTION_STREAM"));

    DBObject searchById = new BasicDBObject("idStream", idStream);
    DBObject found = collstream.findOne(searchById);

    if (found != null) {

        DBObject configData = (DBObject) found.get("configData");
        DBObject searchDatasetById = new BasicDBObject("idDataset", configData.get("idDataset"));
        searchDatasetById.put("configData.current", 1);
        DBObject foundDataset = colldataset.findOne(searchDatasetById);

        if (foundDataset != null) {
            Map<String, Object> cur = extractOdataPropertyFromMongo(collapi, collstream, foundDataset);
            ret = cur;//from  www .jav a  2s. com
        }
    }
    return ret;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java

License:Apache License

public Object getObjectByField(MongoTemplate template, String key, String value, String collectionName,
        Class destinationClass) {
    Object result = null;/* w  ww  .j  ava  2s  .  com*/

    DBCollection collection = template.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start(key).is(value);

    BasicDBObject dbObject = (BasicDBObject) collection.findOne(qb.get());

    if (dbObject != null) {
        dbObject.remove("_id");

        ObjectMapper mapper = new ObjectMapper();
        result = mapper.convertValue(dbObject, destinationClass);
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java

License:Apache License

public Object getObjectByField(MongoTemplate template, String key, String value, String collectionName,
        Class destinationClass, List<String> fieldsToRemove) {
    Object result = null;// ww  w  . ja  v  a2 s  .  co m

    DBCollection collection = template.getCollection(collectionName);
    QueryBuilder qb = QueryBuilder.start(key).is(value);

    BasicDBObject dbObject = (BasicDBObject) collection.findOne(qb.get());

    if (dbObject != null) {
        dbObject.remove("_id");
        for (String toRemove : fieldsToRemove) {
            dbObject.remove(toRemove);
        }

        ObjectMapper mapper = new ObjectMapper();
        result = mapper.convertValue(dbObject, destinationClass);
    }

    return result;
}

From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java

License:Apache License

public Object getObjectByFields(MongoTemplate template, Map<String, Object> map, String collectionName,
        Class destinationClass) {
    DBCollection collection = template.getCollection(collectionName);

    QueryBuilder qb = QueryBuilder.start();
    for (String key : map.keySet()) {
        qb = qb.and(key).is(map.get(key));
    }/*  w  w w . j a  va2  s  .co  m*/

    BasicDBObject dbObject = (BasicDBObject) collection.findOne(qb.get());

    if (dbObject != null) {
        dbObject.remove("_id");

        ObjectMapper mapper = new ObjectMapper();
        Object result = mapper.convertValue(dbObject, destinationClass);

        return result;
    } else {
        return null;
    }
}