Mongodb java驱动程序将结果作为字符串而不是bson对象获取 2 月 Questions & Answers 332 有没有一种方法可以执行只返回字符串而不转换为bson对象的查询 我想使用jsoniter进行反序列化,但不知道是否可以使用java驱动程序实现
# 1 楼答案 您可以获取bson对象并对其调用toJson(),以获取包含json格式的查询结果的字符串。这应该允许您使用Jsoniter或Gson来反序列化查询结果 例如: Document result = collection.find(eq("birthYear", 1990)).first(); // Made up query String resultJsonString = result.toJson(); 您可以在the MongoDB documentation for the Java DB Driver中阅读有关toJson()方法的更多信息
# 1 楼答案
您可以获取bson对象并对其调用
toJson()
,以获取包含json格式的查询结果的字符串。这应该允许您使用Jsoniter或Gson来反序列化查询结果例如:
您可以在the MongoDB documentation for the Java DB Driver中阅读有关
toJson()
方法的更多信息