這與一年前的另一個StackOverflow問題有關。但是,有點不同。解決和修復MongoDB/Mongoid ID問題
這是在Ruby/Mongoid:2.2.6。
當我開始使用MongoDB時,它看起來像我們不正確地存儲了一個MongoDb文檔的ID,因爲對ID的簡單查找不起作用,但是當我們對其他屬性運行where查詢時,它返回文檔。
我已經嘗試過使用從where返回的對象並將「id」和「_id」設置爲存儲字符串的BSON :: ObjectId版本來「重置」id字段。這沒有奏效,因爲當時的記錄仍然無法通過此字段進行查詢。
在我剛完全擦除磁盤(丟失數月的生產數據)並重新開始之前,有其他建議嗎?
編輯:在不可檢索的循環中重新調用的文檔示例。
{"_id"=>"4f47267193546d160b0171a2", "attribute_tags"=>[{"tag"=>"website"}, {"tag"=>"twitter"}, {"tag"=>"website"}, {"tag"=>"twitter"}], "contact_info"=>{"facebook"=>[], "success"=>true, "created_at"=>2012-02-24 05:58:06 UTC, "tags"=>[], "twitter"=>[], "email"=>[], "phone"=>[], "linkedin"=>[], "google_plus"=>[], "youtube"=>[], "contact_form"=>false}, "created_at"=>2012-02-24 05:56:01 UTC, "data"=>{"twitter_followers_count"=>112, "twitter_is_translator"=>112, "twitter_protected"=>false, "twitter_url"=>"http://www.bettyunderground.com", "twitter_verified"=>false, "twitter_statuses_count"=>2040, "twitter_listed_count"=>14, "twitter_geo_enabled"=>true, "twitter_friends_count"=>124, "twitter_created_at"=>"Fri Jul 17 21:41:00 +0000 2009", "twitter_contributors_enabled"=>false, "enriched_at"=>2012-02-24 05:58:09 UTC}, "demographics"=>{}, "description"=>"The trials and tribulations of a polemicist", "directory_ids"=>[], "forums"=>[], "found_at_url"=>"http://www.bettyunderground.com", "geographics"=>{"language"=>"en", "location"=>"San Francisco, CA"}, "hashtags"=>{"tag"=>{"website"=>true, "twitter"=>true}, "reachable_via"=>{"twitter"=>true}}, "host_names"=>[], "ignore_project_ids"=>[], "keyword_scores"=>{"return policy"=>0.0}, "keywords"=>["return policy"], "last_contact_info_update"=>2012-02-24 05:58:09 UTC, "name"=>"Betty Underground", "new_profiles"=>[{"service"=>"twitter", "user_id"=>"BettyUndergrnd", "score"=>1.0}, {"service"=>"twitter", "username"=>"BettyUndergrnd", "score"=>1.0}], "presence_score"=>0, "profile_url"=>"http://a2.twimg.com/profile_images/1459407098/image_normal.jpg", "profiles_retrieved"=>true, "references"=>[], "share_counts"=>{}, "tags"=>["website", "twitter"], "twitter"=>"BettyUndergrnd", "updated_at"=>2012-03-17 10:08:09 UTC, "wordsmaster_ids"=>[], "reachable_via"=>[], "read_project_ids"=>[]}
它沒有ID字段的ObjectId。不知道它是如何以這種方式破壞的,但事實就是這樣。
我使用修改它的代碼是:
#if d is the document
old_id = d._id
d["_id"] = BSON::ObjectId(old_id)
d.save
我已經把從我的控制檯這樣做的依據。你可以看到我在做什麼。
任何想法將不勝感激。
https://gist.github.com/2087011
你的意思是如果你使用返回的文檔找到文檔的id,它不能被找到? – xdazz 2012-03-17 13:00:34
嗨,你能告訴我們一個示例文檔的樣子,你如何重置_id以及查詢是什麼樣的? – Ren 2012-03-18 02:24:57
xdazz:是的。如果文檔集合被稱爲「實體」,我可以這樣做:Entity.all.each {| x | Entity.find(x.id)},它會在找不到文檔時出錯。 Entity.find(x._id)錯誤也是如此。 – Williamf 2012-03-19 00:14:48