【问题标题】:Mongoose Mongo 2dsphere geoWithinMongoose Mongo 2dsphere geoWithin
【发布时间】:2016-07-24 17:35:34
【问题描述】:

在阅读了许多与我非常接近的问题并阅读了 MongoDB 文档和 Mongoose 文档之后,我仍然无法回答我的问题。

在节点 4.4.0 上使用 express 4.13.4、mongoose 4.4.10、mongodb 2.1.14

我的 Mongoose 位置架构:

var schema = new Schema({
  type: {type: String},
  coordinates: []
},{_id:false});

var model = mongoose.model('LocationModel',schema);

module.exports = {
   model :  model,
   schema : schema
};

我的CatalogModel 架构(我写给 Mongo 的内容):

var locationSchema = require('./locationModel').schema;
var schema = new Schema({
    title : String, 
    format: {type: String, maxlength: 4},
    location: {type: locationSchema, required:true},
    otherStuff: String
});
schema.index({location: '2dsphere'}); // Ensures 2dsphere index for location

model = mongoose.model('CatalogModel',schema);

我创建了一个具体的示例并写入 MongoDB(这工作正常......因为我可以在 Mongo 中查询它)

var polyEntry = new CatalogModel({
    title:"I am just a Polygon",
    otherStuff: "More stuff here",
    location:{
        type:'Polygon',
        coordinates:[[[0,1],[0,2],[1,2],[0,1]]]
    }
});

在 Mongo 中,我向集合询问了索引:

db.catalogmodels.getIndexes()

这就是它所说的(不完全确定这是什么意思)

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.catalogmodels"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2dsphere"
        },
        "name" : "location_2dsphere",
        "ns" : "test.catalogmodels",
        "background" : true,
        "2dsphereIndexVersion" : 3
    }
]

我可以发送db.catalogmodels.find() 并取回我的文档。

{ 
    "_id" : ObjectId("12345678901234566778"), 
    "title" : "I am just a Polygon", 
    "location" : { 
        "type" : "Polygon", 
        "coordinates" : [ [ [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], [ 0, 1 ] ] ] 
    },        
    "__v" : 0 
}

我什至可以在 Mongo 中调用 $geoWithin:

db.catalogmodels.find(
    {
        location:{
            $geoWithin:{
                $geometry:{
                    type:"Polygon",
                    "coordinates":[[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]]
                }
            }
        }
    })

但这是真正的问题

Mongoose 一直告诉我 [错误:无法使用 $geoWithin]

var geoJson = { 
    "type" : "Polygon", 
    "coordinates" : [[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]] 
};
CatalogModel
.find()
.where('location').within(geoJson)
.exec(function(err,data){
    if ( err ) { console.log(err); }
    else {console.log("Data: " + data);}
    db.close()
});

我还将 .find().where().within() 调用替换为:

CatalogEntryModel.find({
    location:{
        $geoWithin:{
            $geometry:{
                type:"Polygon",
                "coordinates":[[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]]
            }
        }
    }
})
.exec(function(err,data){
    if ( err ) { console.log(err); }
    else {console.log("Data: " + data);}
    db.close();
});

Mongoose 不喜欢 $geoWithin 调用是否有原因?最新的 API 说这应该可以。

【问题讨论】:

    标签: mongodb mongoose geojson mongoose-schema 2dsphere


    【解决方案1】:

    我在 Mongoose 上写了这个问题:https://github.com/Automattic/mongoose/issues/4044#

    它已经关闭了。

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 2013-09-16
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多