最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mongoose $ gte和$ lte无法正常工作

网站源码admin23浏览0评论

mongoose $ gte和$ lte无法正常工作

mongoose $ gte和$ lte无法正常工作

最近我一直在开发一个应用程序,我需要获取基于半径的特定位置内的文档。当我将纬度和经度发送到服务器时,我的代码正确地计算了最小和最大纬度和经度。我尝试将计算出的数字放在$ gte和$ lte的mongoose查询中,但是预期的结果不会显示出来!

我尝试过以下mongoose查询:

{ name: /^انار0$/i }

我收到了以下回复:

[
{
    "rating": {
        "average": 0,
        "count": 0
    },
    "location": {
        "latitude": 33.635,
        "longitude": 70.889
    },
    "status": "ONLINE",
    "_id": "5c62fa691fab8f24209f36e6",
    "name": "انار0",
    "_type": "CAFE"
}
]

但是当我发送位置并且查询变成以下内容时:

{ location:
    { latitude:
        { 
            '$gte': 33.626016646154675,
            '$lte': 33.64398335384532
         },
     longitude:
         { 
             '$gte': 70.88152061196703,
             '$lte': 70.89647938803296
          } 
      },
      name: /^انار0$/i
}

我得到一个空的jsonarray作为我的回答,其响应不应该与前一个查询不同!

这是我的代码:

let query = {};
if (location.lat && location.lng){
    /**
     * earth radius is approximately 6378 kilometers
     * so each degree is 111.3170 km based on the (2*Pi/360) * 6378
     * basically each degree is 111317 meters
     * if we want shops in a 1000 meter radius then 1000 meters in degree would be 1000 / 111317
     */
    let Pi = Math.PI;
    if (isNaN(radius)) {
        radius = 1000; // radius in meters
    }
    let radiusInDegrees = radius / 111317;

    let locationQuery = {};
    locationQuery.latitude = {};
    locationQuery.longitude = {};

    locationQuery.longitude.$gte = location.lng - (radiusInDegrees * Math.cos(location.lat * (Pi/180)));
    locationQuery.longitude.$lte = location.lng + (radiusInDegrees * Math.cos(location.lat * (Pi/180)));

    locationQuery.latitude.$gte = location.lat - radiusInDegrees;
    locationQuery.latitude.$lte = location.lat + radiusInDegrees;

    query.location = locationQuery;
}
if (s !== undefined){
    query.name = new RegExp(`^${s}$`, "i");
}
database.Shop.find(query)
    .sort(sort)
    .limit(per_page)
    .skip((page - 1) * per_page)
    .select("name logo status rating _type location")
    .populate("logo")
    .exec((err, shops) => {
        if (err){
            res.status(500);
            return res.json({
                status_code: 500,
                message: "internal server error"
            });
        }
        database.Shop.count(query, (err, count) => {
            res.header("document-count", count);
            let max_pages = Math.ceil(count / per_page);
            res.header("total-pages", max_pages);
            let fullUrl = req.protocol + '://' + req.get('host') + req.baseUrl;
            let originalQS = req.query;
            originalQS.page = page;
            originalQS.per_page = per_page;
            if (page < max_pages) {
                let qs = originalQS;
                qs.page = page + 1;
                res.header("next-page", fullUrl + "?" + queryString.stringify(qs));
            }
            if (page > 1 && max_pages > 0) {
                let previous_page = page - 1;
                if (page > max_pages) {
                    previous_page = max_pages;
                }
                let qs = originalQS;
                qs.page = previous_page;
                res.header("previous-page", fullUrl + "?" + queryString.stringify(qs));
            }
            res.header("page", page);
            res.json(shops);
        });
    });
回答如下:

你必须更新你的查询格式。对于嵌套对象,您需要使用点(.)表示法。所以使用像"location.longitude"而不是使用{ location:{ latitude:{....

所以你可以更新你的代码来构建下面的查询

let query = {};
if (location.lat && location.lng) {
    let Pi = Math.PI;
    if (isNaN(radius)) {
        radius = 1000; // radius in meters
    }
    let radiusInDegrees = radius / 111317;


    query["location.longitude"] = {
        $gte: location.lng - (radiusInDegrees * Math.cos(location.lat * (Pi / 180))),
        $lte: location.lng + (radiusInDegrees * Math.cos(location.lat * (Pi / 180)))
    };

    query["location.latitude"] = {
        $gte: location.lat - radiusInDegrees,
        $lte: location.lat + radiusInDegrees
    };
}
if (s !== undefined) {
    query.name = new RegExp(`^${s}$`, "i");
}

这将生成像下面的查询,应该工作

{
    'location.latitude': { '$gte': 33.626016646154675, '$lte': 33.64398335384532 },
    'location.longitude': { '$gte': 70.88152061196703, '$lte': 70.89647938803296 },
    name: /^انار0$/i
}
发布评论

评论列表(0)

  1. 暂无评论