/** * Creates a new {@link SpringDataMongodbQuery}. * * @param operations must not be {@literal null}. * @param type must not be {@literal null}. */ public SpringDataMongodbQuery(final MongoOperations operations, final Class<? extends T> type) { this(operations, type, operations.getCollectionName(type)); }
/** * Triggers * <a href="https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/">findOneAndReplace<a/> * to replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link FindAndReplaceOptions} into account.<br /> * <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. * @return the converted object that was updated or {@literal null}, if not found. Depending on the value of * {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or * as it is after the update. * @since 2.1 */ @Nullable default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions options) { return findAndReplace(query, replacement, options, getCollectionName(ClassUtils.getUserClass(replacement))); }
/** * Triggers * <a href="https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/">findOneAndReplace<a/> * to replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link FindAndReplaceOptions} into account.<br /> * <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. * @param entityType the type used for mapping the {@link Query} to domain type fields and deriving the collection * from. Must not be {@literal null}. * @param resultType the parametrized type projection return type. Must not be {@literal null}, use the domain type of * {@code Object.class} instead. * @return the converted object that was updated or {@literal null}, if not found. Depending on the value of * {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or * as it is after the update. * @since 2.1 */ @Nullable default <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, Class<T> resultType) { return findAndReplace(query, replacement, options, entityType, getCollectionName(ClassUtils.getUserClass(entityType)), resultType); }
/** * Creates a new {@link SpringDataMongodbQuery}. * * @param operations must not be {@literal null}. * @param type must not be {@literal null}. */ public SpringDataMongodbQuery(final MongoOperations operations, final Class<? extends T> type) { this(operations, type, operations.getCollectionName(type)); }
/** * Triggers * <a href="https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/">findOneAndReplace<a/> * to replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link FindAndReplaceOptions} into account.<br /> * <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. * @return the converted object that was updated or {@literal null}, if not found. Depending on the value of * {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or * as it is after the update. * @since 2.1 */ @Nullable default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions options) { return findAndReplace(query, replacement, options, getCollectionName(ClassUtils.getUserClass(replacement))); }
/** * Triggers * <a href="https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/">findOneAndReplace<a/> * to replace a single document matching {@link Criteria} of given {@link Query} with the {@code replacement} document * taking {@link FindAndReplaceOptions} into account.<br /> * <strong>NOTE:</strong> The replacement entity must not hold an {@literal id}. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a record and also an optional * fields specification. Must not be {@literal null}. * @param replacement the replacement document. Must not be {@literal null}. * @param options the {@link FindAndModifyOptions} holding additional information. Must not be {@literal null}. * @param entityType the type used for mapping the {@link Query} to domain type fields and deriving the collection * from. Must not be {@literal null}. * @param resultType the parametrized type projection return type. Must not be {@literal null}, use the domain type of * {@code Object.class} instead. * @return the converted object that was updated or {@literal null}, if not found. Depending on the value of * {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or * as it is after the update. * @since 2.1 */ @Nullable default <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, Class<T> resultType) { return findAndReplace(query, replacement, options, entityType, getCollectionName(ClassUtils.getUserClass(entityType)), resultType); }
protected final String collectionName() { return mongoTemplate.getCollectionName(recordClass); }
/** * Count all the stored entities for the repository * * @return The count of entities */ @Override public Integer count() { return (int) mongoOperations.getCollection(mongoOperations.getCollectionName(entityClass)).count(); }
@Override @SuppressWarnings("unchecked") public List<SensorType> findSensorTypesByProvider(final String providerId) { if (StringUtils.hasText(providerId)) { final String sensorCollectionName = getMongoOps().getCollectionName(Sensor.class); final Query query = getDistinctSensorByProviderTypeQuery(providerId); // Get uniques provider's components types id list final List<String> providerSensorTypes = getMongoOps().getCollection(sensorCollectionName).distinct("type", query.getQueryObject()); return CollectionUtils.isEmpty(providerSensorTypes) ? Collections.<SensorType>emptyList() : getMongoOps().find(buildQueryForIdInCollection(providerSensorTypes), SensorType.class); } else { // If the provider is null, return all component types return findAll(); } }
@Override @SuppressWarnings("unchecked") public List<ComponentType> findComponentTypesByProvider(final String providerId) { if (StringUtils.hasText(providerId)) { final String componentCollectionName = getMongoOps().getCollectionName(Component.class); // Get unique provider's components types id list final Query query = getDistinctComponentByProviderTypeQuery(providerId); final List<String> providerComponentTypes = getMongoOps().getCollection(componentCollectionName).distinct("componentType", query.getQueryObject()); return CollectionUtils.isEmpty(providerComponentTypes) ? Collections.<ComponentType>emptyList() : getMongoOps().find(buildQueryForIdInCollection(providerComponentTypes), ComponentType.class); } else { // If the provider is null, return all component types return findAll(); } }
protected <V extends CatalogDocument> void doDelete(final Query query, final Class<V> resourceType) { if (SyncResource.class.isAssignableFrom(resourceType)) { final String collectionName = getMongoOps().getCollectionName(resourceType); final Query queryFiltered = query; queryFiltered.fields().include("sensorId").include("providerId").include("applicationId"); final List<DeletedResource> resources = getMongoOps().find(queryFiltered, DeletedResource.class, collectionName); final List<DeletedResource> newResources = new ArrayList<DeletedResource>(); for (final DeletedResource resource : resources) { // Before insert collection, existing documents should be removed from collection to evict // duplicate exceptions and truncate bulk insert // Version 2.6+ of MongoDB allows to control this requirement (ordered parameter) if (getMongoOps().findById(resource.getId(), DeletedResource.class) == null) { resource.set_class(resourceType.getName()); resource.setDeletedAt(new Date()); newResources.add(resource); } } getMongoOps().insert(newResources, DeletedResource.class); } getMongoOps().remove(query, resourceType); }
private void syncDeletedResourcesMetadata(final Class<? extends CatalogDocument> resourceType) { try { final String deletedResourcesCollection = mongoOps.getCollectionName(DeletedResource.class); final Criteria criteria = Criteria.where("_class").is(resourceType.getName()); final long total = mongoOps.count(new Query(criteria), DeletedResource.class);
@SuppressWarnings("unchecked") @Override public List<ComponentType> getActiveComponentTypes(final boolean onlyPublics) { final String componentCollectionName = getMongoOps().getCollectionName(Component.class); List<String> componentTypesIds = null; if (onlyPublics) { final Criteria criteria = Criteria.where("publicAccess").is(Boolean.TRUE); final Query query = new Query(criteria); componentTypesIds = getMongoOps().getCollection(componentCollectionName).distinct("componentType", query.getQueryObject()); } else { componentTypesIds = getMongoOps().getCollection(componentCollectionName).distinct("componentType"); } return CollectionUtils.isEmpty(componentTypesIds) ? Collections.<ComponentType>emptyList() : getMongoOps().find(buildQueryForIdInCollection(componentTypesIds), ComponentType.class); }
); return mongoOperations.aggregate(a, mongoOperations.getCollectionName(Project.class), UserRoleDetails.class).getUniqueMappedResult();