繁体   English   中英

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

[英]parse a particular amount of data from json file using mongodb and java

我在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(执行按时间排序的查询),然后搜索所有其他文档,并在两个LoginRequests的时间戳之间添加时间戳。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM