List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * db.sandflies.find({ "sandfly.location" : { $exists: true }}, { "sandfly.sequence" : 0 }).count()<br> * 6330<br>//from w w w . j a v a2s .c om */ public int countGeoreferred(final String collection, final String dbPrefix, final @Nullable DBObject projection) { checkArgument(isNotBlank(collection), "Uninitialized or invalid collection"); checkArgument(isNotBlank(dbPrefix), "Uninitialized or invalid db prefix"); int count = 0; final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final DBCollection dbcol = db.getCollection(collection); final DBObject query = new BasicDBObject(dbPrefix + "location", new BasicDBObject("$exists", true)); final DBCursor cursor = dbcol.find(query, projection != null ? projection : new BasicDBObject()); try { count = cursor.count(); } finally { cursor.close(); } return count; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Lists all the files in the specified name space. Only latest versions are included in the list. * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used * @param sortCriteria - objects in the collection are sorted with this criteria * @param start - starting index/*from ww w. j a v a2s .co m*/ * @param size - maximum number of objects returned * @param count - (optional) is updated with the number of objects in the database * @return a view of the files stored under the specified name space that contains the specified range. */ public List<GridFSDBFile> listFiles(final @Nullable String namespace, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs.getFileList( new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true)), sortCriteria); cursor.skip(start).limit(size); try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
public List<GridFSDBFile> listFileOpenAccess(final @Nullable String namespace, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs .getFileList(new BasicDBObject(FILE_VERSION_PROP, new BasicDBObject("$exists", true)) .append(FILE_OPEN_ACCESS_LINK_PROP, new BasicDBObject("$exists", true)), sortCriteria); cursor.skip(start).limit(size);/* ww w . ja v a2s .c om*/ try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Lists all the versions of the specified file. * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used * @param filename - filename to be searched for in the database * @param sortCriteria - objects in the collection are sorted with this criteria * @param start - starting index/*from ww w . j a v a2 s .c o m*/ * @param size - maximum number of objects returned * @param count - (optional) is updated with the number of objects in the database * @return a view of the versions stored of the specified file that contains the specified range. */ public List<GridFSDBFile> listFileVersions(final @Nullable String namespace, final String filename, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs.getFileList(new BasicDBObject("filename", filename.trim()), sortCriteria); cursor.skip(start).limit(size); try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:example.QuickTour.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes no args//ww w . j av a 2 s . c o m * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017 */ public static void main(final String[] args) throws UnknownHostException { // connect to the local database server MongoClient mongoClient = new MongoClient(); // get handle to "mydb" DB db = mongoClient.getDB("mydb"); // Authenticate - optional // boolean auth = db.authenticate("foo", "bar"); // get a list of the collections in this database and print them out Set<String> collectionNames = db.getCollectionNames(); for (final String s : collectionNames) { System.out.println(s); } // get a collection object to work with DBCollection testCollection = db.getCollection("testCollection"); // drop all the data in it testCollection.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); testCollection.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = testCollection.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { testCollection.insert(new BasicDBObject().append("i", i)); } System.out.println( "total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount()); // lets get all the documents in the collection and print them out DBCursor cursor = testCollection.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 = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a range query to get a larger subset query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // range query with multiple constraints query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // create an index on the "i" field testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending // list the indexes on the collection List<DBObject> list = testCollection.getIndexInfo(); for (final DBObject o : list) { System.out.println(o); } // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); // see if any previous operation had an error System.out.println("Previous error : " + db.getPreviousError()); // force an error db.forceError(); // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); db.resetError(); // release resources mongoClient.close(); }
From source file:exifIndexer.MetadataQueries.java
public ResultDataNormal cameraBrand(String s) { ArrayList paths = new ArrayList<>(); ArrayList names = new ArrayList<>(); //Consulta por marca de camara MongoHandler dbmon = new MongoHandler(); DB dbmongo = dbmon.connect();/*from ww w.j a va 2 s . co m*/ DBCursor cursorDoc; DBCollection collection = dbmongo.getCollection("CameraBrands"); BasicDBObject query = new BasicDBObject("CAMERA_BRAND", s); cursorDoc = collection.find(query); try { while (cursorDoc.hasNext()) { paths.add((cursorDoc.next().get("IMG_PATH"))); names.add((cursorDoc.curr().get("IMG_NAME")) + "." + (cursorDoc.curr().get("EXTENSION"))); } } finally { cursorDoc.close(); } return new ResultDataNormal(paths, names); }
From source file:exifIndexer.MetadataQueries.java
public ResultDataNormal cameraModel(String s) { ArrayList paths = new ArrayList<>(); ArrayList names;/*from w w w . j ava2 s .co m*/ names = new ArrayList<String>(); //Consulta por modelo de camara MongoHandler dbmon = new MongoHandler(); DB dbmongo = dbmon.connect(); DBCursor cursorDoc; DBCollection collection = dbmongo.getCollection("CameraModels"); BasicDBObject query = new BasicDBObject("CAMERA_MODEL", s); cursorDoc = collection.find(query); try { while (cursorDoc.hasNext()) { paths.add((cursorDoc.next().get("IMG_PATH"))); names.add((cursorDoc.curr().get("IMG_NAME")) + "." + (cursorDoc.curr().get("EXTENSION"))); } } finally { cursorDoc.close(); } return new ResultDataNormal(paths, names); }
From source file:exifIndexer.MetadataQueries.java
public ResultDataNormal searchByISO(String s) { ArrayList paths = new ArrayList<>(); ArrayList names = new ArrayList<>(); //Consulta por modelo de camara MongoHandler dbmon = new MongoHandler(); DB dbmongo = dbmon.connect();//w ww . j av a2 s .c o m DBCursor cursorDoc; DBCollection collection = dbmongo.getCollection("SearchByISO"); BasicDBObject query = new BasicDBObject(); switch (s) { case "LOW": query = new BasicDBObject("ISO_VALUE", new BasicDBObject("$lt", 100)); break; case "HIGH": query = new BasicDBObject("ISO_VALUE", new BasicDBObject("$gt", 3200)); break; default: query = new BasicDBObject("ISO_VALUE", new BasicDBObject("$eq", Integer.parseInt(s))); break; } cursorDoc = collection.find(query); try { while (cursorDoc.hasNext()) { paths.add((cursorDoc.next().get("IMG_PATH"))); names.add((cursorDoc.curr().get("IMG_NAME")) + "." + (cursorDoc.curr().get("EXTENSION"))); } } finally { cursorDoc.close(); } return new ResultDataNormal(paths, names); }
From source file:exifIndexer.MetadataQueries.java
public ResultDataNormal shutterSpeed(String s) { //Consulta Shutter Speed ArrayList<String> paths = new ArrayList<>(); ArrayList<String> names = new ArrayList<>(); MongoHandler dbmon = new MongoHandler(); DB dbmongo = dbmon.connect();/*from w w w . j a v a2 s. c om*/ DBCursor cursorDoc; DBCollection collection = dbmongo.getCollection("Shutter_Speed"); BasicDBObject query = new BasicDBObject("SHUTTER_VALUE", s); cursorDoc = collection.find(query); try { while (cursorDoc.hasNext()) { System.out.println("algo encontrado"); paths.add((cursorDoc.next().get("IMG_PATH").toString())); names.add((cursorDoc.curr().get("IMG_NAME")) + "." + (cursorDoc.curr().get("EXTENSION"))); } } finally { cursorDoc.close(); } return new ResultDataNormal(paths, names); }
From source file:exifIndexer.MetadataQueries.java
public String[] possibleShutterSpeeds() { //Consulta posibles Shutter Speed ArrayList<String> shutterList = new ArrayList<>(); MongoHandler dbmon = new MongoHandler(); DB dbmongo = dbmon.connect();//from ww w .ja v a 2 s . c o m DBCursor cursorDoc; DBCollection collection = dbmongo.getCollection("Possible_shutterSpeeds"); cursorDoc = collection.find(); try { while (cursorDoc.hasNext()) { shutterList.add(cursorDoc.next().get("SHUTTERSPEED").toString()); } } finally { cursorDoc.close(); } String[] res = new String[shutterList.size()]; res = shutterList.toArray(res); return res; }