TaffyDB动态多条件AND查询失效 返回OR结果问题求助
问题根因
TaffyDB 的查询语法规则为:传入数组类型的查询参数时默认执行OR逻辑,即匹配满足数组中任意一个条件的记录;只有传入单个对象,对象内的多个属性才会执行默认的AND逻辑,匹配同时满足所有属性条件的记录。你当前将每个筛选条件作为独立对象存入数组传入查询,自然会得到OR逻辑的结果。
解决方案
将存储筛选条件的变量从数组改为普通对象,所有有效筛选条件作为对象的属性添加,最终传入该对象作为查询参数即可。
额外注意你填充国家下拉选项的代码里存在变量名错误:循环参数为cc,但你使用了未定义的paese变量调用toUpperCase(),需同步修正避免报错。
修正后的核心代码示例
筛选条件收集与查询代码
$('select[name=selectOPtions]').on('change', function(){ // 将数组改为空对象 const myFilter = {}; const chosenType = $('#typeSelect').children("option:selected").val(); const chosenCountry = $('#countrySelect').children("option:selected").val(); const chosenProvince = $('#provinceSelect').children("option:selected").val(); const chosenCity = $('#citiesSelect').children("option:selected").val(); if(chosenType!=null && chosenType !="" && chosenType!="all"){ // 直接给对象加属性,不要push myFilter.type = chosenType; } if(chosenCountry!=null && chosenCountry!="" && chosenCountry!="all"){ myFilter.country = chosenCountry; } if(chosenProvince!=null && chosenProvince!="" && chosenProvince!="all"){ myFilter.province = chosenProvince; } if(chosenCity!=null && chosenCity!="" && chosenCity!="all"){ myFilter.city = chosenCity; } // 传入对象查询,默认走AND逻辑 const list = Stores(myFilter).get(); // 后续渲染逻辑不变 const $storesShow = $('#storesSelect'); $storesShow.empty(); $.each(list, function(id, rec){ if(rec !=''){ $storesShow.append(`<option value="${rec.store_code}"> ${rec.type}-${rec.country}-${rec.city}</option>`); } }); });
国家下拉填充修正代码
const allCountries = Stores().distinct("country"); const $countries = $('#countrySelect'); $.each(allCountries, function(id, cc){ if(cc!=null && cc!=''){ // 修正变量名错误,把paese改为cc $countries.append(`<option value="${cc}"> ${cc.toUpperCase()}</option>`); } });
内容的提问来源于stack exchange,提问作者Shila Mosammami
相关产品推荐
相关产品推荐

