I'm connecting to MongoDB from a Spring Boot application using mongo-driver 3.2.2.
public List<Document> getNodes() {
return mongoDatabase.getCollection("nodes").find().into(new ArrayList<Document>());
}
...
@RequestMapping("/nodes")
public List<Document> nodes(HttpServletResponse response) {
return mongoRepository.getNodes();
}
Currently my API returns _id
as objects:
"_id":{"timestamp":1486646209,"machineIdentifier":14826340,"processIdentifier":16048,"counter":2373754,"time":1486646209000,"date":1486646209000,"timeSecond":1486646209}
but I need them as hex strings. Is there any way I can manipulate the serialization to achieve this? I'm not using entity classes.
Yes, sure. Use this snippet:
ObjectId objectId = new ObjectId(); // somehow got it
String stringValue = objectId.toHexString();
// And vice versa
ObjectId restoredObjectId = new ObjectId(stringValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With