2

I was answered about constructing a MongoDb query

How to code collection find with regex and complex criteria using Java MongoDB driver?

Still the question about a count was unanswered: So for this query how to write an analogous Java code returning count:

db.cache.find({objectKey: { $regex: 'Bos*'}, cacheVersionString:'08/03/15_11:05:09'}).count()
4
  • Do you use Spring Data maybe? Commented Aug 11, 2015 at 17:36
  • Not for this collection Commented Aug 11, 2015 at 17:41
  • And what is generally your approach to use Java and Mongo? Commented Aug 11, 2015 at 17:42
  • either this Driver or SpringData Commented Aug 11, 2015 at 17:44

1 Answer 1

0

With MongoTemplate, it would be something like this:

Query query = new Query();
query.addCriteria(Criteria.where("objectKey").regex("Bos*"));
query.addCriteria(Criteria.where("cacheVersionString").is("08/03/15_11:05:09"));
long count = mongoTemplate.count(query, MyClass.class);
Sign up to request clarification or add additional context in comments.

3 Comments

The cache collection is not mapped to any class
Just map it. It's much easier then to process raw results returned by driver.
What if the mapping is not accurate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.