Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find() 

Source Link

Document

Select all documents in collection and get a cursor to the selected documents.

Usage

From source file:com.spring.tutorial.formHandlers.LoginController.java

@RequestMapping(value = "/login_user", method = RequestMethod.POST)
public String loginUser(ModelMap map, HttpServletRequest request) {
    User user = new User();
    user.setUsername(request.getParameter("username"));
    user.setPassword(request.getParameter("password"));

    MongoDB mongo = new MongoDB(user);
    if (mongo.checkUser()) {
        request.getSession().setAttribute("username", user.getUsername());
        DBCollection collection = mongo.getDb().getCollection("users");
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            if (document.get("username").equals(user.getUsername())
                    && document.get("password").equals(user.getPassword())) {
                request.getSession().setAttribute("dropbox_token", document.get("dropbox_token"));
                request.getSession().setAttribute("id", document.get("id"));
            }/*from w  ww .j  a  va  2s . c  o m*/
        }
        return "redirect:my-drive";
    }
    return "redirect:signup";
}

From source file:com.spring.tutorial.mongo.MongoDB.java

public void getUserInfo() {
    if (auth) {/*w ww .ja v a  2 s  .c o  m*/
        DBCollection collection = db.getCollection("users");
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject document = cursor.next();

            if (document.get("username").equals(user.getUsername())) {
                user.setDropboxAccessToken((String) document.get("dropbox_token"));
            }
        }
    }
}

From source file:com.spring.tutorial.mongo.MongoDB.java

public boolean userExist() {
    //return true if user doesn't exist
    if (auth) {//  w  w w. j  a  v  a 2s  . c  o m
        DBCollection collection = db.getCollection("users");
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject doc = cursor.next();
            if (doc.get("username").equals(user.getUsername()) || doc.get("id").equals(user.getId())) {
                return false;
            }
        }
    }
    return true;
}

From source file:com.spring.tutorial.mongo.MongoDB.java

public boolean checkUser() {
    if (auth) {// w  w w  . j ava2 s  .com
        DBCollection collection = db.getCollection("users");
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            if (document.get("username").equals(user.getUsername())
                    && document.get("password").equals(user.getPassword())) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java

License:Apache License

@Override
public List<Place> getBeans() throws IndexerException {
    DBCollection dbCollection = instanceDB.getCollection(collection);
    DBCursor cursor = dbCollection.find();

    ArrayList<Place> places = new ArrayList<Place>(cursor.count());
    try {/*from  ww w . j  a va 2 s  .c om*/
        while (cursor.hasNext()) {
            places.add(fromDBObject(cursor.next()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }

    return places;
}

From source file:com.stratio.qa.utils.MongoDBUtils.java

License:Apache License

/**
 * Drop all the data associated to a MongoDB Collection.
 *
 * @param collectionName/*w ww.java  2  s. c  om*/
 */
public void dropAllDataMongoDBCollection(String collectionName) {
    DBCollection db = getMongoDBCollection(collectionName);
    DBCursor objectsList = db.find();
    try {
        while (objectsList.hasNext()) {
            db.remove(objectsList.next());
        }
    } finally {
        objectsList.close();
    }
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Returns the metrics for a given account.
 *
 * @param metricAccount the metric account
 * @return the metrics/*from  ww w. j  a  v  a2  s. c  o m*/
 */
public List<BasicDBObject> getMetrics(String metricAccount) {
    DB metricsDB = getDB("nodeablemsgdb");
    String collectionName = Constants.METRIC_COLLECTION_PREFIX + metricAccount;
    DBCollection metricsCollection = metricsDB.getCollection(collectionName);

    return asList(metricsCollection.find());
}

From source file:com.sube.daos.mongodb.CardUsagesMongoDaoImpl.java

License:Apache License

@Override
public Integer count() {
    DBCollection collection = getCardUsagesCollection();
    return collection.find().count();

}

From source file:com.tengen.Final7.java

License:Apache License

public static void main(String[] args) throws IOException {
    MongoClient client = new MongoClient();
    DB db = client.getDB("photoshare");
    int i = 0;/*from  www  . ja v a2 s .c  o  m*/
    DBCollection album = db.getCollection("albums");
    DBCollection image = db.getCollection("images");

    DBCursor cur = image.find();
    cur.next();

    while (cur.hasNext()) {
        Object id = cur.curr().get("_id");
        DBCursor curalbum = album.find(new BasicDBObject("images", id));
        if (!curalbum.hasNext()) {
            image.remove(new BasicDBObject("_id", id));
        }
        cur.next();
    }
}

From source file:com.tengen.Week3Homework1Stream.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();

    DB database = client.getDB("school");
    DBCollection collection = database.getCollection("students");

    try (DBCursor students = collection.find()) {
        students.forEach(student -> {
            BasicDBList scores = (BasicDBList) student.get("scores");
            DBObject worstHomeScoreDoc = scores.stream().map(o -> (DBObject) o) // cast Object to DBObject
                    .filter(o -> o.get("type").equals("homework"))
                    .min((o1, o2) -> ((Double) o1.get("score")).compareTo((Double) o2.get("score"))).get();
            scores.remove(worstHomeScoreDoc);

            collection.update(new BasicDBObject("_id", student.get("_id")), student);
        });//from   ww w  . ja v  a  2  s. c om
    }
}