List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:br.ufabc.impress.mongo.manager.DBHelper.java
@Override public String getByKey(String tableName, String _id) { if (tableName == null || tableName.equals("") || _id == null || _id.equals("")) { return "501"; }// w ww .j a v a 2s . c o m String row = null; DBCursor cursor = null; DBCollection table = db.getCollection(tableName); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("_id", new ObjectId(_id)); cursor = table.find(searchQuery); if (cursor.size() > 0) { JSON json = new JSON(); row = json.serialize(cursor); cursor.close(); return row; } else { cursor.close(); return null; } }
From source file:br.ufabc.impress.mongo.manager.DBHelper.java
@Override public String getByCondition(String tableName, Map condition) { if (tableName == null || tableName.equals("") || condition == null || condition.equals("")) { return "501"; }// w w w. j a v a 2 s . co m String row = ""; DBCollection table = db.getCollection(tableName); BasicDBObject searchQuery = new BasicDBObject(condition); BasicDBObject andQuery = new BasicDBObject(); andQuery.put("$and", searchQuery); DBCursor cursor = table.find(searchQuery); if (cursor.size() > 0) { JSON json = new JSON(); row = json.serialize(cursor); cursor.close(); return row; } else { cursor.close(); return null; } }
From source file:BusinessLogic.Service.RestaurantService.java
public String getAll() { String response = ""; try {//from w ww .ja v a2 s. com ArrayList userConsult = new ArrayList(); MongoConnection dbSingleton = MongoConnection.getInstance(); DB db = dbSingleton.getTestdb(); // get list of collections Set<String> collections = db.getCollectionNames(); // get a single collection DBCollection collection = db.getCollection(collName); DBCursor cursor = collection.find(); try { while (cursor.hasNext()) { response = response + cursor.next().toString() + "\n"; } } finally { cursor.close(); } System.out.println("Done"); } catch (MongoException e) { e.printStackTrace(); } return response; }
From source file:CapaDato.Conexion.java
public static void main(String[] Args) throws UnknownHostException { try {// ww w. j a v a 2 s. c o m // Para conectarse al servidor MongoDB MongoClient conexion = new MongoClient("localhost", 27017); // Ahora conectarse a bases de datos DB ejemplo = conexion.getDB("Ejemplo"); System.out.println("Conectarse a la base de datos exitoso"); DBCollection coleccion = ejemplo.getCollection("Alumno"); // Set<String> collectionNames = ejemplo.getCollectionNames(); // for (final String s : collectionNames) // { // System.out.println(s); // } //Para consultar otro mtodo Object objeto = new Object(); objeto = "null,{nombre:1}"; DBCursor cursor = coleccion.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } //DBObject doc = coleccion.findOne(); //Para consultar //DBCursor cursor = coleccion.find (); //System.out.println(cursor); // int i = 1; // while (cursor.hasNext ()) // { // System.out.println ("insertado documento:" + i); // System.out.println (cursor.next ()); // i ++; // } } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
public Collection<Chronicle> getChroniclesByParent(Chronicle parent) throws T2DBException { Collection<Chronicle> result = new ArrayList<Chronicle>(); if (check(Permission.DISCOVER, parent, false)) { try {/*from w ww . jav a 2 s . c om*/ Database db = parent.getDatabase(); DBCursor cursor = getMongoDB(db).getChronicles() .find(mongoObject(MongoDatabase.FLD_CHRON_PARENT, getIdOrZero(parent))); try { while (cursor.hasNext()) { Chronicle chronicle = unpack(db, (BasicDBObject) cursor.next()); check(Permission.READ, chronicle); result.add(chronicle); } } finally { cursor.close(); } } catch (Exception e) { throw T2DBMsg.exception(e, E.E40122, parent.getName(true)); } } return result; }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForProperty.java
License:Apache License
/** * Find a collection of properties with names matching a pattern. * If the pattern is enclosed in slashes it is taken as a standard * regexp pattern; the slashes will be removed. If it is not enclosed * in slashes, it is taken as a minimal pattern and all occurrences of * "*" will be replaced with ".*" (zero or more arbitrary characters). * //from w w w. j ava 2s . co m * @param database a database * @param pattern a simple pattern or a regexp pattern * @return a collection of properties, possibly empty, never null * @throws T2DBException */ public Collection<Property<?>> getProperties(Database database, String pattern) throws T2DBException { try { DBCollection coll = getMongoDB(database).getProperties(); DBObject query = null; if (pattern != null && pattern.length() > 0) { String regexp = extractRegexp(pattern); if (regexp == null) pattern = pattern.replace("*", ".*"); else pattern = regexp; query = mongoObject(MongoDatabase.FLD_PROP_NAME, Pattern.compile(pattern)); } DBCursor cursor = coll.find(query); Collection<Property<?>> result = new ArrayList<Property<?>>(); try { while (cursor.hasNext()) { result.add(unpack(database, (BasicDBObject) cursor.next())); } } finally { cursor.close(); } return result; } catch (Exception e) { throw T2DBMsg.exception(e, E.E20106, pattern); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForSchema.java
License:Apache License
/** * Find a collection of schema surrogates with labels matching a pattern. * /*from w w w.j av a 2s .c o m*/ * @param db a database * @param pattern a simple pattern where "*" stands for zero or more characters * @return a collection of schema surrogates * @throws T2DBException */ public Collection<Surrogate> getSchemaSurrogateList(Database db, String pattern) throws T2DBException { try { Collection<Surrogate> result = new ArrayList<Surrogate>(); DBCollection coll = getMongoDB(db).getSchemas(); DBObject query = null; if (pattern != null && pattern.length() > 0) { String regexp = extractRegexp(pattern); if (regexp == null) { regexp = pattern.replace("*", ".*"); if (regexp.equals(pattern)) regexp = null; } query = mongoObject(MongoDatabase.FLD_SCHEMA_NAME, regexp == null ? pattern : Pattern.compile(regexp)); } DBCursor cursor = coll.find(query); try { while (cursor.hasNext()) { ObjectId id = (ObjectId) cursor.next().get(MongoDatabase.FLD_ID); Surrogate s = makeSurrogate(db, DBObjectType.SCHEMA, new MongoDBObjectId(id)); result.add(s); } } finally { cursor.close(); } return result; } catch (Exception e) { throw T2DBMsg.exception(e, E.E30105, pattern); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForValueType.java
License:Apache License
/** * Find a collection of value types with names matching a pattern. * If the pattern is enclosed in slashes it is taken as a standard * regexp pattern; the slashes will be removed. If it is not enclosed * in slashes, it is taken as a minimal pattern and all occurrences of * "*" will be replaced with ".*" (zero or more arbitrary characters). * /*from w w w. j a v a 2s.co m*/ * @param database a database * @param pattern a simple pattern or a regexp pattern * @return a collection of value types, possibly empty, never null * @throws T2DBException */ public Collection<ValueType<?>> getValueTypes(Database database, String pattern) throws T2DBException { try { DBObject query = null; if (pattern != null && pattern.length() > 0) { String regexp = extractRegexp(pattern); if (regexp == null) pattern = pattern.replace("*", ".*"); else pattern = regexp; query = mongoObject(MongoDatabase.FLD_VT_NAME, Pattern.compile(pattern)); } DBCursor cursor = getMongoDB(database).getValueTypes().find(query); Collection<ValueType<?>> result = new ArrayList<ValueType<?>>(); try { while (cursor.hasNext()) { result.add(unpack(database, (BasicDBObject) cursor.next())); } } finally { cursor.close(); } return result; } catch (Exception e) { throw T2DBMsg.exception(e, E.E10106, pattern); } }
From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java
License:Apache License
/** * Find a chronicle with an explicit attribute value for a given property and schemas. * This looks like a "reading" method but is used in the context of schema updating. * /*w ww. j ava 2 s. co m*/ * @param property a property * @param schema a schema * @return a surrogate or null * @throws T2DBException */ public Surrogate findChronicle(Property<?> property, Schema schema) throws T2DBException { Surrogate result = null; DBCursor cursor = null; try { Database db = schema.getDatabase(); cursor = getMongoDB(db).getAttributes().find(mongoObject(MongoDatabase.FLD_ATTR_PROP, getId(property))); while (cursor.hasNext()) { ObjectId chrOid = ((BasicDBObject) cursor.next()).getObjectId(MongoDatabase.FLD_ATTR_CHRON); Surrogate entityKey = makeSurrogate(db, DBObjectType.CHRONICLE, chrOid); Schema s = db.getChronicle(entityKey).getSchema(true); if (s.dependsOnSchema(schema)) { result = entityKey; break; } } } catch (Exception e) { throw T2DBMsg.exception(e, E.E30117); } finally { if (cursor != null) cursor.close(); } return result; }
From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java
License:Apache License
/** * Find a chronicle with a given series in a collection of schemas. * This looks like a "reading" method but is used in the context of schema updating. * // ww w .j a v a 2s . co m * @param ss a series definition * @param schema a schema * @return a surrogate or null * @throws T2DBException */ public Surrogate findChronicle(SeriesDefinition ss, Schema schema) throws T2DBException { Surrogate result = null; DBCursor cursor1 = null; DBCursor cursor2 = null; try { Database db = schema.getDatabase(); cursor1 = getMongoDB(db).getChronicles() .find(mongoObject(MongoDatabase.FLD_CHRON_SCHEMA, getId(schema))); OUTER: while (cursor1.hasNext()) { ObjectId chronicleOid = getObjectId((BasicDBObject) cursor1.next()); cursor2 = getMongoDB(db).getSeries().find(mongoObject(MongoDatabase.FLD_SER_CHRON, chronicleOid, MongoDatabase.FLD_SER_NUM, ss.getNumber())); while (cursor2.hasNext()) { Surrogate entityKey = makeSurrogate(db, DBObjectType.CHRONICLE, chronicleOid); Schema s = db.getChronicle(entityKey).getSchema(true); if (s.dependsOnSchema(schema)) { result = entityKey; break OUTER; } } } } catch (Exception e) { throw T2DBMsg.exception(e, E.E30117); } finally { if (cursor1 != null) cursor1.close(); if (cursor2 != null) cursor2.close(); } return result; }