uni-app 客户端使用_.geoNear时调用.count()方法返回值异常

uni-app 客户端使用_.geoNear时调用.count()方法返回值异常

产品分类:
uniCloud/腾讯云

示例代码:

if(total !== 0 && !total){  
    total=(await db.collection('xn-user-action').where(where).count()).result.total;  
    console.log(total);  
};  
if(total-page*limit<0){  
    skip=0;  
    limit=total-(page-1)*limit;  
} else {  
    skip=total-page*limit;  
    page++;  
};  
console.log('total,skip,limt,page',total,skip,limit,page);  
db.collection('xn-user-action').where(where).get().then(res=>{  
    console.log(res)  
    resolve({total, page, list:res.result.data});  
}).catch(err=>{  
    reject(err);  
})

操作步骤:

.count()返回的total是0, .get()返回的data是包含实际数据的数组。 也就是说同样的语法.count() 和.get()方法的到结果不一样

预期结果:

.count() 返回的total是1

实际结果:

返回0

bug描述


更多关于uni-app 客户端使用_.geoNear时调用.count()方法返回值异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

我看一下你的where怎么写的

更多关于uni-app 客户端使用_.geoNear时调用.count()方法返回值异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app客户端使用_.geoNear进行地理位置查询时,调用.count()方法返回异常(返回0),而.get()却能正常获取数据,这通常是由于地理位置索引的特殊性导致的。

问题分析:

  1. 地理位置查询的特殊性_.geoNear是专门用于地理位置查询的操作符,它依赖于地理空间索引。在uniCloud中,这种查询可能不会在.count()方法中被准确统计。
  2. 索引匹配问题.count()方法可能无法正确识别地理位置查询条件,导致统计时忽略了_.geoNear的过滤结果,从而返回0。
  3. 查询执行时机差异.get().count()在数据库中的执行方式可能不同,.get()会完整执行地理位置计算,而.count()可能仅基于普通索引进行快速统计。

解决方案:

  • 使用.get()后手动统计:既然.get()能返回正确数据,可以先执行.get()获取结果数组,然后通过res.result.data.length手动计算总数。虽然这会增加一次数据查询开销,但能保证准确性。
    const res = await db.collection('xn-user-action').where(where).get();
    const total = res.result.data.length;
回到顶部