Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jnosql-dev] Weird MongoDBTemplate aggregate interface

Hey Dmitry, how are you?

I like the approach. Do you want to create a PR for us to review?



On Wed, Oct 11, 2023 at 3:17 PM Dmitry Repchevsky via jnosql-dev <jnosql-dev@xxxxxxxxxxx> wrote:

Hi all,

Current MongoDBTemplate.aggregate interface is not aligned to jakarta NoSQL:

https://github.com/eclipse/jnosql-databases/blob/e97f46e45e62634f6ec12efbe40ba743741ae725/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/mapping/MongoDBTemplate.java#L86

It returns a stream of Mongo Document (as a Map<String, BsonValue>) which may be useful in some scenarios, but unfortunately can not be adopted to the NoSQL model.
Converter helper classes like MongoDBUtils are not public, so there is no way to go for the model for aggregation method.

My problem is that I can not limit select query an the only way to do it is using aggregate like:

[
  { "$match": {} }
  { "$limit": 10 }
]

IMHO this must be something:

MongoDBDocumentManager:

    public Stream<DocumentEntity> aggregate(String collectionName, List<Bson> pipeline) {
        Objects.requireNonNull(pipeline, "filter is required");
        Objects.requireNonNull(collectionName, "collectionName is required");
        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        AggregateIterable<Document> aggregate = collection.aggregate(pipeline);
        return stream(aggregate.spliterator(), false).map(MongoDBUtils::of)
                .map(ds -> DocumentEntity.of(collectionName, ds));
    }

MongoDBTemplate:

    <T> Stream<T> aggregate(String collectionName, List<Bson> pipeline);
    <T> Stream<T> aggregate(Class<T> entity, List<Bson> pipeline);

DefaultMongoDBTemplate:

    @Override
    public <T> Stream<T> aggregate(String collectionName, List<Bson> pipeline) {
        Objects.requireNonNull(collectionName, "collectionName is required");
        Objects.requireNonNull(pipeline, "pipeline is required");
        return this.getManager().aggregate(collectionName, pipeline)
                .map(this.converter::toEntity);
    }

    @Override
    public <T> Stream<T> aggregate(Class<T> entity, List<Bson> pipeline) {
        Objects.requireNonNull(entity, "entity is required");
        Objects.requireNonNull(pipeline, "pipeline is required");
        EntityMetadata entityMetadata = this.entities.get(entity);
        return this.getManager().aggregate(entityMetadata.name(), pipeline)
                .map(this.converter::toEntity);
    }

Kind regards,

Dmitry

_______________________________________________
jnosql-dev mailing list
jnosql-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jnosql-dev


--

Thanks a lot,

Twitter | Linkedin | Youtube


Back to the top