我正在使用 mongo-driver 3.2.2 从 Spring Boot 应用程序连接到 MongoDB。
public List<Document> getNodes() {
return mongoDatabase.getCollection("nodes").find().into(new ArrayList<Document>());
}
...
@RequestMapping("/nodes")
public List<Document> nodes(HttpServletResponse response) {
return mongoRepository.getNodes();
}
目前我的 API 返回 _id
作为对象:
"_id":{"timestamp":1486646209,"machineIdentifier":14826340,"processIdentifier":16048,"counter":2373754,"time":1486646209000,"date":1486646209000,"timeSecond":1486646209}
但我需要它们作为十六进制字符串。有什么办法可以操纵序列化来实现这一点?我没有使用实体类。
最佳答案
是的,当然。使用此代码段:
ObjectId objectId = new ObjectId(); // somehow got it
String stringValue = objectId.toHexString();
// And vice versa
ObjectId restoredObjectId = new ObjectId(stringValue);
关于java - 将 MongoDB ObjectId 序列化为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42162395/