org.springframework.data.mongodb.core.query.Query.getFieldsObject()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(228)

本文整理了Java中org.springframework.data.mongodb.core.query.Query.getFieldsObject方法的一些代码示例,展示了Query.getFieldsObject的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.getFieldsObject方法的具体详情如下:
包路径:org.springframework.data.mongodb.core.query.Query
类名称:Query
方法名:getFieldsObject

Query.getFieldsObject介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public Document getFieldsObject() {
  3. if (!this.includeScore) {
  4. return super.getFieldsObject();
  5. }
  6. Document fields = super.getFieldsObject();
  7. fields.put(getScoreFieldName(), META_TEXT_SCORE);
  8. return fields;
  9. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public Document getFieldsObject() {
  3. Document combinedFieldsObject = new Document();
  4. combinedFieldsObject.putAll(fieldsObject);
  5. combinedFieldsObject.putAll(super.getFieldsObject());
  6. return combinedFieldsObject;
  7. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public <T> List<T> find(Query query, Class<T> entityClass, String collectionName) {
  3. Assert.notNull(query, "Query must not be null!");
  4. Assert.notNull(collectionName, "CollectionName must not be null!");
  5. Assert.notNull(entityClass, "EntityClass must not be null!");
  6. return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
  7. new QueryCursorPreparer(query, entityClass));
  8. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Nullable
  2. @Override
  3. public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName) {
  4. Assert.notNull(query, "Query must not be null!");
  5. Assert.notNull(entityClass, "EntityClass must not be null!");
  6. Assert.notNull(collectionName, "CollectionName must not be null!");
  7. return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(),
  8. getMappedSortObject(query, entityClass), query.getCollation().orElse(null), entityClass);
  9. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Nullable
  2. @Override
  3. public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
  4. Assert.notNull(query, "Query must not be null!");
  5. Assert.notNull(entityClass, "EntityClass must not be null!");
  6. Assert.notNull(collectionName, "CollectionName must not be null!");
  7. if (ObjectUtils.isEmpty(query.getSortObject()) && !query.getCollation().isPresent()) {
  8. return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
  9. } else {
  10. query.limit(1);
  11. List<T> results = find(query, entityClass, collectionName);
  12. return results.isEmpty() ? null : results.get(0);
  13. }
  14. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. private List<T> doFind(@Nullable CursorPreparer preparer) {
  2. Document queryObject = query.getQueryObject();
  3. Document fieldsObject = query.getFieldsObject();
  4. return template.doFind(getCollectionName(), queryObject, fieldsObject, domainType, returnType,
  5. getCursorPreparer(query, preparer));
  6. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. private Flux<T> doFind(@Nullable FindPublisherPreparer preparer) {
  2. Document queryObject = query.getQueryObject();
  3. Document fieldsObject = query.getFieldsObject();
  4. return template.doFind(getCollectionName(), queryObject, fieldsObject, domainType, returnType,
  5. preparer != null ? preparer : getCursorPreparer(query));
  6. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. public <T> Flux<T> find(@Nullable Query query, Class<T> entityClass, String collectionName) {
  2. if (query == null) {
  3. return findAll(entityClass, collectionName);
  4. }
  5. return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
  6. new QueryFindPublisherPreparer(query, entityClass));
  7. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. public <T> Mono<T> findAndRemove(Query query, Class<T> entityClass, String collectionName) {
  2. return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(),
  3. getMappedSortObject(query, entityClass), query.getCollation().orElse(null), entityClass);
  4. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public String toString() {
  3. return String.format("Query: %s, Fields: %s, Sort: %s", serializeToJsonSafely(getQueryObject()),
  4. serializeToJsonSafely(getFieldsObject()), serializeToJsonSafely(getSortObject()));
  5. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType,
  3. String collectionName, Class<T> resultType) {
  4. Assert.notNull(query, "Query must not be null!");
  5. Assert.notNull(replacement, "Replacement must not be null!");
  6. Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead.");
  7. Assert.notNull(entityType, "Entity class must not be null!");
  8. Assert.notNull(collectionName, "CollectionName must not be null!");
  9. Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead.");
  10. Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!");
  11. Assert.isTrue(query.getSkip() <= 0, "Query must not define skip.");
  12. MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
  13. Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), entity);
  14. Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), entity);
  15. Document mappedSort = queryMapper.getMappedSort(query.getSortObject(), entity);
  16. Document mappedReplacement = operations.forEntity(replacement).toMappedDocument(this.mongoConverter).getDocument();
  17. return doFindAndReplace(collectionName, mappedQuery, mappedFields, mappedSort,
  18. query.getCollation().map(Collation::toMongoCollation).orElse(null), entityType, mappedReplacement, options,
  19. resultType);
  20. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public <T> Flux<T> tail(@Nullable Query query, Class<T> entityClass, String collectionName) {
  3. if (query == null) {
  4. // TODO: clean up
  5. LOGGER.debug(String.format("find for class: %s in collection: %s", entityClass, collectionName));
  6. return executeFindMultiInternal(
  7. collection -> new FindCallback(null).doInCollection(collection).cursorType(CursorType.TailableAwait), null,
  8. new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName), collectionName);
  9. }
  10. return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
  11. new TailingQueryFindPublisherPreparer(query, entityClass));
  12. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. public <T> Mono<T> findOne(Query query, Class<T> entityClass, String collectionName) {
  2. if (ObjectUtils.isEmpty(query.getSortObject())) {
  3. return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
  4. query.getCollation().orElse(null));
  5. }
  6. query.limit(1);
  7. return find(query, entityClass, collectionName).next();
  8. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. public <T> Mono<T> findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass,
  2. String collectionName) {
  3. FindAndModifyOptions optionsToUse = FindAndModifyOptions.of(options);
  4. Optionals.ifAllPresent(query.getCollation(), optionsToUse.getCollation(), (l, r) -> {
  5. throw new IllegalArgumentException(
  6. "Both Query and FindAndModifyOptions define a collation. Please provide the collation only via one of the two.");
  7. });
  8. query.getCollation().ifPresent(optionsToUse::collation);
  9. return doFindAndModify(collectionName, query.getQueryObject(), query.getFieldsObject(),
  10. getMappedSortObject(query, entityClass), entityClass, update, optionsToUse);
  11. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. protected Query createQuery(ConvertingParameterAccessor accessor) {
  3. String queryString = parameterBinder.bind(this.query, accessor,
  4. new BindingContext(getQueryMethod().getParameters(), queryParameterBindings));
  5. String fieldsString = parameterBinder.bind(this.fieldSpec, accessor,
  6. new BindingContext(getQueryMethod().getParameters(), fieldSpecParameterBindings));
  7. Query query = new BasicQuery(queryString, fieldsString).with(accessor.getSort());
  8. if (LOG.isDebugEnabled()) {
  9. LOG.debug(String.format("Created query %s for %s fields.", query.getQueryObject(), query.getFieldsObject()));
  10. }
  11. return query;
  12. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. protected Query createQuery(ConvertingParameterAccessor accessor) {
  3. String queryString = parameterBinder.bind(this.query, accessor,
  4. new BindingContext(getQueryMethod().getParameters(), queryParameterBindings));
  5. String fieldsString = parameterBinder.bind(this.fieldSpec, accessor,
  6. new BindingContext(getQueryMethod().getParameters(), fieldSpecParameterBindings));
  7. Query query = new BasicQuery(queryString, fieldsString).with(accessor.getSort());
  8. if (LOG.isDebugEnabled()) {
  9. LOG.debug(String.format("Created query %s for %s fields.", query.getQueryObject(), query.getFieldsObject()));
  10. }
  11. return query;
  12. }

代码示例来源:origin: spring-projects/spring-batch

  1. @Test
  2. public void testFields() throws Exception {
  3. MongoItemReader<String> reader = getBasicBuilder()
  4. .fields("{name : 1, age : 1, _id: 0}")
  5. .build();
  6. when(this.template.find(this.queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  7. assertNull("reader should not return result", reader.read());
  8. Query query = this.queryContainer.getValue();
  9. assertEquals(1, query.getFieldsObject().get("name"));
  10. assertEquals(1, query.getFieldsObject().get("age"));
  11. assertEquals(0, query.getFieldsObject().get("_id"));
  12. }

代码示例来源:origin: spring-projects/spring-batch

  1. @Test
  2. public void testQueryWithFields() {
  3. reader.setFields("{name : 1, age : 1, _id: 0}");
  4. ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  5. when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  6. assertFalse(reader.doPageRead().hasNext());
  7. Query query = queryContainer.getValue();
  8. assertEquals(50, query.getLimit());
  9. assertEquals(0, query.getSkip());
  10. assertEquals("{ }", query.getQueryObject().toJson());
  11. assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  12. assertEquals(1, query.getFieldsObject().get("name"));
  13. assertEquals(1, query.getFieldsObject().get("age"));
  14. assertEquals(0, query.getFieldsObject().get("_id"));
  15. }

代码示例来源:origin: spring-projects/spring-batch

  1. @Test
  2. public void testBasicQuerySecondPage() {
  3. reader.page = 2;
  4. ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  5. when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  6. assertFalse(reader.doPageRead().hasNext());
  7. Query query = queryContainer.getValue();
  8. assertEquals(50, query.getLimit());
  9. assertEquals(100, query.getSkip());
  10. assertEquals("{ }", query.getQueryObject().toJson());
  11. assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  12. assertTrue(query.getFieldsObject().isEmpty());
  13. }

代码示例来源:origin: spring-projects/spring-data-mongodb

  1. @Override
  2. public CloseableIterator<T> doInCollection(MongoCollection<Document> collection)
  3. throws MongoException, DataAccessException {
  4. MongoPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityType);
  5. Document mappedFields = getMappedFieldsObject(query.getFieldsObject(), persistentEntity, returnType);
  6. Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), persistentEntity);
  7. FindIterable<Document> cursor = new QueryCursorPreparer(query, entityType)
  8. .prepare(collection.find(mappedQuery, Document.class).projection(mappedFields));
  9. return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator,
  10. new ProjectingReadCallback<>(mongoConverter, entityType, returnType, collectionName));
  11. }
  12. });

相关文章