I am using MongoDB 3.0. suppose there is a set of documents named photos
, its structure is
{"_id" : 1, photographer: "jack"}
with database.getCollection("photos")
, Mongodb will return a MongoCollection
object, on which I have the method count()
to get the number documents returned.
However, when I make queries with specific conditions. For example find documents with id smaller than 100 :
photosCollections.find(Document.parse("{_id : {$lt : 100}}"))
Above find
method will always return a cursor which doesn't provide a count()
function. So how can I know how many documents returned ? I know on command line, I can use
db.photos.find({_id : {$lt : 100}}).count()
Of course, I can go through the iterator and count the number of documents myself. However I find it really clumsy. I am wondering does MongoDB java driver provides such functionality to count the number of documents returned by the find()
method ? If not, what is the reason behind the decision ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…