gpt4 book ai didi

java - Mongodb java 驱动程序将结果作为字符串而不是 bson 对象获取

转载 作者:行者123 更新时间:2023-12-02 04:04:48 28 4
gpt4 key购买 nike

有没有办法执行只返回字符串而不转换为 bson 对象的查询?

我想使用jsoniter进行反序列化,但不知道是否可以使用java驱动程序来完成。

最佳答案

您可以获取 bson 对象并对其调用 toJson() 来获取包含 json 格式的查询结果的 String。这应该允许您使用 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() 方法的更多信息。 .

关于java - Mongodb java 驱动程序将结果作为字符串而不是 bson 对象获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717927/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com