将MongoDB ObjectId序列化为字符串 [英] Serialize MongoDB ObjectId to string
本文介绍了将MongoDB ObjectId序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用mongo-driver 3.2.2从Spring Boot应用程序连接到MongoDB.
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();
}
当前,我的API返回_id
作为对象:
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);
这篇关于将MongoDB ObjectId序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文