简体   繁体   English

Dynogels:使用OR比较查询

[英]Dynogels: Query using OR comparison

I am trying to optimise the following scans into a single scan (or query ). 我正在尝试将以下scans优化为单个scan (或query )。 The only way I see is to use to use a OR comparator using DynamoDB . 我看到的唯一方法是使用DynamoDB使用OR比较器 I am using dynogels (fork of vogels ) in my application but sadly I am not aware of any OR query functionality in there. 我在我的应用程序中使用dynogelsvogels ),但遗憾的是我不知道任何OR查询功能。

 let arrivals = yield Reservation.scan() .where('room').equals(room) .where('arrival').between(from, to) .execAsync().then((reply) => reply.Items.map((item) => item.get())); let departures = yield Reservation.scan() .where('room').equals(room) .where('departure').between(from, to) .execAsync().then((reply) => reply.Items.map((item) => item.get())); let combined = arrivals.concat(departures); return Promise.resolve(combined); 

Proposed optimisation: 建议的优化:

 return Reservation.scan() .where('room').equals(room) .where('arrival').between(from, to) .or.where('departure').between(from, to) .execAsync().then((reply) => reply.Items.map((item) => item.get())); 

The scans get me the reservations that end ( departure ) or start ( arrival ) (or both) in a specified date range ( from , to ). 扫描得到我在指定日期范围( fromto )结束( departure )或开始( arrival )(或两者)的预订。

I think this can be achieved using filterexpression . 我认为这可以通过filterexpression来实现。

Sample code using filterexpression:- 使用filterexpression的示例代码: -

const scanItem = Reservation.scan().filterExpression('room = :idVal AND ((arrival BETWEEN :arrDateVal1 AND :arrDateVal2) OR (departure BETWEEN :depDateVal1 AND :depDateVal2)) ')
                .expressionAttributeValues({ ':idVal' : '2', ':arrDateVal1' : '21-APR-2017', ':arrDateVal2' : '23-APR-2017'
            ,':depDateVal1' : '21-APR-2017', ':depDateVal2' : '23-APR-2017'});                

scanItem.exec((err, result) => {if(!err) {console.log(JSON.stringify(result,undefined, 2))}});

Note:- 注意:-

Not sure whether you wanted to specifically use where rather than filterexpression (or) just wanted to achieve the result regardless of where or filterexpression . 不确定你是否想要专门使用where而不是filterexpression (或)只是想实现结果,无论在wherefilterexpression

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

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