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.tomtom.speedtools.mongodb.DaoUtils.java

License:Apache License

/**
 * Finds entities that satisfy a specified query filter.
 *
 * @param <T>                Element type.
 * @param collection         Collection to get the entity from.
 * @param mapper             Mapper to be used to reconstitute the object stored in the collection.
 * @param ignoreMapperErrors Ignore mapping exception, skip entities with mapping errors.
 * @param query              The query to select the entity.
 * @param sorting            The sorting parameters for this query.
 * @param paging             The paging parameters for this query.
 * @return The objects in the collection for the given query.
 * @throws InternalDaoException Thrown when an unknown error has occurred. The error will have been logged.
 *//*from   w w w  .  j a v  a  2  s.c om*/
@Nonnull
public static <T> List<T> find(@Nonnull final DBCollection collection, @Nonnull final EntityMapper<T> mapper,
        final boolean ignoreMapperErrors, @Nonnull final MongoDBQuery query,
        @Nonnull final MongoDBSorting sorting, @Nonnull final MongoDBPaging paging)
        throws InternalDaoException {
    assert collection != null;
    assert mapper != null;
    assert query != null;

    try {
        final DBCursor dbCursor = collection.find(query.toDBObject());
        final DBCursor cursor = paging.apply(sorting.apply(dbCursor));

        final List<MapperError> errors = new LinkedList<>();
        final List<T> result = new ArrayList<>();
        while (cursor.hasNext()) {

            final DBObject dbValue = cursor.next();
            final T entity = mapper.fromDb(dbValue, errors);
            result.add(entity);
        }

        if (!errors.isEmpty()) {
            final String message = "Mapper errors found: " + query + ", collection=" + collection.getName()
                    + '.';
            LOG.error("find: {} Errors: '{}'", message, Json.toJson(errors));
            if (!ignoreMapperErrors) {
                throw new InternalDaoException(message);
            }
        }
        return result;
    } catch (final MapperException | MongoException e) {
        final String message = "Entity could not be mapped: " + query + ", collection=" + collection.getName()
                + '.';
        LOG.error("find: " + message, e);
        throw new InternalDaoException(message, e);
    }
}

From source file:com.versioneye.persistence.mongodb.GlobalSettingDao.java

public GlobalSetting getBy(String environment, String key) throws Exception {
    if (environment == null || key == null) {
        return null;
    }//from w w w  . j a  v  a2  s. c  o m
    DBCollection licenses = getCollection();
    BasicDBObject match = new BasicDBObject();
    match.put(GlobalSetting.ENVIRONMENT, environment);
    match.put(GlobalSetting.KEY, key.toUpperCase());
    DBCursor cursor = licenses.find(match);
    while (cursor.hasNext()) {
        DBObject gsObj = cursor.next();
        GlobalSetting gs = new GlobalSetting();
        gs.updateFromDbObject(gsObj);
        return gs;
    }
    return null;
}

From source file:com.versioneye.persistence.mongodb.LicenseDao.java

public List<License> getBy(String language, String key, String version) throws Exception {
    DBCollection licenses = getCollection();
    BasicDBObject match = new BasicDBObject();
    match.put(License.LANGUAGE, language);
    match.put(License.PROD_KEY, key);//w  ww.j ava  2s  .  com
    match.put(License.VERSION, version);
    DBCursor cursor = licenses.find(match);
    List<License> licenseList = new ArrayList<License>();
    while (cursor.hasNext()) {
        DBObject productDB = cursor.next();
        License license = new License();
        license.updateFromDbObject(productDB);
        licenseList.add(license);
    }
    return licenseList;
}

From source file:com.versioneye.persistence.mongodb.LicenseDao.java

public boolean existAlready(String language, String prodKey, String version, String name, String url) {
    DBCollection licenses = getCollection();
    BasicDBObject match = new BasicDBObject();
    match.put(License.LANGUAGE, language);
    match.put(License.PROD_KEY, prodKey);
    match.put(License.VERSION, version);
    match.put(License.NAME, name);/*www  . j a  va 2  s  . co  m*/
    match.put(License.URL, url);
    DBCursor cursor = licenses.find(match);
    if (cursor.hasNext())
        return true;
    else
        return false;
}

From source file:com.wincere.lamda.storm.bolt.CreateTable.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes no args// w  w w.  ja  v  a 2 s.c o m
 * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017
 */
public void update(BasicDBObject doc, OutputCollector collector, Tuple input) throws UnknownHostException {
    // connect to the local database server
    MongoCredential credential = MongoCredential.createMongoCRCredential("superuser", "admin",
            "12345678".toCharArray());
    try {
        MongoClient mongoClient = new MongoClient(new ServerAddress("172.16.1.171", 27017),
                Arrays.asList(credential));

        // MongoClient mongoClient = new MongoClient("172.16.1.171",27017);

        /*
        // Authenticate - optional
        MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password);
        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
        */

        // get handle to "mydb"
        DB db = mongoClient.getDB("UCAPBatchTest");

        // get a collection object to work with
        DBCollection coll = db.getCollection("Queries1");
        //  DBCollection status = db.getCollection("statustest1");
        //DBCollection coll1 = db.getCollection("queryaudittest1");
        // drop all the data in it
        //coll.drop();
        //status.drop();
        //coll1.drop();

        /*  status.insert(new BasicDBObject().append("queryStatus", "Open").append("QueryStatusID","1"));
          status.insert(new BasicDBObject().append("queryStatus", "Answered").append("QueryStatusID","2"));
          status.insert(new BasicDBObject().append("queryStatus", "Closed").append("QueryStatusID","3"));
          status.insert(new BasicDBObject().append("queryStatus", "Cancelled").append("QueryStatusID","4")); */
        // make a document and insert it

        int count = 0;
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        try {

            //.equals("Open")?"1":(splitValue[5].equals("Answered")?"2":"3")

            BasicDBObject searchQuery = new BasicDBObject().append("queryRepeatKey",
                    (String) doc.get("queryRepeatKey"));
            BasicDBObject newDocument = new BasicDBObject();

            DBCursor cursor = coll.find(searchQuery);
            //DBObject result = cursor.next();

            if (cursor.hasNext()) {
                DBObject result = cursor.next();

                String queryValue = (String) result.get("queryValue");
                String queryStatusID = (String) result.get("queryStatusID");
                String queryResponse = (String) result.get("queryResponse");
                String queryResolvedTimeStamp = (String) result.get("queryResolvedTimeStamp");
                String queryAnsweredTimeStamp = (String) result.get("queryAnsweredTimeStamp");
                String queryCreatedTimeStamp = (String) result.get("queryCreatedTimeStamp");

                if (doc.get("queryValue").equals("\\N")) {
                    doc.append("queryValue", queryValue);
                }
                if (doc.get("queryStatusID").equals("\\N")) {
                    doc.append("queryStatusID", queryStatusID);
                }
                if (doc.get("queryResponse").equals("\\N")) {
                    doc.append("queryResponse", queryResponse);
                }
                if (doc.get("queryResolvedTimeStamp").equals("\\N")) {
                    doc.append("queryResolvedTimeStamp", queryResolvedTimeStamp);
                }
                if (doc.get("queryAnsweredTimeStamp").equals("\\N")) {
                    doc.append("queryAnsweredTimeStamp", queryAnsweredTimeStamp);
                }
                doc.append("queryCreatedTimeStamp", queryCreatedTimeStamp);
            }
            if (doc.get("queryStatusID").equals("Open"))
                doc.append("queryCreatedTimeStamp", doc.get("queryCreatedTimeStamp"));

            //System.out.println(count);
            newDocument.append("$set", doc);
            try {
                coll.update(searchQuery, newDocument, true, true);
            } catch (MongoException me) {
                collector.fail(input);
            }
            // collector.ack(input);

            //coll.insert(doc);

        } catch (Exception e) {
            System.err.println("CSV file cannot be read : " + e);
        }

        //System.out.println(count);
        // lets get all the documents in the collection and print them out
        /*DBCursor cursor = coll1.find();
        try {
            while (cursor.hasNext()) {
        System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }*/

        /* // now use a query to get 1 document out
         BasicDBObject query = new BasicDBObject("i", 71);
         cursor = coll.find(query);
                
         try {
             while (cursor.hasNext()) {
        System.out.println(cursor.next());
             }
         } finally {
             cursor.close();
         }*/

        // release resources
        //db.dropDatabase();
        mongoClient.close();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.xemsdoom.xeco.core.storage.mongodb.MongoDBStorage.java

License:Open Source License

@Override
public boolean saveAccounts(Set<Account> accounts, Set<String> deleted) {

    Tracker.log("Attempting to save Accounts in MongoDB...");
    long start = System.currentTimeMillis();

    DBCollection coll = db.getCollection(collection);

    // Remove deleted accounts from collection
    for (String acc : deleted) {
        coll.remove(new BasicDBObject("_id", acc));
    }//from   ww w.  j a  va 2  s.  co  m

    // Update, Insert
    for (Account acc : accounts) {

        BasicDBObject query = new BasicDBObject("_id", acc.getAccountName());
        DBCursor cursor = coll.find(query);

        // Check if document already exists
        if (cursor.hasNext()) {

            DBObject old = cursor.next();
            BasicDBObject update = new BasicDBObject();

            // Update
            update.put("_id", acc.getAccountName());
            update.put("balance", acc.getBalance());
            update.put("freezed", acc.isFrozen());
            coll.update(old, update);

        } else {

            // Insert
            BasicDBObject insert = new BasicDBObject("_id", acc.getAccountName())
                    .append("balance", acc.getBalance()).append("freezed", acc.isFrozen());
            coll.insert(insert);
        }
    }

    Tracker.log("Saving Accounts in MongoDB took: "
            .concat(String.valueOf((System.currentTimeMillis() - start)).concat("ms.")));
    Tracker.log("Saved " + accounts.size() + " and deleted " + deleted.size() + " Accounts.");
    return true;
}

From source file:com.xemsdoom.xeco.core.storage.mongodb.MongoDBStorage.java

License:Open Source License

@Override
public Account getAccountWithAsyncCause(String accountName) {

    DBCollection coll = db.getCollection(collection);
    BasicDBObject query = new BasicDBObject("_id", accountName);
    DBCursor cursor = coll.find(query);
    if (cursor.hasNext()) {
        DBObject document = cursor.next();
        return new Account((String) document.get("_id"), (Double) document.get("balance"),
                (Boolean) document.get("freezed"));

    } else {/*  w  w  w .ja  va2  s. c  o  m*/
        return null;
    }
}

From source file:com.zdy.statistics.analysis.shop.AnalysisShop.java

public int shopSell() throws UnknownHostException {

    DB db = MongoDBConnector.getDB();//w w  w  .j ava  2s.  c  o m

    DBCollection collection = db.getCollection("server");

    BasicDBObject query = new BasicDBObject();
    query.put("message.type", "obtain_prop");
    query.put("message.event", 1);
    java.util.Date now = new java.util.Date();
    String gtTime = DateTimeUtil.dateCalculate(now, -1) + " 00:00:00";
    String ltTime = DateTimeUtil.dateCalculate(now, -1) + " 23:59:59";
    query.put("message.opera_time", new BasicDBObject("$gte", gtTime).append("$lte", ltTime));
    System.out.println(query);
    DBCursor cur = collection.find(query);
    int count = 0;
    while (cur.hasNext()) {
        BasicDBObject dbObject = (BasicDBObject) cur.next();
        DBObject message = (DBObject) dbObject.get("message");
        count += ((int) message.get("count"));
    }

    return count;
}

From source file:com.zjy.mongo.splitter.ShardChunkMongoSplitter.java

License:Apache License

@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
    boolean targetShards = MongoConfigUtil.canReadSplitsFromShards(getConfiguration());
    DB configDB = getConfigDB();// w w  w  .  ja  va 2 s.  c  o m
    DBCollection chunksCollection = configDB.getCollection("chunks");

    MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration());
    String inputNS = inputURI.getDatabase() + "." + inputURI.getCollection();

    DBCursor cur = chunksCollection.find(new BasicDBObject("ns", inputNS));

    int numChunks = 0;

    Map<String, String> shardsMap = null;
    if (targetShards) {
        try {
            shardsMap = getShardsMap();
        } catch (Exception e) {
            //Something went wrong when trying to
            //read the shards data from the config server,
            //so abort the splitting
            throw new SplitFailedException("Couldn't get shards information from config server", e);
        }
    }

    List<String> mongosHostNames = MongoConfigUtil.getInputMongosHosts(getConfiguration());
    if (targetShards && mongosHostNames.size() > 0) {
        throw new SplitFailedException(
                "Setting both mongo.input.split.read_from_shards and mongo.input.mongos_hosts"
                        + " does not make sense. ");
    }

    if (mongosHostNames.size() > 0) {
        LOG.info("Using multiple mongos instances (round robin) for reading input.");
    }

    Map<String, LinkedList<InputSplit>> shardToSplits = new HashMap<String, LinkedList<InputSplit>>();

    try {
        while (cur.hasNext()) {
            final BasicDBObject row = (BasicDBObject) cur.next();
            BasicDBObject chunkLowerBound = (BasicDBObject) row.get("min");
            BasicDBObject chunkUpperBound = (BasicDBObject) row.get("max");
            MongoInputSplit chunkSplit = createSplitFromBounds(chunkLowerBound, chunkUpperBound);
            chunkSplit.setInputURI(inputURI);
            String shard = (String) row.get("shard");
            if (targetShards) {
                //The job is configured to target shards, so replace the
                //mongos hostname with the host of the shard's servers
                String shardHosts = shardsMap.get(shard);
                if (shardHosts == null) {
                    throw new SplitFailedException("Couldn't find shard ID: " + shard + " in config.shards.");
                }

                MongoClientURI newURI = rewriteURI(inputURI, shardHosts);
                chunkSplit.setInputURI(newURI);
            } else if (mongosHostNames.size() > 0) {
                //Multiple mongos hosts are specified, so
                //choose a host name in round-robin fashion
                //and rewrite the URI using that hostname.
                //This evenly distributes the load to avoid
                //pegging a single mongos instance.
                String roundRobinHost = mongosHostNames.get(numChunks % mongosHostNames.size());
                MongoClientURI newURI = rewriteURI(inputURI, roundRobinHost);
                chunkSplit.setInputURI(newURI);
            }
            LinkedList<InputSplit> shardList = shardToSplits.get(shard);
            if (shardList == null) {
                shardList = new LinkedList<InputSplit>();
                shardToSplits.put(shard, shardList);
            }
            chunkSplit.setKeyField(MongoConfigUtil.getInputKey(getConfiguration()));
            shardList.add(chunkSplit);
            numChunks++;
        }
    } finally {
        MongoConfigUtil.close(configDB.getMongo());
    }

    final List<InputSplit> splits = new ArrayList<InputSplit>(numChunks);
    int splitIndex = 0;
    while (splitIndex < numChunks) {
        Set<String> shardSplitsToRemove = new HashSet<String>();
        for (Entry<String, LinkedList<InputSplit>> shardSplits : shardToSplits.entrySet()) {
            LinkedList<InputSplit> shardSplitsList = shardSplits.getValue();
            InputSplit split = shardSplitsList.pop();
            splits.add(splitIndex, split);
            splitIndex++;
            if (shardSplitsList.isEmpty()) {
                shardSplitsToRemove.add(shardSplits.getKey());
            }
        }
        for (String shardName : shardSplitsToRemove) {
            shardToSplits.remove(shardName);
        }
    }

    return splits;
}

From source file:ConecteMongoDB.Consultas.java

public ArrayList<Hero> buscaHero(String hero) {
    ArrayList<Hero> resultados = new ArrayList<>();

    DBCollection colecao = MongoConnection.getInstance().getDB().getCollection("heroesdata");
    BasicDBObject query = new BasicDBObject("Title", hero);
    DBCursor cursor;//from   ww w. j  a v  a  2  s.  c o m

    cursor = colecao.find(query);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String nome = (String) obj.get("Title");
            String lore = (String) obj.get("Lore");
            String sider = (String) obj.get("Side");
            String url = (String) obj.get("Url");
            Hero novoHero = new Hero(nome, lore, sider, url);
            resultados.add(novoHero);
        }
    } finally {
        cursor.close();
    }

    return resultados;
}