简体   繁体   English

如何使用mongoimport从CSV文件导入Mongodb ObjectId?

[英]How to import Mongodb ObjectId from CSV file using mongoimport?

I am struggling to import Mongodb's ObjectId from a CSV file using mongoimport: 我正在努力使用mongoimport从CSV文件导入Mongodb的ObjectId:

I tried every combination and escape method I could think of but can't import ObjectId correctly from CSV. 我尝试了我能想到的每种组合和转义方法,但无法从CSV正确导入ObjectId。

First I tried importing exactly what I get exported from MongoDB to CSV. 首先,我尝试将从MongoDB导出的确切内容导入CSV。 I am using MongoDB 2.2.1. 我使用的是MongoDB 2.2.1。

I just created two collections and referenced one document's _id in another document: 我刚创建了两个集合,并在另一个文档中引用了一个文档的_id:

use yourdb
db.createCollection("student")
db.createCollection("class")
db.student.insert({"name":"Peter"})
db.student.find() returns { "_id" : ObjectId("5143af326d44e1ceb372121d"), "name" : "Peter" }
db.class.insert({"student_id": ObjectId("5143af326d44e1ceb372121d"),"name":"II-4"})

Then I used mongoexport command in shell: 然后我在shell中使用了mongoexport命令:

mongoexport -h localhost:3002 -d yourdb -c classes --csv -f student_id,name > export.txt

Resulting CSV looks like this: 生成的CSV如下所示:

student_id,name
ObjectID(5143af326d44e1ceb372121d),"II-4"

Then I imported the resulting CSV using: 然后我使用以下方法导入生成的CSV:

mongoimport -h localhost:3002 -d yourdb -c class --type csv --file export.txt --headerline

Quering class collection now returns: Quering类集合现在返回:

db.class.find()
{ "_id" : ObjectId("5143afc66d44e1ceb372121e"), "student_id" :   ObjectId("5143af326d44e1ceb372121d"), "name" : "II-4" }
{ "_id" : ObjectId("5143b44788df173ba096550e"), "student_id" : "ObjectID(5143af326d44e1ceb372121d)", "name" : "II-4" }

As you can notice student_id field in the second document is actually a string and not MongoDB ObjectId. 您可以注意到第二个文档中的student_id字段实际上是一个字符串而不是MongoDB ObjectId。

I am wrong on something or Mongo can't import it's own exported CSV?? 我错了什么或者Mongo无法导入它自己导出的CSV?

For anyone with this issue who's trying to insert ObjectIds from JSON - it very much IS possible with a bit of modification to the existing data. 对于有这个问题的人试图从JSON插入ObjectIds - 对现有数据进行一些修改是非常可能的。

Replace: 更换:

{ "_id" : ObjectId("5143afc66d44e1ceb372121e"),
  "student_id" : ObjectId("5143af326d44e1ceb372121d"),
  "name" : "II-4" }

With: 附:

{ "_id" : {"$oid":"5143afc66d44e1ceb372121e"},
  "student_id" : {"$oid":"5143af326d44e1ceb372121d"},
  "name" : "II-4" }

Just use a regular expression to replace the ObjectId wrap. 只需使用正则表达式替换ObjectId换行。

The problem can be reproduced in MongoDB 2.4.1. 问题可以在MongoDB 2.4.1中重现。

The documentation ( http://docs.mongodb.org/manual/reference/mongoimport/ ) states (emphasis by me): 文件( http://docs.mongodb.org/manual/reference/mongoimport/ )陈述(由我强调):

Note Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information . 注意请勿将mongoimport和mongoexport用于完整实例生产备份,因为它们无法可靠地捕获数据类型信息 Use mongodump and mongorestore as described in “Backup Strategies for MongoDB Systems” for this kind of functionality. 使用mongodump和mongorestore,如“MongoDB系统的备份策略”中所述,以实现此类功能。

In this discussion https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/RcnumM5QyxM a similar question was answered as this: 在此讨论中https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/RcnumM5QyxM回答了类似的问题:

Mongoimport with tsv or csv can only import strings and numbers , and not any of the other types specified in [1]. 使用tsv或csv的Mongoimport 只能导入字符串和数字 ,而不能导入 [1]中指定的任何其他类型。 If you do want to import those types, and if you can produce JSON instead of TSV for your import file, that would be a good way to go; 如果您确实要导入这些类型,并且如果您可以为导入文件生成JSON而不是TSV,那么这将是一个很好的方法; otherwise, you can write a post-processing step which converts the strings to the appropriate MongoDB types (based on some knowledge of what the type of value for a given field should be). 否则,您可以编写一个后处理步骤,将字符串转换为适当的MongoDB类型(基于对给定字段的值类型应该是什么的一些知识)。

  • Dan

[1] http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON [1] http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM