Example usage for com.mongodb.client MongoCollection count

List of usage examples for com.mongodb.client MongoCollection count

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection count.

Prototype

@Deprecated
long count(ClientSession clientSession);

Source Link

Document

Counts the number of documents in the collection.

Usage

From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java

License:Apache License

/**
 * Removes a document in the collection.
 *
 * @param databaseName the database/*from   ww w.  j  a  v a 2s. c o  m*/
 * @param collectionName the collection
 * @param documentId the document identifier to delete
 * @return the result of the operation
 * @throws DatasourceException
 * @throws NotFoundException
 */
@Override
public boolean deleteById(String databaseName, String collectionName, String documentId)
        throws DatasourceException, NotFoundException {
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        if (!collectionExists(databaseName, collectionName)) {
            throw new NotFoundException("The collection doesn't exist in the datasource");
        }
        MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName);
        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        Document query = new Document("_id", new ObjectId(documentId));
        if (collection.count(query) == 0) {
            throw new NotFoundException("The document doesn't exist in the datasource");
        }
        collection.deleteOne(query);
        return true;
    } catch (MongoException ex) {
        logger.error("An error occured while updating the document", ex);
        throw new DatasourceException("An error occured while updating the document");
    }
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> Long count(Class<T> entityClass, Map<String, Object> properties) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {/*from w  w  w .  j  a v a  2 s. c  o m*/
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });
    return collection.count(new Document(properties));
}

From source file:org.nuxeo.directory.mongodb.MongoDBReference.java

License:Apache License

/**
 * Adds the links between the source id and the target ids
 * /*from w w w. j  a  v  a2s .  com*/
 * @param sourceId the source id
 * @param targetIds the target ids
 * @param session the mongoDB session
 * @throws DirectoryException
 */
public void addLinks(String sourceId, List<String> targetIds, MongoDBSession session)
        throws DirectoryException {
    if (!initialized) {
        if (dataFileName != null) {
            initializeSession(session);
        }
        initialized = true;
    }
    if (targetIds == null || targetIds.isEmpty()) {
        return;
    }
    try {
        MongoCollection<Document> coll = session.getCollection(collection);
        List<Document> newDocs = targetIds.stream().map(targetId -> buildDoc(sourceId, targetId))
                .filter(doc -> coll.count(doc) == 0).collect(Collectors.toList());
        coll.insertMany(newDocs);
    } catch (MongoWriteException e) {
        throw new DirectoryException(e);
    }
}

From source file:org.nuxeo.directory.mongodb.MongoDBReference.java

License:Apache License

@Override
public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
    if (sourceIds == null || sourceIds.isEmpty()) {
        return;//from w  w  w.j  av  a 2  s .c o m
    }
    try (MongoDBSession session = getMongoDBSession()) {
        MongoCollection<Document> coll = session.getCollection(collection);
        List<Document> newDocs = sourceIds.stream().map(sourceId -> buildDoc(sourceId, targetId))
                .filter(doc -> coll.count(doc) == 0).collect(Collectors.toList());
        coll.insertMany(newDocs);
    } catch (MongoWriteException e) {
        throw new DirectoryException(e);
    }
}

From source file:org.nuxeo.directory.mongodb.MongoDBReference.java

License:Apache License

protected void initializeSession(MongoDBSession session) {
    // fake schema for DirectoryCSVLoader.loadData
    SchemaImpl schema = new SchemaImpl(collection, null);
    schema.addField(sourceField, StringType.INSTANCE, null, 0, Collections.emptySet());
    schema.addField(targetField, StringType.INSTANCE, null, 0, Collections.emptySet());

    Consumer<Map<String, Object>> loader = map -> {
        Document doc = MongoDBSerializationHelper.fieldMapToBson(map);
        MongoCollection<Document> coll = session.getCollection(collection);
        if (coll.count(doc) == 0) {
            coll.insertOne(doc);// w  w  w .  java2 s . c om
        }
    };
    DirectoryCSVLoader.loadData(dataFileName, BaseDirectoryDescriptor.DEFAULT_DATA_FILE_CHARACTER_SEPARATOR,
            schema, loader);
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public long getProxyCardinality() {
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject("ecm:isProxy", true);
    return table.count(searchQuery);
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public long getVersionCardinality() {
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject("ecm:isVersion", true);
    return table.count(searchQuery);
}

From source file:org.nuxeo.tools.esync.db.DbMongo.java

License:Open Source License

@Override
public long getOrphanCardinality() {
    MongoCollection<org.bson.Document> table = getMongoCollection();
    BasicDBObject searchQuery = new BasicDBObject("ecm:parentId", new BasicDBObject("$exists", false));
    searchQuery.append("ecm:isVersion", new BasicDBObject("$ne", true));
    // Don't take in account the root document
    long ret = table.count(searchQuery) - 1;
    return ret;/* www. j av  a2s.  co m*/
}

From source file:org.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Returns the number of documents in the given collection (taking into
 * account the filters in case)./*from  w w w . ja va 2s  .  c om*/
 *
 * @param coll the mongodb DBCollection object.
 * @param filters the filters to apply. it is a Deque collection of mongodb
 * query conditions.
 * @return the number of documents in the given collection (taking into
 * account the filters in case)
 */
public long getCollectionSize(final MongoCollection<BsonDocument> coll, final BsonDocument filters) {
    return coll.count(filters);
}

From source file:rapture.repo.mongodb.MongoDbDataStore.java

License:Open Source License

@Override
public boolean containsKey(String ref) {
    MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);
    return collection.count(new Document(KEY, ref)) > 0;
}