docpadquery-engine

How to combine queries in docpad?


I need to get only last week posts. I don't find examples in Documentation and for tests I use Query-Engine demo with this code:

models = [
        title: 'WRONG: Future'
        date: new Date("2015-05-23")
    ,
        title: 'Correct 1'
        date: new Date("2015-05-22")
    ,
        title: 'Correct 2'
        date: new Date("2015-05-20")
    ,
        title: 'WRONG: Old'
        date: new Date("2015-05-15")
]

max_date = min_date = new Date("2015-05-22");
min_date.setDate(min_date.getDate() - 7);

result = queryEngine.createCollection(models)
    .findAll({
        $and: {
            date: {
                $lte: max_date
            },
            date: {
                $gt: min_date
            }
        }
    }).toJSON()

return result

How to get posts in this slice?


Solution

  • here is the right answer:

        models = [
            title: 'WRONG: Future'
            date: new Date("2015-05-23")
        ,
            title: 'Correct 1'
            date: new Date("2015-05-22")
        ,
            title: 'Correct 2'
            date: new Date("2015-05-20")
        ,
            title: 'WRONG: Old'
            date: new Date("2015-05-15")
    ]
    
    max_date = new Date("2015-05-22");
    min_date = new Date(max_date.getTime() - 7*24*60*60*1000);
    
    result = queryEngine.createCollection(models)
        .findAll({
            date: {$lte: max_date, $gt: min_date }
        }).toJSON()
    
    return result