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.ikanow.infinit.e.utility.MongoEntityFeatureTxfer.java

License:Apache License

@SuppressWarnings("unused")
private void doUnitTestCode(String sMongoDbHost, String sMongoDbPort, String sElasticHost, String sElasticPort,
        BasicDBObject query, int nLimit) {
    Mongo mongoDB = null;//  www. j  ava 2 s .co m
    ElasticSearchManager elasticManager = null;

    try {
        // Initialize the DB:

        mongoDB = new Mongo(sMongoDbHost, Integer.parseInt(sMongoDbPort));
        DBCollection gazDB = mongoDB.getDB("feature").getCollection("entity");

        // Initialize the ES (create the index if it doesn't already):

        // 1. Set-up the entity feature index 

        String indexName = "entity_index";

        //TEST: delete the index:
        //         elasticManager = ElasticSearchManager.getIndex(indexName, sElasticHost + ":" + sElasticPort);
        //         elasticManager.deleteMe();

        //TEST: create the index
        //         String sMapping = new Gson().toJson(new GazateerPojo.Mapping(), GazateerPojo.Mapping.class);
        //         Builder localSettings = ImmutableSettings.settingsBuilder();
        //         localSettings.put("number_of_shards", 1).put("number_of_replicas", 0);          q
        //         elasticManager = ElasticSearchManager.createIndex
        //                        (indexName, false, 
        //                              sElasticHost + ":" + sElasticPort, 
        //                              sMapping, localSettings);

        //TEST: delete the index:
        //         elasticManager.deleteMe();

        //TEST: get the index:
        //         elasticManager = ElasticSearchManager.getIndex(indexName, sElasticHost + ":" + sElasticPort);

        // Now query the DB:

        DBCursor dbc = null;
        if (nLimit > 0) {
            dbc = gazDB.find(query).limit(nLimit);
        } else { // Everything!
            dbc = gazDB.find(query);
        }

        Type listType = new TypeToken<ArrayList<EntityFeaturePojo>>() {
        }.getType();
        List<EntityFeaturePojo> entities = new Gson().fromJson(dbc.toArray().toString(), listType);

        //Debug:
        List<String> entIds = new LinkedList<String>();

        // Loop over array and invoke the cleansing function for each one

        for (EntityFeaturePojo ent : entities) {

            if (null != ent.getAlias()) { // (some corrupt gazateer entry)

                //Debug:
                //System.out.println("entity=" + ent.getGazateerIndex());
                //System.out.println("aliases=" + Arrays.toString(ent.getAlias().toArray()));

                // Insert into the elasticsearch index

                //Debug:
                //System.out.println(new Gson().toJson(ent, GazateerPojo.class));

                // Handle groups (system group is: "4c927585d591d31d7b37097a")
                if (null == ent.getCommunityId()) {
                    ent.setCommunityId(new ObjectId("4c927585d591d31d7b37097a"));
                }

                //TEST: index documemt
                //               ent.synchronizeWithIndex();
                //               boolean b = elasticManager.addDocument(ent, ent.getGazateerIndex(), true);

                //TEST: remove document
                //b = elasticManager.removeDocument(ent.getGazateerIndex());

                //TEST: (part of get, bulk add/delete)
                entIds.add(ent.getIndex());

                // Debug:
                //               if (!b) {
                //                  System.out.println("Didn't add " + ent.getGazateerIndex());                  
                //               }               
            }

        } // End loop over entities

        //TEST: bulk delete
        //elasticManager.bulkAddDocuments(entities, "index", null);
        //elasticManager.bulkDeleteDocuments(entIds);

        //TEST: get document
        //         elasticManager.getRawClient().admin().indices().refresh(Requests.refreshRequest(indexName)).actionGet();
        //         for (String id: entIds) {
        //            Map<String, GetField> results = elasticManager.getDocument(id,"doccount", "disambiguated_name");
        //            System.out.println(id + ": " + results.get("doccount").values().get(0) + " , " + results.get("disambiguated_name").values().get(0));
        //         }

        //TEST: search
        //         elasticManager.getRawClient().admin().indices().refresh(Requests.refreshRequest(indexName)).actionGet();
        //         SearchRequestBuilder searchOptions = elasticManager.getSearchOptions();
        //         XContentQueryBuilder queryObj = QueryBuilders.matchAllQuery();
        //         searchOptions.addSort("doccount", SortOrder.DESC);
        //         searchOptions.addFields("doccount", "type");
        //         SearchResponse rsp = elasticManager.doQuery(queryObj, searchOptions);
        //         SearchHit[] docs = rsp.getHits().getHits();
        //         for (SearchHit hit: docs) {
        //            String id = hit.getId();
        //            Long doccount = (Long) hit.field("doccount").value();
        //            String type = (String) hit.field("type").value();
        //            System.out.println(id + ": " + doccount + ", " + type);
        //         }         

    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } finally {

        if (null != mongoDB) {
            mongoDB.close();
        }
        if (null != elasticManager) {
            //NB not sure when exactly to call this - probably can just not bother?
            //elasticManager.getRawClient().close();
        }
    }
}

From source file:com.ikanow.infinit.e.utility.MongoIndexerUtils.java

License:Apache License

public static List<BasicDBObject> getChunks(String namespace, String description) {
    DBCollection configDB = DbManager.getCollection("config", "chunks");

    BasicDBObject query = new BasicDBObject("ns", namespace);

    // Parse description:
    if (!description.equalsIgnoreCase("all")) {
        if (description.startsWith("+")) {
            query.put("_id", new BasicDBObject(DbManager.gt_, description.substring(1)));
        } else if (description.startsWith("@")) { // it's a list of replica sets
            String replicas[] = description.substring(1).split("\\s*,\\s*");
            query.put("shard", new BasicDBObject(DbManager.in_, replicas));
        } else { // assume it's a set of chunks (allow for hacky replacement because "s don't seem to get loaded from windows?)
            String ids[] = description.replace("^QUOTE^", "\"").split("\\s*,\\s*");
            query.put("_id", new BasicDBObject(DbManager.in_, ids));
        }/*from  ww w.j av a  2  s .  c  o  m*/
    } //TESTED all 3 cases
      //DEBUG
      //System.out.println("CHUNKQ=" + query.toString());

    // Get chunks and build queries
    DBCursor dbc = configDB.find(query);
    ArrayList<BasicDBObject> retList = new ArrayList<BasicDBObject>(dbc.count());
    while (dbc.hasNext()) {
        DBObject chunk = dbc.next();
        BasicDBObject derivedQuery = new BasicDBObject();
        BasicDBObject minObj = (BasicDBObject) chunk.get("min");
        BasicDBObject maxObj = (BasicDBObject) chunk.get("max");
        if (null != minObj) {
            BasicDBObject modifier = addChunkModifier(minObj);
            if (null != modifier)
                derivedQuery.put(DbManager.min_, modifier);
        }
        if (null != maxObj) {
            BasicDBObject modifier = addChunkModifier(maxObj);
            if (null != modifier)
                derivedQuery.put(DbManager.max_, modifier);
        }
        if (!derivedQuery.isEmpty()) {
            derivedQuery.put("$id", chunk.get("_id")); // (temp save the _id for printing in the main loop)
            retList.add(derivedQuery);
        }
    }
    return retList;
}

From source file:com.images3.data.impl.ImageAccessImplMongoDB.java

License:Apache License

@Override
public boolean isDuplicateVersion(String imagePlantId, ImageVersion version) {
    DBCollection coll = getDatabase().getCollection("Image");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId)
            .append("version.templateName", version.getTemplateName())
            .append("version.originalImageId", version.getOriginalImageId());
    DBCursor cursor = coll.find(criteria);
    return cursor.hasNext();
}

From source file:com.images3.data.impl.ImageAccessImplMongoDB.java

License:Apache License

public ImageOS selectImageById(ImageIdentity id) {
    DBCollection coll = getDatabase().getCollection("Image");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", id.getImagePlantId()).append("id",
            id.getImageId());//from  w  w  w.  ja va 2s  .c  o  m
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }
    return getObjectMapper().mapToImageOS((BasicDBObject) cursor.next());
}

From source file:com.images3.data.impl.ImageAccessImplMongoDB.java

License:Apache License

@Override
public ImageOS selectImageByVersion(String imagePlantId, ImageVersion version) {
    DBCollection coll = getDatabase().getCollection("Image");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId)
            .append("version.templateName", version.getTemplateName())
            .append("version.originalImageId", version.getOriginalImageId());
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }/*  w  w  w  .  j a v a  2  s .co m*/
    return getObjectMapper().mapToImageOS((BasicDBObject) cursor.next());
}

From source file:com.images3.data.impl.ImageAccessImplMongoDB.java

License:Apache License

private List<ImageOS> getImages(BasicDBObject criteria, Page pageCursor) {
    DBCollection coll = getDatabase().getCollection("Image");
    int skipRecords = (pageCursor.getStart() - 1) * pageCursor.getSize();
    List<DBObject> objects = coll.find(criteria).skip(skipRecords).limit(pageCursor.getSize()).toArray();
    List<ImageOS> images = new ArrayList<ImageOS>(objects.size());
    for (DBObject obj : objects) {
        images.add(getObjectMapper().mapToImageOS((BasicDBObject) obj));
    }//  w w w .  ja  v a 2 s  .c om
    return images;
}

From source file:com.images3.data.impl.ImageMetricsServiceImplMongoDB.java

License:Apache License

private List<DBObject> selectMetricsByImagePlantId(String imagePlantId) {
    DBCollection coll = getDatabase().getCollection("ImageMetrics");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId).append("second", 0);
    return coll.find(criteria).toArray();
}

From source file:com.images3.data.impl.ImageMetricsServiceImplMongoDB.java

License:Apache License

private List<ImageMetricsOS> getImageStats(BasicDBObject criteria, Page pageCursor) {
    DBCollection coll = getDatabase().getCollection("ImageMetrics");
    int skipRecords = (pageCursor.getStart() - 1) * pageCursor.getSize();
    List<DBObject> objects = coll.find(criteria).skip(skipRecords).limit(pageCursor.getSize()).toArray();
    List<ImageMetricsOS> metrics = new ArrayList<ImageMetricsOS>(objects.size());
    for (DBObject obj : objects) {
        metrics.add(getObjectMapper().mapToImageMetricsOS((BasicDBObject) obj));
    }/*from   w  w w .j  a  v a 2s  .c  om*/
    return metrics;
}

From source file:com.images3.data.impl.ImagePlantAccessImplMongoDB.java

License:Apache License

public boolean isDuplicatedImagePlantName(String name) {
    DBCollection coll = getDatabase().getCollection("ImagePlant");
    BasicDBObject criteria = new BasicDBObject().append("nameKey", name.toLowerCase());
    DBCursor cursor = coll.find(criteria);
    return cursor.hasNext();
}

From source file:com.images3.data.impl.ImagePlantAccessImplMongoDB.java

License:Apache License

public ImagePlantOS selectImagePlantById(String id) {
    DBCollection coll = getDatabase().getCollection("ImagePlant");
    BasicDBObject criteria = new BasicDBObject().append("id", id);
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }/*from w  w  w  .  j a  v  a 2s.co m*/
    return getObjectMapper().mapToImagePlantOS((BasicDBObject) cursor.next());
}