How to select documents in couchdb that are within a time duration?

I don't know if Couch DB enables a simpler why, but definitely you can do as follows.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a couchdb database containing photos, with one document per photo. I would like to create a view that groups the photos based on time proximity, i.e. , group photos that are created within 30 mins of each other.

Couchdb link|improve this question edited Mar 27 at 7:38dgw2,1693613 asked Mar 27 at 4:12user12945861.

I don't know if Couch DB enables a simpler why, but definitely you can do as follows: Use Ektorp API, simply add field as "creation time" and value will be the time the attachment is created. HashMap metadata = new HashMap(); metadata. Add("creation time", System.

CurrentTimeMillis()); dbConnector. Add(metadata); Then you can create a javascript function to query from current time to the last 30 minutes. And then create a view for that: //---------------------------------------- // created the view: Map view= new HashMap(); view.

Put(_id, nameOfYourView); view. Put("language", "javascript"); // this is necessary for views view. Put(viewName, javaScriptFunction); // here the js function should be a map simply check a view in couchdb and copy the structure dbConnector.

Put(view); //---------------------------------------- // get the results according to the view ViewQuery query = new ViewQuery(). DesignDocId(pathToDesignDocument). ViewName(viewName); List results = new ArrayList(); ViewResult vResult = dbCon.

QueryView(query); //---------------------------------------- for (ViewResult. Row row : vResult.getRows()) { results. Add(row.getValue()); }.

I am not sure I follow the solution you propose. How will it group photos that are created within 30 minutes of each other? Does the jsFunction you mention provide a map/reduce method to accomplish this?

Is there a way to do this in CouchDB without using an external library? – user1294586 Mar 27 at 16:17.

Your js will be your map function, and in Ektorp you write and pass the js map function as string(to the code I posted before). You can get the current time as milis, subtract 30 mins, and acquire the time difference you needed(in java). Then you pass that mili time to the javascript.

Remember your javascript function is a string, so you simply append the difference you need to that string. Which you can write as follows: String javaScriptFunction = " function(doc) { if(doc. Created >" + timeDifferenceIneed + "){ emit(doc.

PhotoName, doc); } } " // of course ignored exiting characters so above, timeDifferenceIneed your current time - 30 mins. Then pass this string to above and get your query result.

I see. You are creating a temporary view every time you do a query. This will work but is going to be expensive.

See: wiki.apache.org/couchdb/Introduction_to_... – user1294586 Mar 27 at 21:51 Not a temporary view, it is a permanent view and is stored as a document. – anvarik Mar 27 at 22:10.

If your photo documents have a "timestamp" element that is collated properly in JavaScript, you can create a simple map that will emit the timestamps as keys and then calculate the window you want to query. A simple view map would look like this: "mapname": "function(doc) { emit(doc. Timestamp) }" Once you know the timestamp of the photo you are focused on, you would have to generate startkey and endkey parameters set to your desired window.

CouchDB will return rows with the _id values you need to build URLs to the photo docs. So for an image with a timestamp of 1332927024, your query would be http://couchdb/db/_design/designdocname/_view/mapname? Startkey=1332926124&endkey=1332927924.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions