MongoDB,聚合查询多点地理索引

时间:2014-08-02 16:36:13

标签: mongodb mongoose aggregation-framework geospatial

我正在尝试查询其坐标中包含Multipoints的2dsphere地理索引。根据mongodb文档,它是2.6的一个新功能,我没有找到一个很好的例子来查询它。

我的代码在Type Point的坐标下工作正常,但在我将其更改为MultiPoint时却没有。

我正在使用mongoose和nodejs。

这是猫鼬模式:

AnnonceSchema = mongoose.Schema({
    [...]
    geo: {type: Object, index: '2dsphere',name:String }
});

我的搜索方法:

AnnonceSchema.statics.search = function(long, lat, maxDistanceInKm, done){
    var Annonce = this;
        Annonce.aggregate(
            [
                { $match: [...]
                { $match: { "geo": {$within: {$centerSphere: [
                    [long, lat],
                        maxDistanceInKm / 6371
                 ]}}}},
                { $sort: { score: { $meta: "textScore" } } },
                { $project: { title: 1, _id: 1, score: { $meta: "textScore" }}},
                { $match: { score: { $gt: 1.0 } } }
            ], done);
};

我的单元测试:

var notReturned = {[...], geo: 
    {type: 'MultiPoint', coordinates:[[2.65, 48.79]]}};

var returned = {[...], geo: {type: 'Point', coordinates:[2.65, 48.79]}};
Model.create(doesNotWork, function(){
    Model.create(doesWork, function(){
        Annonce.searchAnnonce(2.65, 48.79, 30, function (err, annonces) {
        assert.strictEqual(annonces.length, 2);
 });
});

我不知道我缺少什么,坐标是好的,2个对象是相同的,唯一不同的是它们的类型,我为MultiPoint放置了一个数组数组。

有没有人尝试过此功能?如何查询MultiPoints地理索引?

谢谢

0 个答案:

没有答案