@Override protected long doCount(String collectionName, Document filter, CountOptions options) { if (!session.hasActiveTransaction()) { return super.doCount(collectionName, filter, options); } return execute(collectionName, collection -> collection.countDocuments(filter, options)); } }
protected long doCount(String collectionName, Document filter, CountOptions options) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName); } if (MongoDatabaseUtils.isTransactionActive(getMongoDbFactory())) { return execute(collectionName, collection -> collection.countDocuments(filter, options)); } return execute(collectionName, collection -> collection.count(filter, options)); }
@Override public Long doInCollection(MongoCollection<Document> collection, Message<?> message) throws MongoException, DataAccessException { return collection.countDocuments(); }
InsertOneOptions insertOptions = new InsertOneOptions().bypassDocumentValidation(true); contacts.insertOne(Document.parse("{ \"name\":\"Jon Snow\"}"), insertOptions); assertThat(db.getCollection("contacts").countDocuments()).isEqualTo(1); InsertOneOptions insertOptions = new InsertOneOptions().bypassDocumentValidation(true); contacts.insertOne(Document.parse("{ \"name\":\"Sally Hamm\"}"), insertOptions); assertThat(db.getCollection("contacts").countDocuments()).isEqualTo(2);
InsertOneOptions insertOptions = new InsertOneOptions().bypassDocumentValidation(true); movies.insertOne(Document.parse("{ \"name\":\"Starter Wars\"}"), insertOptions); assertThat(collection.countDocuments()).isEqualTo(1);
@Override @ManagedAttribute public long getMessageCount() { return this.template.getCollection(this.collectionName).countDocuments(); }
@Override protected long doCount(String collectionName, Document filter, CountOptions options) { if (!session.hasActiveTransaction()) { return super.doCount(collectionName, filter, options); } return execute(collectionName, collection -> collection.countDocuments(filter, options)); } }
protected long doCount(String collectionName, Document filter, CountOptions options) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName); } if (MongoDatabaseUtils.isTransactionActive(getMongoDbFactory())) { return execute(collectionName, collection -> collection.countDocuments(filter, options)); } return execute(collectionName, collection -> collection.count(filter, options)); }
@Override public long getMessageCount() { Query query = Query.query(Criteria.where(MessageDocumentFields.MESSAGE_ID).exists(true) .and(MessageDocumentFields.GROUP_ID).exists(false)); return this.mongoTemplate.getCollection(this.collectionName).countDocuments(query.getQueryObject()); }
@Bean public IntegrationFlow gatewayCollectionCallbackFlow() { return f -> f .handle(collectionCallbackOutboundGateway( (collection, requestMessage) -> collection.countDocuments())) .channel(getResultChannel()); }
/** * Checks if the given collection is empty. Note that RESTHeart creates a * reserved properties document in every collection (with _id * '_properties'). This method returns true even if the collection contains * such document. * * @param coll the mongodb DBCollection object * @return true if the commection is empty */ public boolean isCollectionEmpty(final MongoCollection<BsonDocument> coll) { return coll.countDocuments() == 0; }
@Test @MongoDbAvailable public void testWithCollectionCallbackCount() { Message<String> message = MessageBuilder.withPayload("").build(); MongoDbOutboundGateway gateway = createGateway(); gateway.setEntityClass(Person.class); gateway.setCollectionNameExpression(new LiteralExpression("data")); gateway.setMessageCollectionCallback((collection, requestMessage) -> collection.countDocuments()); gateway.afterPropertiesSet(); long result = (long) gateway.handleRequestMessage(message); assertEquals(4, result); }
@Test public void testUpdateMulti() throws Exception { collection.insertOne(json("a: 1")); collection.insertOne(json("a: 1")); UpdateResult result = collection.updateOne(json("a: 1"), json("$set: {b: 2}")); assertThat(result.getModifiedCount()).isEqualTo(1); assertThat(collection.countDocuments(new Document("b", 2))).isEqualTo(1); result = collection.updateMany(json("a: 1"), json("$set: {b: 3}")); assertThat(result.getModifiedCount()).isEqualTo(2); assertThat(collection.countDocuments(new Document("b", 2))).isEqualTo(0); assertThat(collection.countDocuments(new Document("b", 3))).isEqualTo(2); }
@Override public long countDocuments(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull CountOptions options) { return collection.countDocuments(clientSession, filter, options); }
private static ClosableIterator<Tuple> doCount(MongoDBQueryDescriptor query, MongoCollection<Document> collection) { long count = collection.countDocuments( query.getCriteria() ); MapTupleSnapshot snapshot = new MapTupleSnapshot( Collections.<String, Object>singletonMap( "n", count ) ); return CollectionHelper.newClosableIterator( Collections.singletonList( new Tuple( snapshot, SnapshotType.UNKNOWN ) ) ); }
@Override public int countCompletedJobsToday() { Date today = Date.from(now().truncatedTo(DAYS)); return (int) collection .countDocuments( and( eq(DSL.STATUS, JobStatus.COMPLETED), gte(DSL.END_TIME, today))); }
private static ClosableIterator<Tuple> doCount(MongoDBQueryDescriptor query, MongoCollection<Document> collection) { long count = collection.countDocuments( query.getCriteria() ); MapTupleSnapshot snapshot = new MapTupleSnapshot( Collections.<String, Object>singletonMap( "n", count ) ); return CollectionHelper.newClosableIterator( Collections.singletonList( new Tuple( snapshot, SnapshotType.UNKNOWN ) ) ); }
@Test public void testFineOneAndUpdateRemove() { collection.insertOne(json("_id: 1, a: 1")); Document result = collection.findOneAndDelete(json("_id: 1")); assertThat(result).isEqualTo(json("_id: 1, a: 1")); assertThat(collection.countDocuments()).isZero(); }
@Test public void testFindAndRemoveFromEmbeddedList() { collection.insertOne(json("_id: 1, a: [1]")); Document result = collection.findOneAndDelete(json("_id: 1")); assertThat(result).isEqualTo(json("_id: 1, a: [1]")); assertThat(collection.countDocuments()).isZero(); }
@Test public void testRenameCollection() throws Exception { collection.insertOne(json("_id: 1")); collection.insertOne(json("_id: 2")); collection.insertOne(json("_id: 3")); collection.renameCollection(new MongoNamespace(collection.getNamespace().getDatabaseName(), "other-collection-name")); Collection<String> collectionNames = toArray(db.listCollectionNames()); assertThat(collectionNames).containsExactly("other-collection-name"); assertThat(getCollection("other-collection-name").countDocuments()).isEqualTo(3); }