Reputation: 125
I have a object class:
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(
collection = "users"
)
public class UsersData {
@Field("Name")
private String name;
@Field("Address")
private Address address;
}
Users are fetched using the find(Query query,<T> entityClass)
operation and mapped onto UserData.class
Is there any way to get the objectID of the document representing a User. (I am unable to edit UserData.java as it is a readonly file)
Upvotes: 3
Views: 534
Reputation: 1496
you can use type String
or ObjectId
public class UsersDataWithId extends UsersData {
@Id
private String id;
}
in alternative you can use Document
type from MongoDB driver
MongoCollection<Document> collection = database.getCollection("users");
Document myDoc = collection.find().first();
myDoc.get("_id")
Upvotes: 5