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

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

本文整理了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

@Override
public Document getFieldsObject() {
  if (!this.includeScore) {
    return super.getFieldsObject();
  }
  Document fields = super.getFieldsObject();
  fields.put(getScoreFieldName(), META_TEXT_SCORE);
  return fields;
}

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

@Override
public Document getFieldsObject() {
  Document combinedFieldsObject = new Document();
  combinedFieldsObject.putAll(fieldsObject);
  combinedFieldsObject.putAll(super.getFieldsObject());
  return combinedFieldsObject;
}

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

@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));
}

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

@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);
}

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

@Nullable
@Override
public <T> T findOne(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!");
  if (ObjectUtils.isEmpty(query.getSortObject()) && !query.getCollation().isPresent()) {
    return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
  } else {
    query.limit(1);
    List<T> results = find(query, entityClass, collectionName);
    return results.isEmpty() ? null : results.get(0);
  }
}

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

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));
}

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

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));
}

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

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));
}

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

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

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

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

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

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

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

@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));
}

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

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();
}

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

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);
}

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

@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;
}

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

@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;
}

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

@Test
public void testFields() throws Exception {
  MongoItemReader<String> reader = getBasicBuilder()
      .fields("{name : 1, age : 1, _id: 0}")
      .build();
  when(this.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(1, query.getFieldsObject().get("name"));
  assertEquals(1, query.getFieldsObject().get("age"));
  assertEquals(0, query.getFieldsObject().get("_id"));
}

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

@Test
public void testQueryWithFields() {
  reader.setFields("{name : 1, age : 1, _id: 0}");
  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());
  assertEquals(1, query.getFieldsObject().get("name"));
  assertEquals(1, query.getFieldsObject().get("age"));
  assertEquals(0, query.getFieldsObject().get("_id"));
}

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

@Test
public void testBasicQuerySecondPage() {
  reader.page = 2;
  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(100, query.getSkip());
  assertEquals("{ }", query.getQueryObject().toJson());
  assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  assertTrue(query.getFieldsObject().isEmpty());
}

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

@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));
  }
});

相关文章