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:com.bluedragon.mongo.MongoCollectionCount.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData queryData = getNamedParam(argStruct, "query", null);

    try {//from w  w  w  .j a va2 s . co m
        Document qry = null;
        int count = 0;
        long start = System.currentTimeMillis();
        MongoCollection<Document> col = db.getCollection(collection);

        if (queryData != null) {
            qry = getDocument(queryData);
            count = (int) col.count(qry);
        } else
            count = (int) col.count();

        _session.getDebugRecorder().execMongo(col, "count", qry, System.currentTimeMillis() - start);

        return new cfNumberData(count);

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public long count(Query<?> query) {
    checkArgument(query != null, "Query must not be null");
    MongoDatabase db = factory.getDB();// ww w . ja va  2s.c o m
    MongoCollection<Document> collection = db.getCollection(query.getPlace());
    return collection.count(createDocument(query));
}

From source file:com.eclipsesource.connect.test.util.persistence.InMemoryStorage.java

License:Open Source License

@Override
public long count(Query<?> query) {
    checkArgument(query != null, "Query must not be null");
    MongoCollection<Document> collection = db.getCollection(query.getPlace());
    return collection.count(createDocument(query));
}

From source file:com.futuremove.cacheServer.service.impl.CarDynPropsServiceImpl.java

public Long countNearByNeededCar(Long maxDistance, CarDynProps centerFilter) {
    if (maxDistance < 200L)
        maxDistance = 200L;/* www. j a v  a  2  s  .c  om*/
    MongoCollection<Document> collection = this.getCollection();
    Document filterDoc = centerFilter.toDocument();
    Document locFilter = new Document();
    locFilter.put("$near", new Document().append("$geometry", centerFilter.location.toDocument())
            .append("$maxDistance", maxDistance));
    filterDoc.put("location", locFilter);
    if (centerFilter.dataUpdateTime != null) {
        //?
        filterDoc.put("dataUpdateTime", new Document().append("$gt", centerFilter.dataUpdateTime));
    }
    return collection.count(filterDoc);
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit,
        Integer offset, String sort, List<String> queries, String includes, String excludes,
        Set<String> realms) {
    FindIterable<Document> documents = collection.find();
    MongoDataResultContainer queryDataContainer = new MongoDataResultContainer();

    Bson query = MongoRequestFilters.buildQuery(queries, realms);
    documents = documents.filter(query);
    long count = collection.count(query);
    queryDataContainer.setGetReqCount(count);
    queryDataContainer.setRemainingNumQueryDocuments((int) (count - (limit + offset)));
    documents = buildProjection(documents, includes, excludes);
    final Bson sortObject = MongoSortFilters.createSortObject(sort);
    documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit)
            .cursorType(CursorType.NonTailable);
    queryDataContainer.setQueryDataResult(documents);

    return queryDataContainer;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execPutRequest(MongoCollection<Document> collection, String body,
        List<String> queries, Set<String> realms) {
    Document inputDocument = Document.parse(body);
    MongoDataResultContainer metaDataContainer = new MongoDataResultContainer();

    Document setDocument = inputDocument.get("set", Document.class);
    setDocument.remove(KeycloakFields.REALMS_KEY);
    final Bson fields = new Document("$set", setDocument);

    final Bson bsonQueries = MongoRequestFilters.buildQuery(queries, realms);

    collection.updateMany(bsonQueries, fields);

    metaDataContainer.setPutReqMatches(collection.count(bsonQueries));

    return metaDataContainer;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execDeleteRequest(MongoCollection<Document> collection, List<String> queries,
        Set<String> realms) {
    MongoDataResultContainer metaDataContainer = new MongoDataResultContainer();
    if (queries != null && !queries.isEmpty() || realms != null && !realms.isEmpty()) {
        Bson bsonQueries = MongoRequestFilters.buildQuery(queries, realms);
        metaDataContainer.setDeleteReqMatches(collection.count(bsonQueries));
        collection.deleteMany(bsonQueries);
    } else {/*from   w w w  . ja v a 2 s .co  m*/
        metaDataContainer.setDeleteReqMatches(collection.count());
        collection.drop();
    }

    return metaDataContainer;
}

From source file:com.sitewhere.event.persistence.mongodb.MongoDeviceEventManagement.java

License:Open Source License

@Override
public ISearchResults<IDeviceEvent> listDeviceEvents(IDeviceAssignment assignment,
        IDateRangeSearchCriteria criteria) throws SiteWhereException {
    MongoCollection<Document> events = getMongoClient().getEventsCollection();
    Document query = new Document(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, assignment.getId());
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    Document sort = new Document(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);

    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    FindIterable<Document> found = events.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    MongoCursor<Document> cursor = found.iterator();

    List<IDeviceEvent> matches = new ArrayList<IDeviceEvent>();
    SearchResults<IDeviceEvent> results = new SearchResults<IDeviceEvent>(matches);
    try {/* ww  w .j av a  2  s. c  o m*/
        results.setNumResults(events.count(query));
        while (cursor.hasNext()) {
            Document match = cursor.next();
            matches.add(MongoDeviceEventManagementPersistence.unmarshalEvent(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:DS.Model.java

private void logRestaurant(String name, double rating, String address) {
    //Get the restaurant collection
    MongoCollection<Document> collection = db.getCollection("restaurants");

    //Check to see if the restaurant already exists
    long count = collection.count(eq("name", name));

    //if the restuarant doesn't exist, add it as a new restaurant
    if (count == 0) {
        Document doc1 = new Document("name", name).append("rating", rating).append("address", address);
        collection.insertOne(doc1);//from   ww w.j a  v a 2s. co  m
    }
}

From source file:dto.Dto.java

public boolean existe(String user, String psw) {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("usuarioxactividad");
    long count = col.count(and(eq("usuario", user), eq("contrasea", psw)));
    System.out.println(c);/*from  w w  w  . j  a  v a 2s. c  o m*/
    if (count == 1) {
        return true;
    } else {
        return false;
    }
}