Example usage for com.mongodb.client MongoCollection count

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

Introduction

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

Prototype

@Deprecated
long count();

Source Link

Document

Counts the number of documents in the collection.

Usage

From source file:anuncius.singleton.MongoHandler.java

public boolean existsCollection(String name) {
    MongoCollection<Document> collection = mainDatabase.getCollection(name);
    if (collection != null)
        return collection.count() > 0;
    return false;
}

From source file:anuncius.singleton.MongoHandler.java

public String countEntries(String collectionName) {
    if (collectionName != null) {
        MongoCollection<Document> collection = mainDatabase.getCollection(collectionName);
        if (collection != null) {
            return "" + collection.count();
        }//  w w w  .j  a  va  2s  .  com
    }
    return "0";
}

From source file:com.averageloser.mongodemo.Main.java

public static void main(String[] args) {
    List<Book> books = new ArrayList();
    for (int i = 0; i < 3; i++) {
        Book book = new Book();
        book.setTitle(i + " title");
        book.setDescription(i + " description");
        book.setAuthor(i + " author");
        book.setPublisher(i + " publisher");

        books.add(book);/*from www  .  j  a  v a 2  s. c om*/
    }

    DBHelper helper = BookDBHelper.getInstance();

    MongoClient client = helper.getMongoClient();

    //System.out.println("\n" + client.toString());
    MongoCollection<Document> booksCollection = helper.getMongoCollection(client.getDatabase("items"), "books");

    System.out.println(booksCollection.count());

    //Add documents to the books collection.
    helper.insertDocument(booksCollection, books.get(0));
    helper.insertDocument(booksCollection, books.get(1));
    helper.insertDocument(booksCollection, books.get(2));

    //Print size of the collection.
    System.out.println(booksCollection.count());

    //Grab the book from the database by its title.
    String query = "0 title";

    Book book = (Book) helper.getDocument(booksCollection, new Document("title", query));

    //Print first book details.
    System.out.println(book.toString());

    //Remove the third book from the collection and verify that it was deleted, printing collection size.
    helper.removeDocument(booksCollection, books.get(2));

    System.out.println(booksCollection.count());
}

From source file:com.chadcover.Homework_23.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("students");
    MongoCollection<Document> collection = database.getCollection("gradesBak");

    long count = collection.count();
    System.out.println(count);/*from   w  w w  .  j a  va2 s .  c  om*/

    Bson filter = eq("type", "homework");
    Bson sort = ascending("student_id", "score");
    Bson projection = fields(include("student_id", "score"));

    // better for large datasets
    MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator();
    try {
        int lastId = 0;
        int counter = 0;
        while (result.hasNext()) {
            Document cur = result.next();
            int id = (Integer) cur.get("student_id");
            // when moving to new student
            // delete that record, which is the lowest homework score
            if (id != lastId || counter == 0) {
                double grade = (Double) cur.get("score");
                System.out.println("Delete this score: " + grade);
                collection.deleteOne(eq("_id", cur.get("_id")));
            } else {
                // do nothing
            }
            lastId = id;
            counter++;
        }
    } finally {
        result.close();
    }

}

From source file:com.cognitive.cds.invocation.mongo.EngineInfoDao.java

License:Apache License

/**
 * Call the server to fetch the engine/*  w w w. j a  v a  2 s .c  o  m*/
 *
 * @param name
 * @return
 */
protected EngineInfo fetchEngine(String name) {
    EngineInfo engine = null;

    mongoDbDao.setDatabase("engine");
    MongoCollection<Document> collection = mongoDbDao.getCollection("engines");
    logger.info("Engines Count: " + collection.count());

    Document filter = new Document();
    filter.put("name", name);
    Document eng = collection.find(filter).first();

    if (eng != null) {
        try {
            String json = eng.toJson();
            engine = (EngineInfo) JsonUtils.getMapper().readValue(json, EngineInfo.class);
            engine.setId(engine.get_id().toHexString());

        } catch (IOException e) {
            logger.error("========> Deserialize: " + e.toString());
        }
    }
    return engine;
}

From source file:com.cognitive.cds.invocation.mongo.EngineInfoDao.java

License:Apache License

public ArrayList<EngineInstanceState> getActiveEngines(String type) {

    ArrayList<EngineInstanceState> engines = new ArrayList<EngineInstanceState>();
    mongoDbDao.setDatabase("engine");
    MongoCollection<Document> collection = mongoDbDao.getCollection("instance");
    logger.debug("Total engine instance count: " + collection.count());

    Document filter = new Document();
    filter.put("type", type);
    filter.put("status", true);
    FindIterable<Document> actives = collection.find(filter);
    for (Document document : actives) {
        try {/*from   w w  w .  ja v  a  2 s  .  c  o m*/
            String json = document.toJson();
            EngineInstanceState eng = (EngineInstanceState) JsonUtils.getMapper().readValue(json,
                    EngineInstanceState.class);
            engines.add(eng);
        } catch (Exception e) {
            logger.error("=======> EngineState - intentMapping Insert Exception EngineState: " + e.toString());
        }
    }
    logger.debug("Total active engine instance count: " + engines.size() + " of type: " + type);
    return engines;
}

From source file:com.cognitive.cds.invocation.mongo.WorkProductSubscriptionDao.java

License:Apache License

/**
 * Call the server to fetch the engine//ww  w  .j  a  va  2  s  . co m
 * 
 * @param user
 * @return
 */
public WorkProductSubscription getWorkProductSubscription(String user) {
    WorkProductSubscription wps = null;

    mongoDbDao.setDatabase("work");
    MongoCollection<Document> collection = mongoDbDao.getCollection("subscriptions");
    logger.info("Subscription Count: " + collection.count()); // max of one
                                                              // per user...
    Document filter = new Document();
    filter.put("user", user);
    Document obj = collection.find(filter).first();
    if (obj != null) {
        try {
            String json = obj.toJson();
            wps = (WorkProductSubscription) JsonUtils.getMapper().readValue(json,
                    WorkProductSubscription.class);
        } catch (IOException e) {
            logger.error("========> Deserialize: " + e.toString());
        }
    } // else {
      // return defaulted response, or simply return null, which means
      // "all".
      // }
    return wps;
}

From source file:com.company.Action.java

License:Apache License

public void run() throws NotFound, ResponseException, InterruptedException {
    Way2SMS way2SMS = new Way2SMS();
    way2SMS.Login(USERNAME, PASSWORD);//  w  w  w.j a v a2  s  .c  o  m

    Set<User> hashset = new HashSet<>();
    Set<String> numbers = new HashSet<>();
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("Users");
    MongoCollection<Document> collection = database.getCollection("credenzappusers");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Phone1") != null) {
            String value = String.valueOf(cur.get("Phone1"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name1")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Phone2") != null) {
            String value = String.valueOf(cur.get("Phone2"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name2")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Phone3") != null) {
            String value = String.valueOf(cur.get("Phone3"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name3")).split(" ")[0], value));
                }
            }
        }
        //            System.out.println("Phone2 is"+cur.get("Phone2"));
    }

    System.out.println(hashset.size());

    collection = database.getCollection("credenzreceipt");
    System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Contact1") != null) {
            String value = String.valueOf(cur.get("Contact1"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name1")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Contact2") != null) {
            String value = String.valueOf(cur.get("Contact2"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name2")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Contact3") != null) {
            String value = String.valueOf(cur.get("Contact3"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    try {
                        hashset.add(new User(((String) cur.get("Name3")).split(" ")[0], value));
                    } catch (Exception e) {
                        System.out.println("cought exception");
                    }
                }
            }
        }
        //            System.out.println("Phone2 is"+cur.get("Phone2"));
    }

    collection = database.getCollection("droidfest_users");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("phone_no") != null) {
            String value = String.valueOf(cur.get("phone_no"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("name")).split(" ")[0], value));
                }
            }
        }
    }

    collection = database.getCollection("hingemeetusers");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Number") != null) {
            String value = String.valueOf(cur.get("Number"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name")).split(" ")[0], value));
                }
            }
        }
    }

    System.out.println(hashset.size());
    int i = 1;
    for (User user : hashset) {
        //            System.out.print(user.getName());
        //            System.out.println(user.getPhone_no());
        i++;
        if (i > 7) {
            way2SMS.SendSMS(user.getPhone_no(), "Hi " + user.getName()
                    + "! This is how you can build your Campus Startup: http://bit.ly/28Pa8aC FREE first lesson: http://bit.ly/28POhxJ Only for you:)");
            Thread.sleep(4000);
        } else {
            System.out.println("Interrupted for " + user.getPhone_no());
        }
        System.out.println(i);
    }
    //        List <User> number=new ArrayList<>();
    //        number.add(new User("lol","9819124829"));
    //        number.add(new User("lol","8446499908"));
    //        number.add(new User("lol","7709758284"));
    //        number.add(new User("lol","8956613425"));
    //        number.add(new User("lol","7276807536"));
    //        for (int i=0;i<number.size();i++) {
    //            way2SMS.SendSMS(number.get(i).getPhone_no(), "Hi "+number.get(i).getName()+"! This is how you can build your Campus Startup: http://bit.ly/28Pa8aC FREE first lesson: http://bit.ly/28POhxJ Only for you :)"); //args ; Phone no , Message
    //        }
}

From source file:com.DA.assignment1.Query.Demonstrate.java

public static void sum(MongoCollection<Document> movie) {
    long sum = movie.count();
    System.out.println("the total movies are: " + sum);
}

From source file:com.erudika.para.persistence.MongoDBUtils.java

License:Apache License

/**
 * Gives count information about a MongoDB table.
 * @param appid name of the collection/*  ww  w .j  a v  a2 s .c  o m*/
 * @return a long
 */
public static long getTableCount(final String appid) {
    if (StringUtils.isBlank(appid)) {
        return -1;
    }
    try {
        MongoCollection<Document> td = getTable(appid);
        return td.count();
    } catch (Exception e) {
        logger.error(null, e);
    }
    return -1;
}