Example usage for com.mongodb DBCollection distinct

List of usage examples for com.mongodb DBCollection distinct

Introduction

In this page you can find the example usage for com.mongodb DBCollection distinct.

Prototype

public List distinct(final String fieldName) 

Source Link

Document

Find the distinct values for a specified field across a collection and returns the results in an array.

Usage

From source file:mongodb.Distinct_titles.java

public static void main(String[] args) throws UnknownHostException {
    MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("db");
    DBCollection Collection = db.getCollection("movies_import");

    //Creating list object to store the distinct Movie titles
    List listed = Collection.distinct("Title");

    System.out.println("The distinct Movie titles are : \n");

    //For loop is used to parse the entire list od Distinct titles and display results
    for (int i = 0; i < listed.size(); i++) {
        System.out.println(listed.get(i).toString());
    }// w w w. ja va 2  s.  c  o  m
}

From source file:mongodb.JavaFindDistinct.java

public static void sizesOfAllWords(DBCollection collection) {
    List<Double> results = collection.distinct("size");
    System.out.println("\nDistinct Sizes of words: ");
    System.out.println(results.toString());
}

From source file:net.kamradtfamily.mongorest.DistinctServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    String key = req.getParameter("key");
    if (db_name == null || col_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];// w  w  w  .  jav  a 2 s.c  o m
            col_name = names[1];
        }
        if (db_name == null || col_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }
    if (key == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);
    DBCollection col = db.getCollection(col_name);

    List l = col.distinct(key);
    if (l == null || l.isEmpty()) {
        error(res, SC_NOT_FOUND, Status.get("no documents found"));
        return;
    }

    res.setIntHeader("X-Documents-Count", l.size());

    StringBuilder buf = tl.get();
    buf.setLength(0);

    JSON.serialize(l, buf);
    out_str(req, buf.toString(), "application/json");

}

From source file:org.geotools.data.mongodb.MongoDataStore.java

License:LGPL

/**
 * Get list of valid layers for this mongo DB; those containing at least one valid, non-null
 * GeoJSON geometry//from   w  ww.j av a2s  . c o m
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void getLayers() {
    Mongo mongo = null;
    try {
        // Get the list of collections from Mongo...
        mongo = new Mongo(config.getHost(), config.getPort());
        DB db = mongo.getDB(config.getDB()); // TODO add authentication
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            DBCollection dbc = db.getCollection(s);
            log.info("getLayers; collection=" + dbc);
            // find distinct non-null geometry to determine if valid layer
            // TODO branch point for separate geometry-specific layers per collection
            List geoList = dbc.distinct("geometry.type");
            // distinct returns single BSON List, may barf if results large, > max doc. size
            // trap exception on props distinct and assume it's valid since there's obviously
            // something there (http://www.mongodb.org/display/DOCS/Aggregation)
            List propList = null;
            try {
                propList = dbc.distinct("properties");
            } catch (IllegalArgumentException ex) {
                propList = new BasicBSONList();
                propList.add("ex nihilo");
            } catch (MongoException ex) {
                propList = new BasicBSONList();
                propList.add("ex nihilo");
            }
            // check that layer has valid geometry and some properties defined
            if (geoList != null && propList != null && propList.size() > 0) {
                boolean hasValidGeo = false;
                for (GeometryType type : GeometryType.values()) {
                    if (geoList.contains(type.toString())) {
                        hasValidGeo = true;
                        break;
                    }
                }
                if (hasValidGeo) {
                    layers.add(new MongoLayer(dbc, config));
                }
            }
        }
    } catch (Throwable t) {
        log.severe("getLayers error; " + t.getLocalizedMessage());
    }
    if (mongo != null) {
        mongo.close();
    }
}

From source file:org.ossmeter.platform.admin.MetricListAnalysis.java

License:Open Source License

@Get("json")
public String represent() {
    Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes()
            .get("org.restlet.http.headers");
    if (responseHeaders == null) {
        responseHeaders = new Series(Header.class);
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
    }/*  ww  w.j  av  a  2s .  co  m*/
    responseHeaders.add(new Header("Access-Control-Allow-Origin", "*"));
    responseHeaders.add(new Header("Access-Control-Allow-Methods", "GET"));

    try {
        Mongo mongo = Configuration.getInstance().getMongoConnection();

        DB db = mongo.getDB("ossmeter");
        DBCollection col = db.getCollection("metricAnalysis");

        ObjectMapper mapper = new ObjectMapper();
        ArrayNode results = mapper.createArrayNode();

        for (Object p : col.distinct("metricId")) {
            results.add(p.toString());
        }

        mongo.close();

        return results.toString();

    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    return null;
}

From source file:org.uom.fit.level2.datavis.repository.ChatServicesImpl.java

@Override
public JSONArray getAllChatDataAllMessage() {
    try {/* www. j av  a  2 s . c o  m*/
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("chat");
        DBCollection collection = db.getCollection("message");

        List<String> lst = collection.distinct("message");

        String json = new Gson().toJson(lst);
        JSONArray jsonarray = (JSONArray) new JSONParser().parse(json);
        return jsonarray;

    } catch (Exception e) {
        System.out.println("Exception Error getAllChatDataAllMEssage");
        return null;
    }
}

From source file:uk.ac.susx.tag.classificationframework.datastructures.CacheManager.java

License:Apache License

public List<Integer> getCacheConfigs(String databaseName, String collectionName) {
    DBCollection collection = client.getDB(databaseName).getCollection(collectionName);
    return collection.distinct("pipelineConfig"); // Shh... It'll all be okay... We only ever add ints to this field
}