create a view for getting the list of documents where end time is equal or greater than current time and type

StackOverflow https://stackoverflow.com/questions/23564143

  •  18-07-2023
  •  | 
  •  

문제

I have a requirement to create bit complex queries could you please help me to write.

In my data base I have list of docs, for example:

{
  "_id": "26",
  "_rev": "1-53ac67e9ec4b4ce8ffa9cd609e107aaf",
  "customer_name": "Praneeth",
  "type": "trip",
  "duration": "10 hours 27 mins",
  "end_time": "Jan 1, 2014 10:11:00 PM",
  "start_time": "Jan 11, 2014 8:46:00 AM",
} 

Now I want to create a view which can read current timestamp and type from the URL to get the docs whose end_time is less than or equal to current timestamp and type is "trip". Here type could be anything not only the trip, based on type being passed from the URL I should get the docs.

Suppose if I write in SQL, query it would be like this:

 SELECT * FROM table_name WHERE end_time>="current time passed from the URL as key" and type='type passed from the url as key'

My view is like below now how to call this view from the URL to get the desired output

    function(doc){
var startTime=new Date(doc.start_time);
var endTime=new Date(doc.end_time);
emit([doc.type,endTime.getTime()], doc);
    }      

is below one is correct??

     http://localhost:5984/trip/_design/current_trip/_view/current_trip?startkey=["trip",1388607960000]&end_key={}
도움이 되었습니까?

해결책

Solution is simply emitting in map function of your view [trip, end_time]. The CouchDB's rule for order of array keys is to sort it lexicographically. Example data set will be sorted like this:

{ end_time: 40, trip: "ABC", ... },
{ end_time: 10, trip: "BCA", ... },
{ end_time: 20, trip: "BCA", ... },
{ end_time: 30, trip: "CAB", ... },
...

If you want to have dates up to end_time with customer with most recent end_time first, you may consider query parameter descending=true as very useful (or emit [trip, -end_time] what is even more useful if you need to have different order of fields in key array).

다른 팁

This may work for you...assuming you just want to compare to 'now', you don't need to worry about passing any parameters on the url. Just access the view normally.

function(doc) {
    var now = new Date();
    var endTime = new Date(doc.end_time);        
    if(doc.type == \"trip\" &&  endTime > now)
        emit( ... );
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top