Trying to refactor a lot of code from one of our services I ended up using a piece of code as :
Document documentObject;
String docMongoId = ((DBObject) documentObject).removeField("_id").toString();
which though not on compiling but during execution has resulted into
java.lang.ClassCastException: com.pack.model.Document cannot be cast to com.mongodb.DBObject
wherein Document
is a type defined by us. We now have a piece of code as follows :
MongoCollection<Document> dbCollection = mongoClient.getDatabase("dbName")
.getCollection("collectionName", Document.class);
Seeking help, already went through few links -
Get ID of last inserted document in a mongoDB w/ Java driver
MongoError: cannot change _id of a document
How to query documents using "_id" field in Java mongodb driver?
which mostly suggest using BasicDBObject
and we don't want to be using the older context anymore. Is there a way I can access the _id
of the Document now(with client update) in java?
Please do ask for any additional or missing information required here.