Reputation: 557
For me i want to update the existing record in mongodb using spring mongotemplate.For that i have to search the existing record from the mongodb.While saving record in mongodb it automatically creates field called "_id"
in each and every record. I tried a lot to access this field.But i can't able to do.. :(
So that i manually created one field called myid
in my DTO object. Each and every time saving i'm serializing the id in file and saving.Searching using this myid
.I know hard code file path will create problem while deploying in server.
Is there any way to do this or any way to access system generated _id
for searching and updating .. ??
Thanks.
Upvotes: 0
Views: 459
Reputation: 4088
_id
is a primary key. If you don't specify a field in your object with annotation @id
then Mongo
will create one for you. You cannot access it because Mongo
is not able to populate that field as it does not exists or there is a mapping missing.
You will have to have something like in your code [It would be nice if you could post you code too.]
import org.springframework.data.annotation.Id;
@Id
protected String id;
HTH
Upvotes: 1