Example usage for com.mongodb.client MongoIterable first

List of usage examples for com.mongodb.client MongoIterable first

Introduction

In this page you can find the example usage for com.mongodb.client MongoIterable first.

Prototype

@Nullable
TResult first();

Source Link

Document

Helper to return the first item in the iterator or null.

Usage

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 va  2  s.  c o m
 */
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:io.seventyone.mongoutils.MongoConverterImplementation.java

License:Apache License

@Override
public <T> T firstEntityFrom(MongoIterable<Document> iterable, Class<T> entityClass) {

    if (iterable == null || entityClass == null) {
        return null;
    }/*  ww  w  . j  ava  2  s. co  m*/

    Document document = iterable.first();

    return entityFrom(document, entityClass);
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.MongoClient.java

License:Apache License

public void connect(ServerConfiguration configuration) {
    com.mongodb.MongoClient mongo = null;
    try {/*from  w  ww. j a  v a2 s . c  o m*/
        String userDatabase = configuration.getUserDatabase();
        mongo = createMongoClient(configuration);

        MongoIterable<String> collectionNames;
        if (StringUtils.isNotEmpty(userDatabase)) {
            collectionNames = mongo.getDatabase(userDatabase).listCollectionNames();
        } else {
            collectionNames = mongo.getDatabase("test").listCollectionNames();
        }
        collectionNames.first();

    } catch (IOException ex) {
        throw new MongoConnectionException(ex);
    } catch (MongoException ex) {
        LOG.error("Error when accessing Mongo server", ex);
        throw new MongoConnectionException(ex.getMessage());
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public void connect(ServerConfiguration configuration) {
    MongoClient mongo = null;//ww  w . j ava  2s.  c om
    try {
        String userDatabase = configuration.getUserDatabase();
        mongo = createMongoClient(configuration);

        MongoIterable<String> collectionNames;
        if (StringUtils.isNotEmpty(userDatabase)) {
            collectionNames = mongo.getDatabase(userDatabase).listCollectionNames();
        } else {
            collectionNames = mongo.getDatabase("test").listCollectionNames();
        }
        collectionNames.first();

    } catch (IOException ex) {
        throw new MongoConnectionException(ex);
    } catch (MongoException ex) {
        LOG.error("Error when accessing Mongo server", ex);
        throw new MongoConnectionException(ex.getMessage());
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}