gpt4 book ai didi

java - 使用mongodb和java从json文件中解析特定量的数据

转载 作者:行者123 更新时间:2023-12-02 07:45:13 25 4
gpt4 key购买 nike

我在我的一个项目中使用 java 中的 mongodb。用户将输入一个他知道将出现在 json 文件中的时间。我想要做的是搜索包含该时间的文档,从该文档到下一个 LoginRequest 文档,所有文档都将作为输出生成。

    For example:
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"}, "LoginRequest" : { "Time" : "11-06-2012 11:59:33", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cc"}, "LoginResponse" : { "innerAttr1" : "innerValue1", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cb"}, "OtherRequest" : { "innerAttr3" : "innerValue3"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cd"}, "OtherResponse" : { "innerAttr2" : "innerValue2", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ce"}, "LoginRequest" : { "Time" : "11-06-2012 12:34:05", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cf"}, "LoginResponse" : { "innerAttr1" : "innerValue1", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cg"}, "OtherRequest" : { "innerAttr3" : "innerValue3"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ci"}, "LoginRequest" : { "Time" : "11-06-2012 14:59:33", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cm"}, "LoginResponse" : { "innerAttr1" : "innerValue1", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cj"}, "OtherRequest" : { "innerAttr3" : "innerValue3"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cs"}, "OtherResponse" : { "innerAttr2" : "innerValue2", "innerAttr4" : "innerValue4"} }

这里假设用户输入时间为“11-06-2012 12:34:05”。所以输出应该是:

Output:
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ce"}, "LoginRequest" : { "Time" : "11-06-2012 12:34:05", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cf"}, "LoginResponse" : { "innerAttr1" : "innerValue1", "innerAttr4" : "innerValue4"} }
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113cg"}, "OtherRequest" : { "innerAttr3" : "innerValue3"} }

我能够得到 { "_id": { "$oid": "4ceb753a70fdf877ef5113ce"}, "LoginRequest": { "Time": "11-06-2012 12:34:05", "innerAttr4": "innerValue4"} } 作为输出,但我希望输出如上所述。

最佳答案

您没有在 LoginResponse 或 OtherResponse 文档中存储任何将它们与之前的 LoginRequest 相关联的内容。因此,使用当前架构,您无法构造一个查询来返回 LoginRequest,后跟所有其他文档,直到下一个 LoginRequest。

如果不了解应用程序的用途和架构的详细信息,就很难为您提供明确的解决方案。不过,这里有一些建议:

(a) 在所有文档中存储时间戳,而不仅仅是在 LoginRequest 中。因此,给定一个 LoginRequest,您可以找到下一个 LoginRequest(执行按时间排序的查询),然后搜索时间戳位于两个 LoginRequest 的时间戳之间的所有其他文档。

(b) 如果您的应用程序架构允许,请将 LoginRequest 的 ID 存储在其后的 LoginResponse 和 OtherRequest 文档中(直到下一个 LoginRequest)。

(c) 不要为 LoginRequest、LoginResponse 和 OtherRequest 存储单独的文档,而是在集合中存储特定登录的所有交互的单个文档。然后,这将是一个简单的单个查询来检索所有这些信息。

关于java - 使用mongodb和java从json文件中解析特定量的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10976522/

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