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();