我在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"} }作为输出,但是我希望输出如上所述。

最佳答案

您没有在将它们与之前的LoginRequest关联的LoginResponse或OtherResponse文档中存储任何内容。因此,在当前架构下,您无法构造查询以返回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/

10-11 10:27