Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to determine mongodb collection size in my java spring application. I know that reactive rective Mongo Template has a count() method what does that, however it needs a query param.

So my solution is:

public Mono<Long> collectionSize(){
    Criteria criteria = Criteria.where("_id").exists(true);
    return this.reactiveMongoTemplate.count(Query.query(criteria),MY_COLLECTION_NAME);
}

However I dont like this solution, because I have to use a captain obvious criteria.

Is there any better solution for this problem?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

Criteria has an empty constructor.

public Mono<Long> collectionSize(){
    Criteria criteria = new Criteria();
    return this.reactiveMongoTemplate.count(Query.query(criteria),MY_COLLECTION_NAME);
}

Reference

And all of the variants of count requires a query param as documented here

Query doesn't need criteria, you can just supply the query param. Reference

public Mono<Long> collectionSize(){
        return this.reactiveMongoTemplate.count(new Query(),MY_COLLECTION_NAME);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...