阿里云服务空间在uni-app使用JQL查询地理位置在field内使用JQL运算符arrayElemAt会报错

阿里云服务空间在uni-app使用JQL查询地理位置在field内使用JQL运算符arrayElemAt会报错

示例代码:

async demo() {  
    let { data } = await dbJQL  
        .collection('JLJ-merchant-list')  
        .where({  
            location: dbCmd.geoNear({  
                geometry: new dbJQL.Geo.Point(116.98737, 36.67789),  
                maxDistance: 200000  
            })  
        })  
        .field(`_id,arrayElemAt(area,1) as code`)  
        .get();  
    return data;  
}

操作步骤:

运行上面代码,就会报错,如果去掉where条件,在field内是可以使用arrayElemAt运算方法的。

预期结果:

查询数组内的第2个项

实际结果:

arrayElemAt查不出数组内的第二项

bug描述:

阿里云服务空间,使用JQL查询地理位置在field内使用JQL运算符arrayElemAt会报错。 Error: Command failed with error 2 (BadValue): ‘$geoNear, $near, and $nearSphere are not allowed in this context’ on server 172.28.211.214:3717. The full response is { “operationTime” : { “$timestamp” : { “t” : 1756135564, “i” : 9 } }, “ok” : 0.0, “errmsg” : “$geoNear, $near, and $nearSphere are not allowed in this context”, “code” : 2, “codeName” : “BadValue”, “$clusterTime” : { “clusterTime” : { “$timestamp” : { “t” : 1756135564, “i” : 9 } }, “signature” : { “hash” : { “$binary” : “eTVE6D0hjp/LPtZui3jq2U2Vxpg=”, “$type” : “00” }, “keyId” : { “$numberLong” : “7492094857553379333” } } } } 23:26:19.827 [本地运行] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)


更多关于阿里云服务空间在uni-app使用JQL查询地理位置在field内使用JQL运算符arrayElemAt会报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

jql where 中使用geoNear,会转换为聚合查询。而$match阶段中不支持使用$geoNear。你可以直接使用聚合查询的geoNear阶段实现。

更多关于阿里云服务空间在uni-app使用JQL查询地理位置在field内使用JQL运算符arrayElemAt会报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


明白了

这是因为在阿里云服务空间的JQL查询中,geoNear地理位置查询与arrayElemAt等聚合运算符存在兼容性限制。错误信息明确指出$geoNear不能在包含聚合操作的上下文中使用。

当同时使用where中的地理位置查询和field中的聚合运算符时,JQL会尝试将查询转换为MongoDB的聚合管道,但$geoNear必须作为聚合管道的第一个阶段,这与arrayElemAt的管道阶段产生了冲突。

解决方案:

  1. 先执行地理位置查询获取基础数据
  2. 在应用层对查询结果进行数组元素提取处理
async demo() {
    // 先执行地理位置查询
    let { data } = await dbJQL
        .collection('JLJ-merchant-list')
        .where({
            location: dbCmd.geoNear({
                geometry: new dbJQL.Geo.Point(116.98737, 36.67789),
                maxDistance: 200000
            })
        })
        .field('_id,area')
        .get();
    
    // 在应用层处理数组
    data = data.map(item => {
        return {
            _id: item._id,
            code: item.area && item.area.length > 1 ? item.area[1] : null
        };
    });
    
    return data;
}
回到顶部