org.springframework.data.mongodb.core.query.Query类的使用及代码示例

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

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

Query介绍

[英]MongoDB Query object representing criteria, projection, sorting and query hints.
[中]MongoDB查询对象,表示条件、投影、排序和查询提示。

代码示例

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

@Override
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
  Assert.notNull(example, "Sample must not be null!");
  Assert.notNull(sort, "Sort must not be null!");
  Query q = new Query(new Criteria().alike(example)).with(sort);
  return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName());
}

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

@Override
public boolean exists(String scriptName) {
  Assert.hasText(scriptName, "ScriptName must not be null or empty!");
  return mongoOperations.exists(query(where("_id").is(scriptName)), NamedMongoScript.class, SCRIPT_COLLECTION_NAME);
}

代码示例来源:origin: yu199195/myth

@Override
public int remove(final String transId) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(transId));
  template.remove(query, collectionName);
  return CommonConstant.SUCCESS;
}

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

@Override
protected Query complete(Criteria criteria, Sort sort) {
  Query query = (criteria == null ? new Query() : new Query(criteria)).with(sort);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Created query " + query);
  }
  return query;
}

代码示例来源:origin: yu199195/hmily

@Override
public List<HmilyTransaction> listAllByDelay(final Date date) {
  Query query = new Query();
  query.addCriteria(Criteria.where("lastTime").lt(date));
  final List<MongoAdapter> mongoBeans =
      template.find(query, MongoAdapter.class, collectionName);
  if (CollectionUtils.isNotEmpty(mongoBeans)) {
    return mongoBeans.stream().map(this::buildByCache).collect(Collectors.toList());
  }
  return Collections.emptyList();
}

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

@Override
public Flux<T> findAll(Sort sort) {
  Assert.notNull(sort, "Sort must not be null!");
  return findAll(new Query().with(sort));
}

代码示例来源:origin: com.bq.oss.lib/mongodb

private boolean doUpdate(Object id, DBObject data, String... fieldsToDelete) {
  Assert.notNull(id);
  data.removeField("_id");
  Update update = new Update();
  setField("", data, update);
  for (String field : fieldsToDelete) {
    update.unset(field);
  }
  WriteResult result = mongoOperations.updateFirst(Query.query(Criteria.where("_id").is(id)), update,
      metadata.getCollectionName());
  return result.getN() != 0;
}

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

@Override
public Flux<T> findAllById(Iterable<ID> ids) {
  Assert.notNull(ids, "The given Iterable of Id's must not be null!");
  return findAll(new Query(new Criteria(entityInformation.getIdAttribute())
      .in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()))));
}

代码示例来源: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 Mono<ReactiveGridFsResource> getResource(String location) {
  Assert.notNull(location, "Filename must not be null!");
  return findOne(query(whereFilename().is(location))).flatMap(this::getResource)
      .defaultIfEmpty(ReactiveGridFsResource.absent(location));
}

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

/**
 * Get the {@code value} for the provided {@code key} performing {@code findOne} MongoDB operation.
 * @param key the metadata entry key
 * @return the metadata entry value or null if doesn't exist.
 * @see MongoTemplate#findOne(Query, Class, String)
 */
@Override
public String get(String key) {
  Assert.hasText(key, "'key' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key));
  query.fields().exclude(ID_FIELD);
  @SuppressWarnings("unchecked")
  Map<String, String> result = this.template.findOne(query, Map.class, this.collectionName);
  return result == null ? null : result.get(VALUE);
}

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

/**
 * Remove the metadata entry for the provided {@code key} and return its {@code value}, if any,
 * using {@code findAndRemove} MongoDB operation.
 * @param key the metadata entry key
 * @return the metadata entry value or null if doesn't exist.
 * @see MongoTemplate#findAndRemove(Query, Class, String)
 */
@Override
public String remove(String key) {
  Assert.hasText(key, "'key' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key));
  query.fields().exclude(ID_FIELD);
  @SuppressWarnings("unchecked")
  Map<String, String> result = this.template.findAndRemove(query, Map.class, this.collectionName);
  return result == null ? null : result.get(VALUE);
}

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

/**
 * If the specified key is not already associated with a value, associate it with the given value.
 * This is equivalent to
 * <pre> {@code
 * if (!map.containsKey(key))
 *   return map.put(key, value);
 * else
 *   return map.get(key);
 * }</pre>
 * except that the action is performed atomically.
 * <p>
 * @param key the metadata entry key
 * @param value the metadata entry value to store
 * @return null if successful, the old value otherwise.
 * @see java.util.concurrent.ConcurrentMap#putIfAbsent(Object, Object)
 */
@Override
public String putIfAbsent(String key, String value) {
  Assert.hasText(key, "'key' must not be empty.");
  Assert.hasText(value, "'value' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key));
  query.fields().exclude(ID_FIELD);
  @SuppressWarnings("unchecked")
  Map<String, String> result = this.template.findAndModify(query, new Update().setOnInsert(VALUE, value),
      new FindAndModifyOptions().upsert(true), Map.class, this.collectionName);
  return result == null ? null : result.get(VALUE);
}

代码示例来源:origin: yu199195/hmily

final PageParameter pageParameter = query.getPageParameter();
final int pageSize = pageParameter.getPageSize();
Query baseQuery = new Query();
if (StringUtils.isNoneBlank(query.getTransId())) {
  baseQuery.addCriteria(new Criteria("transId").is(query.getTransId()));
  baseQuery.addCriteria(new Criteria("retriedCount").lt(query.getRetry()));
final long totalCount = mongoTemplate.count(baseQuery, mongoTableName);
if (totalCount <= 0) {
  return voCommonPager;
int start = (currentPage - 1) * pageSize;
voCommonPager.setPage(PageHelper.buildPage(query.getPageParameter(), (int) totalCount));
baseQuery.skip(start).limit(pageSize);
final List<MongoAdapter> mongoAdapters =
    mongoTemplate.find(baseQuery, MongoAdapter.class, mongoTableName);
if (CollectionUtils.isNotEmpty(mongoAdapters)) {
  final List<HmilyCompensationVO> recoverVOS =

代码示例来源:origin: kaaproject/kaa

@Override
public MongoEndpointProfile findEndpointIdByKeyHash(byte[] endpointKeyHash) {
 LOG.debug("Get count of endpoint profiles by endpoint key hash [{}] ", endpointKeyHash);
 Query query = query(where(EP_ENDPOINT_KEY_HASH).is(endpointKeyHash));
 query.fields().include(ID);
 return findOne(query);
}

代码示例来源:origin: kaaproject/kaa

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

代码示例来源:origin: kaaproject/kaa

@Override
public void removeNotificationsByKeyHash(final byte[] keyHash) {
 LOG.debug("Remove unicast notifications by endpoint key hash [{}] ", keyHash);
 mongoTemplate.remove(query(where(EP_ENDPOINT_KEY_HASH).is(keyHash)), getCollectionName());
}

代码示例来源:origin: yu199195/hmily

@Override
public HmilyTransaction findById(final String id) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  MongoAdapter cache = template.findOne(query, MongoAdapter.class, collectionName);
  return buildByCache(Objects.requireNonNull(cache));
}

代码示例来源:origin: yu199195/hmily

@Override
public int updateStatus(final String id, final Integer status) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("status", status);
  final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("update data exception!");
  }
  return ROWS;
}

代码示例来源:origin: roncoo/spring-boot-demo

public void deleteById(int id) {
  Criteria criteria = Criteria.where("id").in(id);
  Query query = new Query(criteria);
  mongoTemplate.remove(query, RoncooUser.class);
}

相关文章