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(final DBObject query) 

Source Link

Document

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

Usage

From source file:com.health.smart.util.MongoC.java

public static String find(String collection, String document) throws Exception {
    DBCollection coll = getClient().getDB(database).getCollection(collection);
    DBObject docObj = (DBObject) JSON.parse(document);
    List<DBObject> objects;
    if (!docObj.containsField("projection")) {
        objects = coll.find((DBObject) JSON.parse((String) docObj.get("criteria"))).toArray();
    } else {//from w w w  .ja  va 2 s .  c om
        objects = coll.find((DBObject) JSON.parse((String) docObj.get("criteria")),
                (DBObject) JSON.parse((String) docObj.get("projection"))).toArray();
    }
    return objects.toString();
}

From source file:com.health.smart.util.MongoC.java

public static String find(String collection, DBObject document) throws Exception {
    DBCollection coll = getClient().getDB(database).getCollection(collection);
    List<DBObject> objects = coll.find(document).toArray();
    return objects.toString();
}

From source file:com.health.smart.util.MongoC.java

public static List<DBObject> findObject(String collection, DBObject document) throws Exception {
    DBCollection coll = getClient().getDB(database).getCollection(collection);
    return coll.find(document).toArray();
}

From source file:com.ibm.bluemix.smartveggie.dao.SubOutletVendorAllocationDaoImpl.java

@Override
public BasicDBObject deallocatedSubOutlet(SubOutletVendorAllocationDTO subOutletVendorAllocationDTO) {
    try {/*  ww w.  j a  va 2s  .  c  om*/
        System.out.println("starting object delete..");
        DB db = MongodbConnection.getMongoDB();
        BasicDBObject query = new BasicDBObject();
        if (subOutletVendorAllocationDTO != null) {
            if (subOutletVendorAllocationDTO.getVendorUsername() != null
                    && !subOutletVendorAllocationDTO.getVendorUsername().equalsIgnoreCase("")) {
                query.append("vendorUsername", subOutletVendorAllocationDTO.getVendorUsername());
            }
            if (subOutletVendorAllocationDTO.getSmartCityCode() != null
                    && !subOutletVendorAllocationDTO.getSmartCityCode().equalsIgnoreCase("")) {
                query.append("smartCityCode", subOutletVendorAllocationDTO.getSmartCityCode());
            }
            if (subOutletVendorAllocationDTO.getSmartOutletCode() != null
                    && !subOutletVendorAllocationDTO.getSmartOutletCode().equalsIgnoreCase("")) {
                query.append("smartOutletCode", subOutletVendorAllocationDTO.getSmartOutletCode());
            }
            if (subOutletVendorAllocationDTO.getSuboutletCode() != null
                    && !subOutletVendorAllocationDTO.getSuboutletCode().equalsIgnoreCase("")) {
                query.append("suboutletCode", subOutletVendorAllocationDTO.getSuboutletCode());
            }
            if (subOutletVendorAllocationDTO.getSuboutletAllocatedFrom() != null
                    && !subOutletVendorAllocationDTO.getSuboutletAllocatedFrom().equalsIgnoreCase("")) {
                query.append("suboutletAllocatedFrom",
                        subOutletVendorAllocationDTO.getSuboutletAllocatedFrom());
            }
            if (subOutletVendorAllocationDTO.getSuboutletAllocatedTo() != null
                    && !subOutletVendorAllocationDTO.getSuboutletAllocatedTo().equalsIgnoreCase("")) {
                query.append("suboutletAllocatedTo", subOutletVendorAllocationDTO.getSuboutletAllocatedTo());
            }
        }
        System.out.println("Querying for Delete: " + query);
        DBCollection col = db.getCollection(ICollectionName.COLLECTION_ALLOC_SUBOUTLETS);
        DBCursor cursor = col.find(query);
        BasicDBObject obj = null;
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            System.out.println("Retrieved Allocated Vendor manager outlet: " + obj);
        }
        col.remove(query);
        cursor.close();
        return obj;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.ibm.bluemix.smartveggie.dao.SubOutletVendorAllocationDaoImpl.java

@Override
public BasicDBObject getVendorForSubOutlet(String vendorUserName) {
    try {//  ww w. j  a  va2 s.  c  om
        System.out.println("starting object retrieve..");
        DB db = MongodbConnection.getMongoDB();
        BasicDBObject query = new BasicDBObject();
        query.append("vendorUsername", vendorUserName);
        System.out.println("Querying for getting vendor suboutlet: " + query);
        DBCollection col = db.getCollection(ICollectionName.COLLECTION_ALLOC_SUBOUTLETS);
        DBCursor cursor = col.find(query);
        BasicDBObject obj = null;
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            //Check the date
            Date currentDate = Calendar.getInstance().getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String allocationTillString = (String) obj.get("suboutletAllocatedTo");
            if (allocationTillString != null) {
                Date allocEndDate = null;
                try {
                    allocEndDate = dateFormat.parse(allocationTillString);
                    if (allocEndDate.before(currentDate)) {
                        System.out.println("Suboutlet Allocation already ended....");
                        //subOutletAvailableList.add(allocation);
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Retrieved Allocated Vendor suboutlet: " + obj);
            }
        }
        cursor.close();
        return obj;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.ibm.bluemix.smartveggie.dao.UserDaoImpl.java

@Override
public BasicDBObject getUser(String userName, String password) {
    DB db = MongodbConnection.getMongoDB();
    DBCollection col = db.getCollection(ICollectionName.COLLECTION_USERS);
    BasicDBObject obj = null;/* ww w  .  j  a va2 s .  c  o  m*/
    try {
        System.out.println("starting object read..");
        BasicDBObject query = new BasicDBObject();
        if ((userName != null) && (userName != ""))
            query.append("userName", userName);
        if ((password != null) && (password != ""))
            query.append("password", password);
        System.out.println("Querying for: " + query);
        DBCursor cursor = col.find(query);
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            System.out.println("Retrieved: " + obj);
        }
        cursor.close();
    } catch (Exception e) {
        throw e;
    }
    return obj;
}

From source file:com.ibm.bluemix.smartveggie.dao.UserDaoImpl.java

@Override
public List<BasicDBObject> retrieveAllUsersForType(String userTypeCode) {
    List<BasicDBObject> listDBObjects = null;
    try {/*from w  w  w.jav a  2s. co  m*/
        System.out.println("Retrieving All User of Type..." + userTypeCode);
        listDBObjects = new ArrayList<BasicDBObject>();
        DB db = MongodbConnection.getMongoDB();
        DBCollection col = db.getCollection(ICollectionName.COLLECTION_USERS);

        BasicDBObject query = new BasicDBObject();
        if ((userTypeCode != null) && (userTypeCode != ""))
            query.append("userType", userTypeCode);
        System.out.println("Querying for: " + query);

        DBCursor cursor = col.find(query);
        BasicDBObject obj = null;
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            System.out.println("Retrieved: " + obj);
            listDBObjects.add(obj);
        }
        cursor.close();
    } catch (Exception e) {
        throw e;
    }
    return listDBObjects;
}

From source file:com.ifactory.service.weather.photo.PhotoService.java

License:Apache License

public ArrayList<Photo> get() {
    DB db = mongoClient.getDB(this.dbName);
    DBCollection coll = db.getCollection(PHOTO_COLLECTION);
    BasicDBObject query = null;//from ww  w. j a  v  a  2  s.c  o m
    DBCursor cursor = null;
    ArrayList<Photo> photoList = new ArrayList();
    int weatherClassMin = -1;
    int weatherClassMax = -1;
    double rad = this.radius;

    while (true) {
        // If latitude and longitude were given, append geo search query
        if (this.lat != UNAVAILABLE_LATITUDE && this.lng != UNAVAILABLE_LONGITUDE) {
            query = setGeoCoord(this.lat, this.lng, rad);
        } else {
            query = new BasicDBObject();
        }

        // It the weather Id has given, append weather search query
        if (this.weatherId > 0) {
            if (weatherClassMin == -1 && weatherClassMax == -1) {
                query.append("weather", this.weatherId);
            } else {
                System.out.println("query with weatherClassMin(" + weatherClassMin + ") and weatherClassMax("
                        + weatherClassMax + ")");
                query.append("weather",
                        new BasicDBObject("$gte", weatherClassMin).append("$lte", weatherClassMax));
                System.out.println(query.toString());
            }
        }

        try {
            cursor = coll.find(query).limit(this.limit);
            if (cursor.count() > 0) {
                System.out.println(
                        "photo found(lat: " + this.lat + ", lng: " + this.lng + ", radius: " + rad + ")");
                break;
            } else if (this.growable == false || rad >= UNAVAILABLE_LATITUDE) {
                if (rad >= UNAVAILABLE_LATITUDE) {
                    rad = this.radius / 2;
                    if (weatherClassMin == -1 && weatherClassMax == -1) {
                        // In this case, there is no proper photos by the given weather.
                        // Let's find any photos bound for same weather class.
                        weatherClassMin = ((int) this.weatherId / 100) * 100;
                        weatherClassMax = (((int) this.weatherId / 100) + 1) * 100;
                        System.out.println("weatherClassMin and weatherClassMax exist");
                    } else if (this.weatherId > 0) {
                        this.weatherId = 0;
                        System.out.println("weatherid goes to zero");
                        continue;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        } catch (CommandFailureException e) {
            cursor = null;
            break;
        }

        rad = rad * 2;
    }

    try {
        while (cursor != null && cursor.hasNext()) {
            DBObject obj = cursor.next();
            Photo.Builder b = new Photo.Builder((String) obj.get("name"),
                    ((Number) obj.get("weather")).intValue());

            ArrayList<Double> coord = ((ArrayList<Double>) ((DBObject) obj.get("geo")).get("coordinates"));
            b.geoCoord(coord.get(0), coord.get(1)).day(((Boolean) obj.get("day")).booleanValue())
                    .timestamp(((Number) obj.get("timestamp")).longValue()).origin(this.origin);

            photoList.add(b.build());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return photoList;
}

From source file:com.ijuru.ijambo.dao.WordDAO.java

License:Open Source License

/**
 * Gets the number of words with different difficulty levels
 * @return map of difficulty levels and counts
 *//*w  w  w.  j  a v a  2 s  . co  m*/
public Map<Difficulty, Integer> getDifficultyCounts() {
    DBCollection words = db.getCollection("words");
    Map<Difficulty, Integer> counts = new HashMap<Difficulty, Integer>();

    for (Difficulty difficulty : Difficulty.values()) {
        BasicDBObject query = new BasicDBObject();
        query.put("difficulty", difficulty.ordinal());
        counts.put(difficulty, words.find(query).count());
    }

    return counts;
}

From source file:com.ijuru.ijambo.dao.WordDAO.java

License:Open Source License

/**
 * Gets a random word/*from   w ww . j a  v a 2s  .c o  m*/
 * @param difficulty the difficulty (may be null)
 * @return the word
 */
public Word getRandomWord(Difficulty difficulty) {
    DBCollection words = db.getCollection("words");
    BasicDBObject obj;

    if (difficulty != null) {
        // Get count of words of this difficulty
        BasicDBObject query = new BasicDBObject();
        query.put("difficulty", difficulty.ordinal());
        int count = words.find(query).count();

        // Pick random one
        int randOffset = (int) (Math.random() * count);
        obj = (BasicDBObject) words.find(query).limit(-1).skip(randOffset).next();
    } else {
        int randOffset = (int) (Math.random() * words.find().count());
        obj = (BasicDBObject) words.find().limit(-1).skip(randOffset).next();
    }

    return new Word(obj.getString("word"), obj.getString("meaning"),
            Difficulty.fromInt(obj.getInt("difficulty")));
}