gpt4 book ai didi

rethinkdb - 动态链过滤条件

转载 作者:行者123 更新时间:2023-12-02 01:27:38 25 4
gpt4 key购买 nike

如何在 RethinkDB 中链接多个条件?这就是我现在得到的,如果我只将 live or sports 作为参数传递,它会起作用。一旦我传递了 livesports 参数,sports 显然总是覆盖 filter 变量和 live 参数被忽略。

app.get('/bets', function (req, res) {
var live = req.query.live;
var sports = req.query.sports;

var filter = {};

if (live === undefined) {
filter = r.or(r.row('live').eq(0), r.row('live').eq(1));
} else {
filter.live = parseInt(live);
}

if (sports !== undefined) {
var sports = sports.split(',');
filter = function (doc) {
return r.expr(sports).contains(doc("sport"));
}
}

r.table('bets').filter(filter).limit(100).run(connection, function(err, cursor) {
// ...
});
});

最佳答案

您可以使用 RethinkDB 链接过滤器。

类似的东西(警告,未经测试):

app.get('/bets', function (req, res) {
var live = req.query.live;
var sports = req.query.sports;

var liveFilter, sportFilter;

if (live === undefined) {
liveFilter = r.or(r.row('live').eq(0), r.row('live').eq(1));
} else {
liveFilter = function (doc) { return true; };
}

if (sports !== undefined) {
sports = sports.split(','); // no need to redefine the variable here
sportFilter = function (doc) {
return r.expr(sports).contains(doc("sport"));
}
} else {
sportFilter = function (doc) { return true; };
}

r.table('bets')
.filter(liveFilter) // apply the first filter
.filter(sportsFilter) // apply the second filter
.limit(100)
.run(connection, function(err, cursor) {
// ...
});
});

或者,您可以创建一个过滤器函数来处理实时和运动过滤器(同样未经测试,这是为了让您入门):

app.get('/bets', function (req, res) {
var live = req.query.live;
var sports = req.query.sports.split(',');

var filter = function(doc){
var sportPass, livePass;

if (live === undefined) {
livePass = r.or(r.row('live').eq(0), r.row('live').eq(1))(doc);
} else {
livePass = parseInt(live); // not sure what you meant by filter.live here
}

if (sports !== undefined) {
sportPass = r.expr(sports).contains(doc("sport"));
}

return sportPass && livePass;
};


r.table('bets').filter(filter).limit(100).run(connection, function(err, cursor) {
// ...
});
});

关于rethinkdb - 动态链过滤条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com