2012-03-17 83 views
0

这与一年前的另一个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

+0

你的意思是如果你使用返回的文档找到文档的id,它不能被找到? – xdazz 2012-03-17 13:00:34

+0

嗨,你能告诉我们一个示例文档的样子,你如何重置_id以及查询是什么样的? – Ren 2012-03-18 02:24:57

+0

xdazz:是的。如果文档集合被称为“实体”,我可以这样做:Entity.all.each {| x | Entity.find(x.id)},它会在找不到文档时出错。 Entity.find(x._id)错误也是如此。 – Williamf 2012-03-19 00:14:48

回答

0

在Mongoid中,有一个rake任务来转换ObjectIds。

如果您使用此功能,您将拥有收藏的镜像。然后,只需重命名,你就会被设置。

但如果您有重复的object_ids,将会出错,因此您可能需要运行几次。

而且,它很慢。

2

_id场是不可改变的。您必须插入一个新的文档,其新值为_id,并删除旧文档。

+0

这看起来像是正确的答案。但是,有些关联会让人很难做。 – Williamf 2012-03-19 00:26:02

+0

@Williamf:好像你没有多少选择。 – 2012-03-19 10:30:22

+0

似乎有一个耙子任务来做到这一点。 objectids_convert。任何人使用它? – Williamf 2012-03-19 13:16:12