我来是因为我需要实现地理计算,但是,它不起作用。我目前正在使用包 globalsign/mgo
从文档我们有这个:
db.<collection>.find( { <location field> :
{ $near :
{ $geometry :
{ type : "Point" ,
coordinates : [ <longitude> , <latitude> ] } ,
$maxDistance : <distance in meters>,
}
}
}
)
查询 2dsphere 索引:https://docs.mongodb.com/manual/tutorial/query-a-2dsphere-index/ 2dsphere 索引:https://docs.mongodb.com/manual/core/2dsphere/
所以我有以下内容:
import (
"github.com/globalsign/mgo/bson"
"shared/models"
"time"
)
type Address struct {
ID *bson.ObjectId `protobuf:"bytes,1,opt,name=id,proto3" json:"_id,omitempty" bson:"_id"`
IsArchived bool `protobuf:"varint,2,opt,name=IsArchived,proto3" json:"is_archived,omitempty" bson:"is_archived"`
CreatedAt time.Time `protobuf:"varint,3,opt,name=CreatedAt,proto3" json:"created_at,omitempty" bson:"created_at"`
UpdatedAt time.Time `protobuf:"varint,4,opt,name=UpdatedAt,proto3" json:"update_at,omitempty" bson:"updated_at"`
PlaceID *bson.ObjectId `protobuf:"bytes,5,opt,name=PlaceID,proto3" json:"place_id,omitempty" bson:"place_id"`
CountryCode string `protobuf:"bytes,6,opt,name=CountryCode,proto3" json:"country_code,omitempty" bson:"country_code,omitempty"`
AdministrativeArea string `protobuf:"bytes,7,opt,name=AdministrativeArea,proto3" json:"administrative_area,omitempty" bson:"administrative_area,omitempty"`
Locality string `protobuf:"bytes,8,opt,name=Locality,proto3" json:"locality,omitempty" bson:"locality,omitempty"`
PostalCode string `protobuf:"bytes,9,opt,name=PostalCode,proto3" json:"postal_code,omitempty" bson:"postal_code,omitempty"`
Thoroughfare string `protobuf:"bytes,10,opt,name=Thoroughfare,proto3" json:"thoroughfare,omitempty" bson:"thoroughfare,omitempty"`
Premise string `protobuf:"bytes,11,opt,name=Premise,proto3" json:"premise,omitempty" bson:"premise,omitempty"`
Location GeoJson `protobuf:"bytes,12,opt,name=Location,proto3" json:"location,omitempty" bson:"location,omitempty"`
}
type GeoJson struct {
Type string `json:"-"`
Coordinates []float64 `json:"coordinates"`
}
var addresses []*models.Address
collection := _session.DB(constants.DBName).C(documentName)
err := collection.Find(session, addressDocument, bson.M{
"location": bson.M{
"$near": bson.M{
"$geometry": bson.M{
"type": "Point",
"coordinates": []float64{ -115.172813, 36.101379 },
},
"$maxDistance": 5000000,
},
},
}) &addresses)
由此,我得到了以下错误:
"error processing query: ns=DBNAME.addressDocumentTree: GEONEAR field=location maxdist=5e+06 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query"
我在程序初始化时确保我的索引
// EnsureIndex
func EnsureAddressIndex(session *mgo.Session) error {
_session := session.Copy()
defer _session.Close()
c := _session.DB(constants.DBName).C(addressDocument)
// Might be needed one day
pIndex := mgo.Index{
Key: []string{"location:2dsphere"},
Bits: 26,
}
err := c.EnsureIndex(pIndex)
if err != nil {
return err
}
return nil
}
有什么想法吗?我有超过 20k 个地址,它们都有经纬度。
我尝试从 globalsign/mgo 切换到 gopkg.in/mgo.v2 但没有成功
最佳答案
终于找到问题了..实际上,我没有以正确的方式创建索引..
// EnsureIndex force ids to be unique
func EnsureAddressIndex(session *mgo.Session) error {
_session := session.Copy()
defer _session.Close()
c := _session.DB(constants.DBName).C(addressDocument)
// Might be needed one day
pIndex := mgo.Index{
Key: []string{"$2dsphere:location"},
Bits: 26,
}
err := c.EnsureIndex(pIndex)
if err != nil {
return err
}
return nil
}
在索引声明中,我有以下行:
pIndex := mgo.Index{
Key: []string{"location:2dsphere"},
Bits: 26,
}
但是正确的写法是
pIndex := mgo.Index{
Key: []string{"$2dsphere:location"},
Bits: 26,
}
希望对您有所帮助!
关于mongodb - 无法通过 2sphere 找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727125/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m