本文介绍了一个以上的2dsphere索引,不确定要运行哪个geoNear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中的集合内使用$geoNearnear.我已将MongoDB数据库托管到mlabs .而且在我的本地计算机上一切正常,但是不知道为什么当我部署我的应用程序时,我会遇到以下错误:

下面是我使用的代码:

Shops.aggregate([
  {
     $geoNear: {
         near: { 
            type: "Point",
            coordinates: coordinates
         },
         distanceField: "dist.calculated",
         maxDistance: 80467,
         spherical: true
     }
  }
])
.then((products)=>{
      res.json(products);
})

有人可以帮我吗?

解决方案

如错误消息所示,这是因为您有多个2dsphere索引,所以$geoNear不知道要使用哪个索引. /p>

在这种情况下,您可以:

文档中也提到了该错误:

您可以使用 db.collection.getIndexes()列出在集合上定义的所有索引.

下面是使用key参数的示例:

> db.test.insert([
  {_id:0, loc1:{type:'Point',coordinates:[1,1]}, loc2:{type:'Point',coordinates:[2,2]}},
  {_id:1, loc1:{type:'Point',coordinates:[2,2]}, loc2:{type:'Point',coordinates:[1,1]}}
])

然后创建两个2dsphere索引:

> db.test.createIndex({loc1:'2dsphere'})
> db.test.createIndex({loc2:'2dsphere'})

在未指定key的情况下运行$geoNear将输出错误:

> db.test.aggregate({$geoNear:{near:{type:'Point',coordinates:[0,0]},distanceField:'d'}})
...
  "errmsg": "more than one 2dsphere index, not sure which to run geoNear on",
...

使用key: loc1将根据loc1索引对结果进行排序(_id: 0_id: 1之前):

> db.test.aggregate(
    {$geoNear: {
        near: {type: 'Point',coordinates: [0,0]},
        distanceField: 'd',
        key: 'loc1'}})
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 157424.6238723255 }
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 314825.2636028646 }

然后,使用key: loc2将根据loc2索引对结果进行排序(_id: 1_id: 0之前):

> db.test.aggregate(
    {$geoNear: {
        near: {type: 'Point',coordinates: [0,0]},
        distanceField: 'd',
        key: 'loc2'}})
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 157424.6238723255 }
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 314825.2636028646 }

I am using $geoNear and near inside the aggregate in MongoDB. I have hosted my MongoDB database into mlabs. And everything working fine on my local, But don't know why when I deploy my app to live and I am getting the below error:

Below is the code which I have used:

Shops.aggregate([
  {
     $geoNear: {
         near: { 
            type: "Point",
            coordinates: coordinates
         },
         distanceField: "dist.calculated",
         maxDistance: 80467,
         spherical: true
     }
  }
])
.then((products)=>{
      res.json(products);
})

Can anyone please help me with the same?

解决方案

As the error message indicated, this is because you have more than one 2dsphere indexes, so $geoNear doesn't know which one to use.

In this situation, you can either:

The error is mentioned in the docs as well:

You can use db.collection.getIndexes() to list all indexes defined on the collection.

Here's an example of using the key parameter:

> db.test.insert([
  {_id:0, loc1:{type:'Point',coordinates:[1,1]}, loc2:{type:'Point',coordinates:[2,2]}},
  {_id:1, loc1:{type:'Point',coordinates:[2,2]}, loc2:{type:'Point',coordinates:[1,1]}}
])

Then I create two 2dsphere indexes:

> db.test.createIndex({loc1:'2dsphere'})
> db.test.createIndex({loc2:'2dsphere'})

Running $geoNear without specifying key will output the error:

> db.test.aggregate({$geoNear:{near:{type:'Point',coordinates:[0,0]},distanceField:'d'}})
...
  "errmsg": "more than one 2dsphere index, not sure which to run geoNear on",
...

Using key: loc1 will sort the result according to the loc1 index (_id: 0 comes before _id: 1):

> db.test.aggregate(
    {$geoNear: {
        near: {type: 'Point',coordinates: [0,0]},
        distanceField: 'd',
        key: 'loc1'}})
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 157424.6238723255 }
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 314825.2636028646 }

And, using key: loc2 will sort the result according to the loc2 index (_id: 1 comes before _id: 0):

> db.test.aggregate(
    {$geoNear: {
        near: {type: 'Point',coordinates: [0,0]},
        distanceField: 'd',
        key: 'loc2'}})
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 157424.6238723255 }
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 314825.2636028646 }

这篇关于一个以上的2dsphere索引,不确定要运行哪个geoNear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:35