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

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

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

Query.getSortObject介绍

暂无

代码示例

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

private Document getMappedSortObject(Query query, Class<?> type) {
  if (query == null || ObjectUtils.isEmpty(query.getSortObject())) {
    return null;
  }
  return queryMapper.getMappedSort(query.getSortObject(), mappingContext.getPersistentEntity(type));
}

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

@Override
public Document getSortObject() {
  Document result = new Document();
  result.putAll(sortObject);
  Document overrides = super.getSortObject();
  result.putAll(overrides);
  return result;
}

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

private Document getMappedSortObject(Query query, Class<?> type) {
  if (query == null) {
    return null;
  }
  return queryMapper.getMappedSort(query.getSortObject(), mappingContext.getPersistentEntity(type));
}

代码示例来源: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

@Override
  public Document getSortObject() {

    Document sort = new Document();

    if (this.sortByScore) {
      sort.put(getScoreFieldName(), META_TEXT_SCORE);
    }

    sort.putAll(super.getSortObject());

    return sort;
  }
}

代码示例来源: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

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

@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 <S, T> 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, "EntityType 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

public GridFSFindIterable find(Query query) {
  Assert.notNull(query, "Query must not be null!");
  Document queryObject = getMappedQuery(query.getQueryObject());
  Document sortObject = getMappedQuery(query.getSortObject());
  return getGridFs().find(queryObject).sort(sortObject);
}

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

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

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

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

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

@Test
public void testCollection() throws Exception {
  MongoItemReader<String> reader = getBasicBuilder()
      .parameterValues(Collections.singletonList("foo"))
      .jsonQuery("{ name : ?0 }")
      .collection("collection")
      .build();
  ArgumentCaptor<String> collectionContainer = ArgumentCaptor.forClass(String.class);
  when(this.template.find(this.queryContainer.capture(), eq(String.class), collectionContainer.capture()))
      .thenReturn(new ArrayList<>());
  assertNull("reader should not return result", reader.read());
  Query query = this.queryContainer.getValue();
  assertEquals("{ \"name\" : \"foo\" }", query.getQueryObject().toJson());
  assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  assertEquals("collection", collectionContainer.getValue());
}

代码示例来源: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-batch

@Test
public void testQueryWithHint() {
  reader.setHint("{ $natural : 1}");
  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("{ $natural : 1}", query.getHint());
}

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

@Test
public void testVarargs() throws Exception {
  MongoItemReader<String> reader = getBasicBuilder()
      .parameterValues("foo")
      .jsonQuery("{ name : ?0 }")
      .collection("collection")
      .build();
  ArgumentCaptor<String> collectionContainer = ArgumentCaptor.forClass(String.class);
  when(this.template.find(this.queryContainer.capture(), eq(String.class), collectionContainer.capture()))
      .thenReturn(new ArrayList<>());
  assertNull("reader should not return result", reader.read());
  Query query = this.queryContainer.getValue();
  assertEquals("{ \"name\" : \"foo\" }", query.getQueryObject().toJson());
  assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  assertEquals("collection", collectionContainer.getValue());
}

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

@SuppressWarnings("serial")
@Test
public void testQueryWithParameters() {
  reader.setParameterValues(new ArrayList<Object>(){{
    add("foo");
  }});
  reader.setQuery("{ name : ?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("{ \"name\" : \"foo\" }", query.getQueryObject().toJson());
  assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
}

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

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

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

@SuppressWarnings("serial")
@Test
public void testQueryWithCollection() {
  reader.setParameterValues(new ArrayList<Object>(){{
    add("foo");
  }});
  reader.setQuery("{ name : ?0 }");
  reader.setCollection("collection");
  ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  ArgumentCaptor<String> collectionContainer = ArgumentCaptor.forClass(String.class);
  when(template.find(queryContainer.capture(), eq(String.class), collectionContainer.capture())).thenReturn(new ArrayList<>());
  assertFalse(reader.doPageRead().hasNext());
  Query query = queryContainer.getValue();
  assertEquals(50, query.getLimit());
  assertEquals(0, query.getSkip());
  assertEquals("{ \"name\" : \"foo\" }", query.getQueryObject().toJson());
  assertEquals("{ \"name\" : -1 }", query.getSortObject().toJson());
  assertEquals("collection", collectionContainer.getValue());
}

代码示例来源: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"));
}

相关文章