简体   繁体   English

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

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

I am using mongodb in java for one of my projects. 我在java中使用mongodb作为我的一个项目。 User is going to enter a time which he knows will be in the json file. 用户将输入他知道将在json文件中的时间。 What I want to do is search for the document which contains that time and from that document till the next LoginRequest document all documents are to be produced as an output. 我想要做的是搜索包含该时间的文档,并从该文档到下一个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"} }

Here suppose user enters time as "11-06-2012 12:34:05". 这里假设用户输入时间为“11-06-2012 12:34:05”。 So the output for this should be: 所以这个输出应该是:

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"} }

I am able to get { "_id" : { "$oid" : "4ceb753a70fdf877ef5113ce"}, "LoginRequest" : { "Time" : "11-06-2012 12:34:05", "innerAttr4" : "innerValue4"} } as an output but I want the output to be as mentioned above. 我能够得到{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ce"}, "LoginRequest" : { "Time" : "11-06-2012 12:34:05", "innerAttr4" : "innerValue4"} }作为输出,但我想输出为如上所述。

You are not storing anything in your LoginResponse or OtherResponse documents that associates them with the LoginRequest that preceded them. 您没有在LoginResponse或OtherResponse文档中存储任何内容,将它们与之前的LoginRequest相关联。 Hence, with your current schema, you cannot construct a query to return the LoginRequest followed by all the other documents until the next LoginRequest. 因此,使用当前架构,您无法构造查询以返回LoginRequest,后跟所有其他文档,直到下一个LoginRequest。

Without knowing the details of your application's purpose and architecture, it is hard to give you a definitive solution. 如果不了解应用程序的用途和体系结构的详细信息,很难为您提供明确的解决方案。 Here, however, are a few suggestions: 但是,这里有一些建议:

(a) Store a timestamp in all documents rather than just in the LoginRequest. (a)在所有文档中存储时间戳,而不是仅存储在LoginRequest中。 Thus, given a LoginRequest, you could find the next LoginRequest (do a query ordered by time) and then search for all other documents with a timestamp between the timestamps of the two LoginRequests. 因此,给定LoginRequest,您可以找到下一个LoginRequest(执行按时间排序的查询),然后搜索所有其他文档,并在两个LoginRequests的时间戳之间添加时间戳。

(b) If your application architecture allows it, store the id of the LoginRequest in the LoginResponse and OtherRequest documents that follow it (until the next LoginRequest). (b)如果您的应用程序体系结构允许,请将LoginRequest的id存储在LoginResponse和其后的OtherRequest文档中(直到下一个LoginRequest)。

(c) Don't store separate documents for LoginRequest, LoginResponse and OtherRequest, but instead store a single document in the collection for all the interactions for a particular login. (c)不要为LoginRequest,LoginResponse和OtherRequest存储单独的文档,而是在集合中存储单个文档以用于特定登录的所有交互。 Then it will be a simple single query to retrieve all that information. 然后,它将是一个简单的单一查询来检索所有信息。

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

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