List of usage examples for com.mongodb.client MongoIterable iterator
@Override MongoCursor<TResult> iterator();
From source file:com.epam.dlab.backendapi.dao.BaseDAO.java
License:Apache License
/** * Checks that the documents iterator have one document only. * * @param documents documents/*from w ww .j a v a 2s . com*/ */ private Optional<Document> limitOne(MongoIterable<Document> documents) { Document first = documents.first(); try (MongoCursor<Document> iterator = documents.iterator()) { if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new DlabException("too many items found while one is expected"); } } } return Optional.ofNullable(first); }
From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java
License:Apache License
/** * Gets the list of collections present in a database in mongo to which user is connected to. * * @param dbName Name of database//from w w w . j a va2s. c o m * @return List of All Collections present in MongoDb * @throws DatabaseException throw super type of UndefinedDatabaseException * @throws CollectionException exception while performing get list operation on collection */ public Set<String> getCollList(String dbName) throws DatabaseException, CollectionException { if (dbName == null || dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Invalid Database name"); } try { // List<String> dbList = databaseService.getDbList(); // if (!dbList.contains(dbName)) { // throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, // "Database with dbName [ " + dbName + "] does not exist"); // } MongoIterable<String> listCollectionNames = mongoInstance.getDatabase(dbName).listCollectionNames(); Set<String> collectionList = new HashSet<>(); MongoCursor<String> iterator = listCollectionNames.iterator(); while (iterator.hasNext()) { String colName = iterator.next(); if (colName.contains(".files") || colName.contains(".chunks")) { continue; } collectionList.add(colName); } // For a newly added database there will be no system.users, So we // are manually creating the system.users if (collectionList.contains("system.indexes") && !collectionList.contains("system.users")) { mongoInstance.getDatabase(dbName).createCollection("system.users"); collectionList.add("system.users"); } return collectionList; } catch (MongoException m) { throw new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION, m.getMessage()); } }
From source file:io.debezium.connector.mongodb.MongoUtil.java
License:Apache License
/** * Perform the given operation on each of the values in the iterable container. * //from ww w.j a v a2 s . co m * @param iterable the iterable collection obtained from a MongoDB client; may not be null * @param operation the operation to perform; may not be null */ public static <T> void forEach(MongoIterable<T> iterable, Consumer<T> operation) { try (MongoCursor<T> cursor = iterable.iterator()) { while (cursor.hasNext()) { operation.accept(cursor.next()); } } }
From source file:io.debezium.connector.mongodb.MongoUtil.java
License:Apache License
/** * Determine if the supplied {@link MongoIterable} contains at least one element that satisfies the given predicate. * //w ww .ja v a 2s . c o m * @param iterable the iterable; may not be null * @param matcher the predicate function called on each value in the iterable until a match is found; may not be null * @return {@code true} if a matching value was found, or {@code false} otherwise */ public static <T> boolean contains(MongoIterable<T> iterable, Predicate<T> matcher) { try (MongoCursor<T> cursor = iterable.iterator()) { while (cursor.hasNext()) { if (matcher.test(cursor.next())) return true; } } return false; }
From source file:joliex.mongodb.MongoDbConnector.java
@RequestResponse public Value listCollection() { Value v = Value.create();/* w ww . j a v a 2 s . c om*/ MongoIterable<String> listCollectionNames = db.listCollectionNames(); MongoCursor<String> iteratorListCollectionNames = listCollectionNames.iterator(); int counterCollection = 0; while (iteratorListCollectionNames.hasNext()) { String collection = iteratorListCollectionNames.next(); v.getChildren("collection").get(counterCollection).add(Value.create(collection)); counterCollection++; } return v; }
From source file:joliex.mongodb.MongoDbConnector.java
@RequestResponse public Value listDB() { Value v = Value.create();/* w w w. j a va 2 s. c o m*/ MongoIterable<String> databaseNames = mongoClient.listDatabaseNames(); MongoCursor<String> databaseNamesIterator = databaseNames.iterator(); int counterDatabase = 0; while (databaseNamesIterator.hasNext()) { v.getChildren("db").get(counterDatabase).add(Value.create(databaseNamesIterator.next())); counterDatabase++; } return v; }
From source file:mypackage.DBInformation.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { try {//from w ww. jav a 2s .c o m String dbName = request.getParameter("dbname"); MongoClient mongoClient = new MongoClient("localhost", 27017); HttpSession httpSession = request.getSession(false); MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName); MongoIterable<String> mongoIterable = mongoDatabase.listCollectionNames(); MongoCursor<String> mongoCursor = mongoIterable.iterator(); JSONObject jSONObject = new JSONObject(); JSONObject jSONObject1 = new JSONObject(); JSONArray jSONArray = new JSONArray(); int i = 0; while (mongoCursor.hasNext()) { jSONArray.put(mongoCursor.next()); i++; } jSONObject.put("db", jSONArray); jSONObject.put("counter", i); out.println(jSONObject); } catch (JSONException e) { } } }
From source file:net.ymate.platform.persistence.mongodb.support.ResultSetHelper.java
License:Apache License
public static <T extends IEntity> List<T> toEntities(Class<T> entity, MongoIterable<Document> iterable) throws Exception { MongoCursor<Document> _documentIt = iterable.iterator(); List<T> _resultSet = new ArrayList<T>(); while (_documentIt.hasNext()) { _resultSet.add(toEntity(entity, _documentIt.next())); }/*from w w w . j av a2s .c o m*/ return _resultSet; }
From source file:org.flywaydb.core.internal.dbsupport.mongo.MongoDatabaseUtil.java
License:Apache License
/** * Checks whether a Mongo database is empty. * * @param mongoDb a MongoDatabase object * @return {@code true} if it is, {@code false} if isn't. Returns {@code false} if the database does not exist. * @throws FlywayException when the check fails. *//* w ww. j a va 2s . c o m*/ public static boolean empty(MongoDatabase mongoDb) throws FlywayException { try { MongoIterable<String> collectionIterable = mongoDb.listCollectionNames(); return !collectionIterable.iterator().hasNext(); } catch (MongoException e) { throw new FlywayException("Unable to check whether Mongo database " + mongoDb.getName() + " is empty", e); } }
From source file:org.radarcns.connect.mongodb.MongoDbWriter.java
License:Apache License
private void retrieveOffsets() { MongoIterable<Document> documentIterable = mongoHelper.getDocuments(OFFSETS_COLLECTION); try (MongoCursor<Document> documents = documentIterable.iterator()) { while (documents.hasNext()) { Document doc = documents.next(); Document id = (Document) doc.get("_id"); String topic = id.getString("topic"); int partition = id.getInteger("partition"); long offset = doc.getLong("offset"); latestOffsets.put(new TopicPartition(topic, partition), offset); }/*from w w w .j ava 2 s. c o m*/ } }