Example usage for com.mongodb.client MongoCollection distinct

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

Introduction

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

Prototype

<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);

Source Link

Document

Gets the distinct values of the specified field name.

Usage

From source file:connector.DBConnector.java

public static int getNumberOfTwitterUsers() {

    MongoCollection<Document> coll = database.getCollection("tweets");

    /*/*from   w  w w.  j  a v a  2 s.  c  o m*/
    Well, the root cause of the error here is because you have a String type as expected output and one of the distinct values is actually null.
     */
    //Or accept the results as BsonValue and handle those:
    ArrayList<BsonValue> distinctUsers = coll.distinct("user", BsonValue.class).into(new ArrayList<>());

    /*
    But in the latter case, you still need to handle the types returned. 
    There are methods on BsonValue to allow you to code for this, but it is also a fair bit of overkill for just getting a list of distinct values.
     */
    System.out.println("Collection size: " + coll.count());
    System.out.println("Unique users: " + distinctUsers.size());

    return distinctUsers.size();

}

From source file:connector.DBConnector.java

public static List<Map<String, String>> mentionedTwitterUsers() {
    List<Map<String, Integer>> countList = new ArrayList();
    List<Map<String, String>> resultList = new ArrayList();
    MongoCollection<Document> coll = database.getCollection("tweets");
    try (MongoCursor<BsonValue> cursor = coll.distinct("user", BsonValue.class).iterator()) {
        while (cursor.hasNext()) {
            String tempUser = cursor.next().toString().split("'")[1];
            try (MongoCursor<Document> cursor2 = coll
                    .aggregate(Arrays.asList(new BasicDBObject("$match",
                            new BasicDBObject("text", new BasicDBObject("$regex", "@" + tempUser)))))
                    .iterator()) {//from  ww  w. j a  v a 2 s.c o m
                int count = 0;
                while (cursor2.hasNext()) {
                    cursor2.next();
                    count++;
                }
                Map<String, Integer> m = new HashMap();
                m.put(tempUser, count);
                countList.add(m);
            }
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    countList.sort((Map<String, Integer> o1, Map<String, Integer> o2) -> {
        if (o1.values().iterator().next() < o2.values().iterator().next()) {
            return 1;
        } else {
            return -1;
        }
    });
    for (int i = 0; i < 5; i++) {
        Map<String, String> m = new HashMap();
        m.put(countList.get(i).keySet().iterator().next(), countList.get(i).values().iterator().next() + "");
        resultList.add(m);
    }

    return resultList;
}

From source file:module.ImportPlatform.java

License:Open Source License

public ImportPlatform() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionPlatforms = db.getCollection("platforms");
    MongoCollection<Document> collectionSamples = db.getCollection("samples");
    MongoCollection<Document> collectionSeries = db.getCollection("series");

    // ===== Platforms =====

    List<String> listGpl = collectionSamples.distinct("exp_group.id_platform", String.class)
            .into(new ArrayList<String>());

    for (String idPlatform : listGpl) {
        Document doc = collectionPlatforms.find(Filters.in("_id", idPlatform)).first();
        if (doc.getString("type") == null) {
            System.out.println(idPlatform + ": " + doc);
        }/*from   w w w .  j  a v a2s . c  om*/
    }

    mongoClient.close();
}