在阅读了许多离我很近的问题,阅读了MongoDB文档和猫鼬文档之后,我仍然无法回答我的问题。
在节点4.4.0上使用快递4.13.4、猫鼬4.4.10、mongodb 2.1.14
我的猫鼬定位方案:
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 (这很好.因为我可以在蒙古语中查询)
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
}我甚至可以在蒙戈打个$geoWithin电话:
db.catalogmodels.find(
{
location:{
$geoWithin:{
$geometry:{
type:"Polygon",
"coordinates":[[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]]
}
}
}
}),但以下是的实际问题:
猫鼬一直告诉我错误:不能使用$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();
});猫鼬不喜欢$geoWithin电话有什么原因吗?最新的API说,这应该有效。
发布于 2016-04-11 15:42:32
我在“猫鼬:https://github.com/Automattic/mongoose/issues/4044#”上写了这篇文章
它已经关闭了。
https://stackoverflow.com/questions/36415162
复制相似问题