简体   繁体   中英

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

I am using mongodb in java for one of my projects. User is going to enter a time which he knows will be in the json file. 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.

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

You are not storing anything in your LoginResponse or OtherResponse documents that associates them with the LoginRequest that preceded them. Hence, with your current schema, you cannot construct a query to return the LoginRequest followed by all the other documents until the next 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. 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.

(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).

(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. Then it will be a simple single query to retrieve all that information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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