collection.find(<query>).first() not returning NULL, if the item is not present

302 views
Skip to first unread message

Himani Garg

unread,
Dec 26, 2018, 11:15:18 PM12/26/18
to mongodb-user

I am writing a Java application, which makes connection to Mongo DB, creates the database and collection (if it doesn't exist), and then searches for a value in the collection. If it returns NULL, then a new value is added in collection, else the same value is returned.

I tried "collection.find().first()". This should return null, in case the item doesn't exist. But in my case, it is throwing an exception (not sure though) which is- "Attempted to decrement the reference count below 0". The find() method is running as usual, this exception is thrown in first() method (during binding.release() during execute in Mongo.java).


Code:


MongoDatabase database = mongoClient.getDatabase(this.databaseName);
MongoCollection<Document> collection = database.getCollection(this.collectionName);
FindIterable<Document> documents = collection.find(spec);
Document firstDocument = (Document)documents.first();

Expected: null, Actual Result: Exception

Is there any possible reason for it?
I also tried the iterator method, but same thing is happening. During find it is working perfectly, but it is throwing an exception during .iterator().

Jeff Yemin

unread,
Dec 27, 2018, 9:03:50 AM12/27/18
to mongodb-user
This seems like a bug, but to diagnose further please provide the full stack trace of the exception.

Regards,
Jeff
Reply all
Reply to author
Forward
0 new messages