如何在mongoose中使用地理位置创建查询(用于近距离搜索)

问题描述 投票:0回答:1

我是mongodb和mongoose的新手。我不知道我的查询是否正常工作但是当我向代码添加一些geojson时它返回null。 我唯一的目标是可以使用州,国家和州过滤我的数据,也可以搜索附近的地方。如果有人可以帮助我,那将是非常有帮助的。谢谢

var query = {
  $and : [
  {city : new RegExp('^'+req.body.city+'$', "i") },
  {state : req.body.state},
  {country : req.body.country},
    {
      loc : {
      $nearSphere : {
        $geometry : {
          type : "Point",
          coordinates : [-117.16108380000003,32.715738]
        },
        $maxDistance : 100
      }
    }
  }
Business.find(query).populate('deal_id').sort({business_type : -1,deal_id : -1})
.exec(function(err,businesses){
  res.json(businesses)
  return
})

我不知道我是否做得对,这是我的样本数据:

[
{
    "_id": "5a0b1f489929442c36fd5c83",
    "business_row": 29160,
    "created_at": "2017-11-14T16:52:10.130Z",
    "owner_name": "David Lui",
    "company_website": "",
    "phone_number": "604-273-3288",
    "contact_name": "David Lui",
    "zip_postal": "V6X 3Z9",
    "state": "British Columbia",
    "country": "Canada",
    "city": "Richmond",
    "address": "3779 Sexsmith Rd # 2172 Richmond British Columbia",
    "company_name": "Aem Seafood",
    "__v": 1,
    "slug": "Aem-Seafood&Richmond",
    "loc": {
        "coordinates": [
            "-123.129488",
            "49.185359"
        ],
        "type": "Point"
    },
    "deal_id": [],
    "is_favorite": false,
    "is_draft": false,
    "has_featured": false,
    "owner_id": [
        "5a0adcf9f7205f0004535def"
    ],
    "files": [],
    "operations": [],
    "sub_category": [],
    "category_options": [
        {
            "value": "5a0b186b9f3a4a2710075654",
            "sub_cat": {
                "value": "59f6d13d00086a6e645c50a4",
                "label": "Meat And Fish Markets"
            }
        }
    ],
    "category_id": [
        "5a0b186b9f3a4a2710075654"
    ],
    "business_type_name": "Free",
    "business_type": "0",
    "user_id": [
        "5a0adcf9f7205f0004535def"
    ]
}

]

node.js mongodb mongoose
1个回答
0
投票

结果我不需要查询它的城市,州和国家,并使用$ geoWithIn

© www.soinside.com 2019 - 2024. All rights reserved.