@Override public BulkOperations remove(Query query) { Assert.notNull(query, "Query must not be null!"); DeleteOptions deleteOptions = new DeleteOptions(); query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation); models.add(new DeleteManyModel<>(query.getQueryObject(), deleteOptions)); return this; }
@Override public <T> List<T> find(Query query, Class<T> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); Assert.notNull(entityClass, "EntityClass must not be null!"); return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, new QueryCursorPreparer(query, entityClass)); }
public long count(Query query, @Nullable Class<?> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); CountOptions options = new CountOptions(); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); Document document = queryMapper.getMappedObject(query.getQueryObject(), Optional.ofNullable(entityClass).map(it -> mappingContext.getPersistentEntity(entityClass))); return doCount(collectionName, document, options); }
@Nullable @Override public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(entityClass, "EntityClass must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), getMappedSortObject(query, entityClass), query.getCollation().orElse(null), entityClass); }
private Flux<T> doFind(@Nullable FindPublisherPreparer preparer) { Document queryObject = query.getQueryObject(); Document fieldsObject = query.getFieldsObject(); return template.doFind(getCollectionName(), queryObject, fieldsObject, domainType, returnType, preparer != null ? preparer : getCursorPreparer(query)); }
public <T> Flux<T> find(@Nullable Query query, Class<T> entityClass, String collectionName) { if (query == null) { return findAll(entityClass, collectionName); } return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, new QueryFindPublisherPreparer(query, entityClass)); }
private List<T> doFind(@Nullable CursorPreparer preparer) { Document queryObject = query.getQueryObject(); Document fieldsObject = query.getFieldsObject(); return template.doFind(getCollectionName(), queryObject, fieldsObject, domainType, returnType, getCursorPreparer(query, preparer)); }
@Override public <T> Flux<T> tail(@Nullable Query query, Class<T> entityClass, String collectionName) { if (query == null) { // TODO: clean up LOGGER.debug(String.format("find for class: %s in collection: %s", entityClass, collectionName)); return executeFindMultiInternal( collection -> new FindCallback(null).doInCollection(collection).cursorType(CursorType.TailableAwait), null, new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName), collectionName); } return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, new TailingQueryFindPublisherPreparer(query, entityClass)); }
public <T> Mono<T> findOne(Query query, Class<T> entityClass, String collectionName) { if (ObjectUtils.isEmpty(query.getSortObject())) { return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, query.getCollation().orElse(null)); } query.limit(1); return find(query, entityClass, collectionName).next(); }
public <T> Mono<T> findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass, String collectionName) { FindAndModifyOptions optionsToUse = FindAndModifyOptions.of(options); Optionals.ifAllPresent(query.getCollation(), optionsToUse.getCollation(), (l, r) -> { throw new IllegalArgumentException( "Both Query and FindAndModifyOptions define a collation. Please provide the collation only via one of the two."); }); query.getCollation().ifPresent(optionsToUse::collation); return doFindAndModify(collectionName, query.getQueryObject(), query.getFieldsObject(), getMappedSortObject(query, entityClass), entityClass, update, optionsToUse); }
@Override public List<MongoEndpointNotification> findNotificationsByKeyHash(final byte[] keyHash) { LOG.debug("Find unicast notifications by endpoint key hash [{}] ", keyHash); DBObject dbObject = query(where(EP_ENDPOINT_KEY_HASH).is(keyHash)).getQueryObject(); DBCursor cursor = mongoTemplate.getDb().getCollection(getCollectionName()).find(dbObject); List<MongoEndpointNotification> endpointNotifications = new ArrayList<>(); while (cursor.hasNext()) { endpointNotifications.add(mongoTemplate.getConverter() .read(MongoEndpointNotification.class, cursor.next())); } return endpointNotifications; }
@Override protected Query createQuery(ConvertingParameterAccessor accessor) { String queryString = parameterBinder.bind(this.query, accessor, new BindingContext(getQueryMethod().getParameters(), queryParameterBindings)); String fieldsString = parameterBinder.bind(this.fieldSpec, accessor, new BindingContext(getQueryMethod().getParameters(), fieldSpecParameterBindings)); Query query = new BasicQuery(queryString, fieldsString).with(accessor.getSort()); if (LOG.isDebugEnabled()) { LOG.debug(String.format("Created query %s for %s fields.", query.getQueryObject(), query.getFieldsObject())); } return query; }
protected GridFSFindPublisher prepareQuery(Query query) { Assert.notNull(query, "Query must not be null!"); Document queryObject = getMappedQuery(query.getQueryObject()); Document sortObject = getMappedQuery(query.getSortObject()); GridFSFindPublisher publisherToUse = getGridFs().find(queryObject).sort(sortObject); Integer cursorBatchSize = query.getMeta().getCursorBatchSize(); if (cursorBatchSize != null) { publisherToUse = publisherToUse.batchSize(cursorBatchSize); } return publisherToUse; }
@Override public MongoEndpointProfile findByKeyHash(byte[] endpointKeyHash) { LOG.debug("Find endpoint profile by endpoint key hash [{}] ", endpointKeyHash); DBObject dbObject = query(where(EP_ENDPOINT_KEY_HASH) .is(endpointKeyHash)) .getQueryObject(); DBObject result = mongoTemplate.getDb() .getCollection(getCollectionName()) .findOne(dbObject); return mongoTemplate.getConverter().read(getDocumentClass(), result); }
@Override public MongoTopicListEntry findByHash(byte[] hash) { LOG.debug("Find topic list entry by hash [{}] ", hash); DBObject dbObject = query(where(ID).is(hash)).getQueryObject(); DBObject result = mongoTemplate.getDb() .getCollection(getCollectionName()) .findOne(dbObject); return mongoTemplate.getConverter().read(getDocumentClass(), result); }
@Override public MongoEndpointConfiguration findByHash(final byte[] hash) { LOG.debug("Find endpoint configuration by hash [{}] ", hash); DBObject dbObject = query(where(ID).is(hash)).getQueryObject(); DBObject result = mongoTemplate.getDb() .getCollection(getCollectionName()) .findOne(dbObject); return mongoTemplate.getConverter().read(getDocumentClass(), result); }
@Override public MongoEndpointProfile findByAccessToken(String endpointAccessToken) { LOG.debug("Find endpoint profile by access token [{}] ", endpointAccessToken); DBObject dbObject = query(where(EP_ACCESS_TOKEN).is(endpointAccessToken)) .getQueryObject(); DBObject result = mongoTemplate.getDb() .getCollection(getCollectionName()) .findOne(dbObject); return mongoTemplate.getConverter().read(getDocumentClass(), result); }
@Test public void testBasicQueryFirstPage() { ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class); when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>()); assertFalse(reader.doPageRead().hasNext()); Query query = queryContainer.getValue(); assertEquals(50, query.getLimit()); assertEquals(0, query.getSkip()); assertEquals("{ }", query.getQueryObject().toJson()); assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson()); }
@Test public void testBasic() throws Exception { MongoItemReader<String> reader = getBasicBuilder().build(); when(template.find(this.queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>()); assertNull("reader should not return result", reader.read()); Query query = this.queryContainer.getValue(); assertEquals(50, query.getLimit()); assertEquals(0, query.getSkip()); assertEquals("{ }", query.getQueryObject().toJson()); assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson()); }
@Override public CloseableIterator<T> doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { MongoPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityType); Document mappedFields = getMappedFieldsObject(query.getFieldsObject(), persistentEntity, returnType); Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), persistentEntity); FindIterable<Document> cursor = new QueryCursorPreparer(query, entityType) .prepare(collection.find(mappedQuery, Document.class).projection(mappedFields)); return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator, new ProjectingReadCallback<>(mongoConverter, entityType, returnType, collectionName)); } });