多个靠近猫鼬

Multiple near with mongoose

我有以下 "query":

MySchema
    .where('address1.loc').near({
        center: {
            type: 'Point',
            coordinates: user.address1.loc
        },
        maxDistance: 10 * 1000
    }).where('address2.loc').near({
        center: {
            type: 'Point',
            coordinates: user.address2.loc
        },
        maxDistance: 10 * 1000
    })
    .exec(function(err, objects) {
        console.log(err);
        console.log(objects);

        if(err) return eachCallback(err);

        return eachCallback();
    });

我的架构有两个地址(一个取货地址和一个交接地址)。所以我必须使用两个"nears"。但这似乎是不可能的:

{ [MongoError: Can't canonicalize query: BadValue Too many geoNear expressions] name: 'MongoError' }

我有什么选择?

更新: 我和 MongoDB 的一些人谈过。尽管我的用例是有效的,但这似乎不是开箱即用的。所以我正在寻找 "workaround"。如果您需要有关用例的详细信息,请告诉我。

这是我在 "addresses" 中定义位置的方式:

...
loc: {type: [Number], index: '2dsphere'},
...

我遇到了同样的问题和错误信息。但我不记得我是如何克服的。我希望我们能解决你的问题! 如果您的架构定义如下,请尝试以下解决方案;

loc : {
        'type': { type: String, default: 'Point' },
        coordinates: [Number]
    } 

您可以在 collection 上查看 address.loc.coordinates 商店的编号吗?

替代方式

尝试使用$geoWithin运算符

我想你可以像下面这样改变你的提问条款。您应该使用 $geoWithin$centerSphere 而不是 $near$maxDistance。您已尝试查找在 10.000 米附近具有 loc 字段的记录。 $centerSphere表示radius,它使用旧坐标值(仅[经度,纬度])并且可以与geoJSON字段和2dsphere索引一起使用。并且您的 $centerSphere 定义必须是 10/6371 10 公里。 (tutorial).

如果您将第二个运算符与 $near 运算符一起使用,则会在 MongoDB side 上出现问题。

You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that uses a different type of special index. For example you cannot combine $near with the $text query.

var r = 10/6371;
var area1 = { center: address1.loc.coordinates, radius: r, unique: true }
var area2 = { center: address2.loc.coordinates, radius: r, unique: true }

MySchema
    .where('address1.loc').
    .within()
    .circle(area1)
    .where('address2.loc').
    .within()
    .circle(area2)
    .exec(function(err, objects) {
        console.log(err);
        console.log(objects);

        if(err) return eachCallback(err);

        return eachCallback();
    });

希望这对您有所帮助..