@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); }
private Long findVersionByKey(byte[] endpointKeyHash) { LOG.debug("Find endpoint profile version by key hash [{}] ", endpointKeyHash); Long version = null; Query query = query(where(EP_ENDPOINT_KEY_HASH).is(endpointKeyHash)); query.fields().include(OPT_LOCK); DBObject result = mongoTemplate.getDb() .getCollection(getCollectionName()) .findOne(query.getQueryObject()); if (result != null) { version = (Long) result.get(OPT_LOCK); } return version; }
Field fields = query.fields();
@Override protected Query createQuery(ConvertingParameterAccessor accessor) { MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery); Query query = creator.createQuery(); if (tree.isLimiting()) { query.limit(tree.getMaxResults()); } TextCriteria textCriteria = accessor.getFullText(); if (textCriteria != null) { query.addCriteria(textCriteria); } String fieldSpec = getQueryMethod().getFieldSpecification(); if (!StringUtils.hasText(fieldSpec)) { ReturnedType returnedType = processor.withDynamicProjection(accessor).getReturnedType(); if (returnedType.isProjecting()) { returnedType.getInputProperties().forEach(query.fields()::include); } return query; } try { BasicQuery result = new BasicQuery(query.getQueryObject(), Document.parse(fieldSpec)); result.setSortObject(query.getSortObject()); return result; } catch (JSONParseException o_O) { throw new IllegalStateException(String.format("Invalid query or field specification in %s!", getQueryMethod()), o_O); } }
.is(pageLink.getEndpointGroupId()))); query.skip(offs).limit(lim + 1); query.fields() .include(DaoConstants.PROFILE) .include(EP_SERVER_PROFILE_PROPERTY)
@Override public EndpointProfileBodyDto findBodyByKeyHash(byte[] endpointKeyHash) { LOG.debug("Find endpoint profile body by endpoint key hash [{}] ", endpointKeyHash); EndpointProfileBodyDto endpointProfileBodyDto = null; Query query = Query.query(where(EP_ENDPOINT_KEY_HASH).is(endpointKeyHash)); query.fields() .include(DaoConstants.PROFILE) .include(EP_SERVER_PROFILE_PROPERTY) .include(EP_APPLICATION_ID) .include(EP_PROFILE_VERSION) .include(EP_SERVER_PROFILE_VERSION_PROPERTY) .include(EP_USE_RAW_SCHEMA); EndpointProfileDto pf = mongoTemplate.findOne(query, getDocumentClass()).toDto(); if (pf != null) { endpointProfileBodyDto = new EndpointProfileBodyDto( endpointKeyHash, pf.getClientProfileBody(), pf.getServerProfileBody(), pf.getClientProfileVersion(), pf.getServerProfileVersion(), pf.getApplicationId()); } LOG.debug("[{}] Found client-side endpoint profile body {} with client-side endpoint " + "profile version {} and server-side endpoint profile body {} " + "with server-side endpoint profile version {} and application id {}", endpointKeyHash, pf.getClientProfileBody(), pf.getServerProfileBody(), pf.getClientProfileVersion(), pf.getServerProfileVersion(), pf.getApplicationId()); return endpointProfileBodyDto; }
/** * 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); }
/** * 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); }
/** * 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); }
private int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(SEQUENCE); return (Integer) this.template.findAndModify(query, new Update().inc(SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName).get(SEQUENCE); // NOSONAR - never returns null }
/** * Perform MongoDB {@code INC} operation for the document, which contains the {@link MessageDocument} * {@code sequence}, and return the new incremented value for the new {@link MessageDocument}. * The {@link #SEQUENCE_NAME} document is created on demand. * @return the next sequence value. */ protected int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(MessageDocumentFields.SEQUENCE); return (Integer) this.mongoTemplate.findAndModify(query, new Update().inc(MessageDocumentFields.SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName) .get(MessageDocumentFields.SEQUENCE); // NOSONAR - never returns null }
@Override protected Query createQuery(ConvertingParameterAccessor accessor) { MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery); Query query = creator.createQuery(); if (tree.isLimiting()) { query.limit(tree.getMaxResults()); } TextCriteria textCriteria = accessor.getFullText(); if (textCriteria != null) { query.addCriteria(textCriteria); } String fieldSpec = getQueryMethod().getFieldSpecification(); if (!StringUtils.hasText(fieldSpec)) { ReturnedType returnedType = processor.withDynamicProjection(accessor).getReturnedType(); if (returnedType.isProjecting()) { returnedType.getInputProperties().forEach(query.fields()::include); } return query; } try { BasicQuery result = new BasicQuery(query.getQueryObject(), Document.parse(fieldSpec)); result.setSortObject(query.getSortObject()); return result; } catch (JSONParseException o_O) { throw new IllegalStateException(String.format("Invalid query or field specification in %s!", getQueryMethod()), o_O); } }
Field fields = query.fields();
public List<ApplicationSystem> findAll(String... includeFields) { log.debug("Find all ApplicationSystems (include fields: {})", Arrays.toString(includeFields)); Query q = new Query(); q.fields().include("name"); // Mandatory field for (String includeField : includeFields) { q.fields().include(includeField); } List<ApplicationSystem> applicationSystems = mongoOperations.find(q, ApplicationSystem.class); log.debug("Found {} applicationSystems", applicationSystems.size()); return applicationSystems; }
@Override public Set<String> findIdsWithNameByLaunchesRef(String name, Set<String> launchRef) { Query query = query(where(LAUNCH_REFERENCE).in(launchRef)).addCriteria(where(NAME).is(name)); query.fields().include("_id"); return mongoTemplate.find(query, TestItem.class).stream().map(TestItem::getId).collect(toSet()); }
public static String findStringFieldById(MongoOperations mongo, String field, String id, String collection) { Criteria criteria = Criteria.where(FIELD_ID).is(id); Query query = Query.query(criteria); query.fields().include(field); DBObject json = mongo.findOne(query, DBObject.class, collection); return json == null ? null : json.get(field).toString(); }
private static Query createApiDocQuery(String clientId, String apiId) { Query query = new Query() .addCriteria(Criteria.where(ApiDocNames.CLIENT_ID).is(clientId)) .addCriteria(Criteria.where(ApiDocNames.API_ID).is(apiId)); query.fields().exclude(ApiDocNames.SWAGGER); query.fields().exclude(ApiDocNames.BASE_API_DRAFT); return query; } }
public List<ApplicationSystem> findAllPublished(String[] includeFields) { Query q = new Query(); q.addCriteria(new Criteria("state").is("JULKAISTU")); for (String includeField : includeFields) { q.fields().include(includeField); } return mongoOperations.find(q, ApplicationSystem.class); } }
@Override public Page<Launch> findModifiedBefore(String project, Date before,Pageable p) { Query query = Query.query(Criteria.where(Modifiable.LAST_MODIFIED).lte((before))) .addCriteria(Criteria.where(PROJECT_ID_REFERENCE).is(project)); query.with(p); query.fields().include(ID_REFERENCE); return pageByQuery(query, p, Launch.class); }
@Override public Stream<Launch> streamModifiedInRange(String project, Date from, Date to) { Query query = Query.query(Criteria.where(Modifiable.LAST_MODIFIED).gte(from).lte((to))) .addCriteria(Criteria.where(PROJECT_ID_REFERENCE).is(project)); query.fields().include(ID_REFERENCE); return Streams.stream(mongoTemplate.stream(query, Launch.class)); }