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, sorry for the late reply.

It is always good to keep compatibility when there is an option; if you don't, let's break and document it.

I saw your PR: https://github.com/eclipse/jnosql-databases/pull/245

I reviewed it with Max.

I will create a new PR with the documentation about BK.


On Wed, Oct 11, 2023 at 6:04 PM Dmitry Repchevsky <redmitry@xxxxxxx> wrote:

Hi Otavio,

The question is should we keep the compatibility.

There are several choices:

1. replace current aggregate().
I do not like this, because aggregate pipeline is thought to return any complex results that are not necessary aligned to the model (?).
Breaking compatibility with previous versions - some people may already explore this in their code

2. use another method names.
Not good, as... well... aggregate is aggregate.

3. add another aggregate method.
    e.g. public Stream<DocumentEntity> aggregate(String collectionName, Bson[] pipeline) {
Have to use an array instead of List to do not clash parameters.

What do you think?

Best,

Dmitry

On 10/11/2023 2:57 PM, Otavio Santana wrote:

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



--

Thanks a lot,

Twitter | Linkedin | Youtube


Back to the top