2
我有一個rails應用程序,其中有一些索引問題。我按名稱搜索地點。 首先,我認爲它與addresses.coords有問題,但我不知道它。mongodb/rails「異常:找不到特殊索引:2d爲:」
搜索控制器的相關部分:
@practices = Practice.published
@practices = @practices.where(:"addresses.country" => params[:country].upcase) if params[:country].present?
if params[:location].present? && latlng = get_coordinates
@practices = @practices.near_sphere(:"addresses.coords" => latlng).max_distance(:"addresses.coords" => get_distance)
end
# now find doctors based on resulting practices
@doctors = Doctor.published.in("organization_relations.practice_id" => @practices.distinct(:_id))
完整的崩潰日誌:
Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
@length=255
@request_id=646
@response_to=0
@op_code=2004
@flags=[]
@full_collection_name="um-contacts.$cmd"
@skip=0
@limit=-1
@selector={:distinct=>"practices", :key=>"_id", :query=>{"deleted_at"=>nil, "published_at"=>{"$lte"=>2012-11-05 15:17:14 UTC}, "addresses.country"=>"DE", "addresses.coords"=>{"$nearSphere"=>[13.4060912, 52.519171], "$maxDistance"=>0.01569612305760477}}}
@fields=nil>
failed with error 13038: "exception: can't find special index: 2d for: { deleted_at: null, published_at: { $lte: new Date(1352128634313) }, addresses.country: \"DE\", addresses.coords: { $nearSphere: [ 13.4060912, 52.519171 ], $maxDistance: 0.01569612305760477 } }"
See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.):
app/controllers/search_controller.rb:16:in `index'
那索引的結果,不知道如何從嵌入地址查詢他們通過has_many。
> db.practices.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "um-contacts.practices",
"name" : "_id_"
}
]
幫助將非常感激!
編輯:看起來像的arent創造了adresses.coords指標,
db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.users", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.doctors", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.collaborations", "name" : "_i
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.practices", "name" : "_id_" }
,但應在實踐類中創建:
class Practice
...
embeds_many :addresses, cascade_callbacks: true, as: :addressable
...
field :name, type: String
field :kind, type: String
field :slug, type: String
index({"addresses.coords" => '2d'}, { min: -180, max: 180, background: true })
index({name: 1})
index({slug: 1}, { unique: true })
...
任何人有一個想法,爲什麼它的失敗?
您是否嘗試過運行db.practices.ensureIndex({addresses.coords:「2d」})? – Alex
db.practices.ensureIndex({「addresses.coords」:「2d」})創建索引並且錯誤消失了,但不應該由上面的代碼完成? – user1800569