Rails/Mongoid:mongoimport之后,父对象无法识别has_many/belongs_to关系的子对象 [英] Rails/Mongoid: Parent object not recognising child of has_many/belongs_to relation after mongoimport

查看:149
本文介绍了Rails/Mongoid:mongoimport之后,父对象无法识别has_many/belongs_to关系的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mongoimport工具通过CSV将包含以下行的CSV导入mongodb:

A CSV containing the following rows is imported into mongodb via CSV using the mongoimport tool:

object_1_id,field1,field2
52db7f026956b996a0000001,apples,oranges
52db7f026956b996a0000001,pears,plums

这些字段将导入到集合Object2中.

The fields are imported into the collection Object2.

导入后,通过控制台确认行已存在.

After import the rows are confirmed to exist via the console.

#<Object2 _id: 52e0713417bcabcb4d09ad12, _type: nil, field1: "apples", field2: "oranges", object_1_id: "52db7f026956b996a0000001">
#<Object2 _id: 52e0713517bcabcb4d09ad76, _type: nil, field1: "pears", field2: "plums", object_1_id: "52db7f026956b996a0000001">

Object2可以通过object_1_id访问Object1:

> o = Object2.first
#<Object2 _id: 52e0713417bcabcb4d09ad12, _type: nil, field1: "apples", field2: "oranges", object_1_id: "52db7f026956b996a0000001">
> o1 = o.object_1
#<Object1 _id: "52db7f026956b996a0000001", other_fields: "text and stuff">

但是Object1无法看到使用mongoimport导入的任何Object2行.它可以查看通过控制台或其他方式创建的所有行:

But Object1 cannot see any of the Object2 rows that were imported with mongoimport. It can see all rows that have been created via the console or other means:

> o1.object_2s.count
10
> o1.object_2s.find("52e0713417bcabcb4d09ad12")
Mongoid::Errors::DocumentNotFound:
    Document not found for class Object2 with id(s) 52e0713417bcabcb4d09ad12.

TL; DR Object1似乎无法识别通过mongoimport导入的子模型,尽管子模型正确存储了父ID并能够识别其父模型.

TL;DR Object1 doesn't appear to recognise child models imported via mongoimport, despite the child correctly storing the parent ID and being able to identify its parent.

推荐答案

每亩的注释太短,因此将id作为字符串而不是BSON ObjectIds导入.

As per mu is too short's comment the ids were being imported as Strings instead of BSON ObjectIds.

mongoexportmongoimport(我仅使用后者)仅支持字符串和数字(​​请参阅: https://stackoverflow.com/a/15763908/943833 ).

mongoexport and mongoimport (I was only using the latter) only support strings and numbers (See: https://stackoverflow.com/a/15763908/943833).

要从CSV类型导入数据,您必须使用扩展JSON转储,如上面的链接所述.

In order to import data with type from a CSV you have to use Extended JSON dumps as explained in the above link.

快速而肮脏的解决方案:

1)使用mongoexport将要导入的集合导出为JSON:

1) Export the collection you want to import as JSON using mongoexport:

mongoexport -d database -c collection -o output.json

2)抓住导出文件的第一行.它应该看起来像这样:

2) Grab the first line of the export file. It should look something like this:

{ "_id" : { "$oid" : "52dfe0106956b9ee6e0016d8" }, "column2" : "oranges", "column1" : "apples", "object_1_id" : { "$oid" : "52dfe0106956b9ee6e0016d8" }, "updated_at" : { "$date" : 1390403600994 }, "created_at" : { "$date" : 1390403600994 } }

3)_id字段以及您不想导入的任何其他字段.

3) The _id field as well as any other fields you don't want to import.

4)使用您选择的语言以JSON代码段作为每行的模板来生成JSON文件.

4) Use your language of choice to generate a JSON file using the JSON snippet as a template for each line.

5)使用mongoimport导入新的JSON文件:

5) Import the new JSON file using mongoimport:

mongoimport -d database -c collection --type json --file modified.json

与CSV相比,这将保留更好的类型.我不确定它是否与使用mongodumpmongorestore一样可靠,但是由于我的CSV文件来自其他地方,因此对我来说它们不是一个选择.

This will preserve types better than CSV is capable. I'm not sure whether it is as reliable as using mongodump and mongorestore but they aren't an option for me since my CSV file comes from elsewhere.

这篇关于Rails/Mongoid:mongoimport之后,父对象无法识别has_many/belongs_to关系的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆