Java Code Examples for com.mongodb.client.MongoCollection#countDocuments()

The following examples show how to use com.mongodb.client.MongoCollection#countDocuments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Mongo.java    From xian with Apache License 2.0 7 votes vote down vote up
public static <T> Page<T> findPageByPageNumber(MongoCollection<T> collection, Bson filter, int pageNumber, int pageSize) {
    long total = collection.countDocuments(filter);
    Page<T> page = new Page<>();
    page.setPageSize(pageSize);
    int totalPage = new Double(Math.ceil((double) total / pageSize)).intValue();
    page.setTotalPage(totalPage);
    if (totalPage < pageNumber && totalPage != 0) {
        pageNumber = totalPage;
    }
    page.setPageNumber(pageNumber);
    page.setTotal(total);
    int skip = (pageNumber - 1) * pageSize;
    collection.find(filter).skip(skip).limit(pageSize).forEach((Consumer<T>) page.getList()::add);
    return page;
}
 
Example 2
Source File: MongoDbDAO.java    From MtgDesktopCompanion with GNU General Public License v3.0 7 votes vote down vote up
private Object getNextSequence() {
	MongoCollection<Document> countersCollection = db.getCollection("idSequences");
	if (countersCollection.countDocuments() == 0) {
		createCountersCollection(countersCollection);
	}
	Document searchQuery = new Document("_id", "stock_increment");
	Document increase = new Document("seq", 1);
	Document updateQuery = new Document("$inc", increase);
	Document result = countersCollection.findOneAndUpdate(searchQuery, updateQuery);
	return result.get("seq");
}
 
Example 3
Source File: MongoAdapterTest.java    From calcite with Apache License 2.0 7 votes vote down vote up
@BeforeAll
public static void setUp() throws Exception {
  MongoDatabase database = POLICY.database();

  populate(database.getCollection("zips"), MongoAdapterTest.class.getResource("/zips-mini.json"));
  populate(database.getCollection("store"), FoodmartJson.class.getResource("/store.json"));
  populate(database.getCollection("warehouse"),
      FoodmartJson.class.getResource("/warehouse.json"));

  // Manually insert data for data-time test.
  MongoCollection<BsonDocument> datatypes =  database.getCollection("datatypes")
      .withDocumentClass(BsonDocument.class);
  if (datatypes.countDocuments() > 0) {
    datatypes.deleteMany(new BsonDocument());
  }

  BsonDocument doc = new BsonDocument();
  Instant instant = LocalDate.of(2012, 9, 5).atStartOfDay(ZoneOffset.UTC).toInstant();
  doc.put("date", new BsonDateTime(instant.toEpochMilli()));
  doc.put("value", new BsonInt32(1231));
  doc.put("ownerId", new BsonString("531e7789e4b0853ddb861313"));
  datatypes.insertOne(doc);

  schema = new MongoSchema(database);
}
 
Example 4
Source File: Mongo.java    From xian with Apache License 2.0 6 votes vote down vote up
public static <T> Page<T> findPageByPageNumber(MongoCollection<T> collection, Bson filter, Bson sort, int pageNumber, int pageSize) {
    long total = collection.countDocuments(filter);

    Page<T> page = new Page<>();
    page.setPageSize(pageSize);
    int totalPage = new Double(Math.ceil((double) total / pageSize)).intValue();
    page.setTotalPage(totalPage);
    if (totalPage < pageNumber && totalPage != 0) {
        pageNumber = totalPage;
    }
    page.setPageNumber(pageNumber);
    page.setTotal(total);
    int skip = (pageNumber - 1) * pageSize;
    collection.find(filter).sort(sort).skip(skip).limit(pageSize).forEach((Consumer<T>) page.getList()::add);
    return page;
}
 
Example 5
Source File: MongoAdapterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static void populate(MongoCollection<Document> collection, URL resource)
    throws IOException {
  Objects.requireNonNull(collection, "collection");

  if (collection.countDocuments() > 0) {
    // delete any existing documents (run from a clean set)
    collection.deleteMany(new BsonDocument());
  }

  MongoCollection<BsonDocument> bsonCollection = collection.withDocumentClass(BsonDocument.class);
  Resources.readLines(resource, StandardCharsets.UTF_8, new LineProcessor<Void>() {
    @Override public boolean processLine(String line) throws IOException {
      bsonCollection.insertOne(BsonDocument.parse(line));
      return true;
    }

    @Override public Void getResult() {
      return null;
    }
  });
}
 
Example 6
Source File: Mongo.java    From xian with Apache License 2.0 5 votes vote down vote up
public static <T> Page<T> findPageBySkip(MongoCollection<T> collection, Bson filter, long skip, long limit) {
    long total = collection.countDocuments(filter);
    Page<T> page = new Page<>();
    page.setPageSize(new Long(limit).intValue());
    page.setTotalPage(Long.valueOf(total / limit).intValue());
    page.setPageNumber(Long.valueOf(skip / limit + 1).intValue());
    page.setTotal(total);
    collection.find(filter).forEach((Consumer<T>) page.getList()::add);
    return page;
}
 
Example 7
Source File: Mongo.java    From xian with Apache License 2.0 5 votes vote down vote up
public static <T> Page<T> findPageBySkip(MongoCollection<T> collection, Bson filter, Bson sort, long skip, long limit) {
    long total = collection.countDocuments(filter);
    Page<T> page = new Page<>();
    page.setPageSize(new Long(limit).intValue());
    page.setTotalPage(Long.valueOf(total / limit).intValue());
    page.setPageNumber(Long.valueOf(skip / limit + 1).intValue());
    page.setTotal(total);
    collection.find(filter).sort(sort).forEach((Consumer<T>) page.getList()::add);
    return page;
}
 
Example 8
Source File: DBusMongoClient.java    From DBus with Apache License 2.0 5 votes vote down vote up
public boolean isShardCollection(String db, String collection) {
    boolean ret = false;
    if (shard) {
        MongoDatabase mdb = mongoClient.getDatabase("config");
        MongoCollection mongoCollection = mdb.getCollection("collections");
        String id = StringUtils.join(new String[]{db, collection}, ".");
        Document condition = new Document("_id", id).append("dropped", false);
        ret = (mongoCollection.countDocuments(condition) > 0);
        logger.info("db:{}, collection:{} is shard collection.", db, collection);
    }
    return ret;
}
 
Example 9
Source File: CamelSinkMongoDBITCase.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
private boolean hasAllRecords(MongoCollection<Document> collection) {
    return collection.countDocuments() >= expect;
}
 
Example 10
Source File: MongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static long count(Class<?> entityClass) {
    MongoCollection collection = mongoCollection(entityClass);
    return collection.countDocuments();
}
 
Example 11
Source File: MongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static long count(Class<?> entityClass, String query, Object... params) {
    String bindQuery = bindFilter(entityClass, query, params);
    BsonDocument docQuery = BsonDocument.parse(bindQuery);
    MongoCollection collection = mongoCollection(entityClass);
    return collection.countDocuments(docQuery);
}
 
Example 12
Source File: MongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static long count(Class<?> entityClass, String query, Map<String, Object> params) {
    String bindQuery = bindFilter(entityClass, query, params);
    BsonDocument docQuery = BsonDocument.parse(bindQuery);
    MongoCollection collection = mongoCollection(entityClass);
    return collection.countDocuments(docQuery);
}
 
Example 13
Source File: MongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static long count(Class<?> entityClass, Document query) {
    MongoCollection collection = mongoCollection(entityClass);
    return collection.countDocuments(query);
}
 
Example 14
Source File: MongoRyaInstanceDetailsRepository.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isInitialized() throws RyaDetailsRepositoryException {
    final MongoCollection<Document> col = db.getCollection(INSTANCE_DETAILS_COLLECTION_NAME);
    return col.countDocuments() == 1;
}