0

これは、1 年前の別の StackOverflow の質問に関連しています。でも、ちょっと違う。

これは Ruby/Mongoid: 2.2.6 にあります。

MongoDB を使い始めたとき、MongoDb ドキュメントの ID を間違って保存したようです。これは、ID の単純な検索が機能していないにもかかわらず、他の属性に対して where クエリを実行するとドキュメントが返されるためです。

whereから返されたオブジェクトを使用してidフィールドを「リセット」し、「id」と「_id」を保存された文字列のBSON::ObjectIdバージョンに設定しようとしました。then レコードはまだこのフィールドでクエリできないため、これは機能しませんでした。

ディスクを完全に消去して (数か月分の運用データを失う)、最初からやり直す前に、他に提案はありますか?

編集:取得できないループで返されるドキュメントの例。

{"_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

4

2 に答える 2

2

_idフィールドは不変です。新しい値を持つ新しいドキュメントを挿入し_id、古いドキュメントを削除する必要があります。

于 2012-03-17T13:03:39.837 に答える
0

Mongoidには、ObjectIdを変換するためのrakeタスクがあります。

これを使用すると、コレクションのミラーが作成されます。次に、名前を変更するだけで設定されます。

ただし、object_idが重複しているとエラーが発生するため、数回実行する必要がある場合があります。

そして、それは遅いです。

于 2012-03-20T12:26:57.067 に答える