Я иду, потому что мне нужно реализовать геокалькулы, однако, это не работает.В настоящее время я использую пакет globalsign/mgo
Из документа у нас есть это:
db.<collection>.find( { <location field> :
{ $near :
{ $geometry :
{ type : "Point" ,
coordinates : [ <longitude> , <latitude> ] } ,
$maxDistance : <distance in meters>,
}
}
}
)
Запрос 2dsphere index: 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)
Из этого я получил следующую ошибку:
"запрос обработки ошибки: ns = DBNAME.addressDocumentTree: поле GEONEAR = местоположение maxdist = 5e + 06 isNearSphere = 0 \ nSort: {} \ nProj: {} \ n возвращен планировщик: ошибка: не удается найти индекс для запроса $ geoNear "
Я обеспечиваю свой индекс при инициализации моей программы
// 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
}
Есть идеи?У меня более 20 тыс. Адресов, и все они имеют широту / долготу.
Изменить 1
Я попытался переключиться с globalsign/mgo
на gopkg.in/mgo.v2
безуспешно