如何使用mongoimport从CSV文件导入Mongodb ObjectId? [英] How to import Mongodb ObjectId from CSV file using mongoimport?

查看:499
本文介绍了如何使用mongoimport从CSV文件导入Mongodb ObjectId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用mongoimport从CSV文件导入Mongodb的ObjectId:

I am struggling to import Mongodb's ObjectId from a CSV file using mongoimport:

我尝试了所有可以想到的组合和转义方法,但无法从CSV正确导入ObjectId.

I tried every combination and escape method I could think of but can't import ObjectId correctly from CSV.

首先,我尝试完全导入从MongoDB导出到CSV的内容. 我正在使用MongoDB 2.2.1.

First I tried importing exactly what I get exported from MongoDB to CSV. I am using MongoDB 2.2.1.

我刚刚创建了两个集合,并在另一个文档中引用了一个文档的_id:

I just created two collections and referenced one document's _id in another document:

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"})

然后我在shell中使用了mongoexport命令:

Then I used mongoexport command in shell:

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

生成的CSV如下所示:

Resulting CSV looks like this:

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

然后我使用以下命令导入生成的CSV文件:

Then I imported the resulting CSV using:

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

查询类集合现在返回:

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

您会注意到第二个文档中的student_id字段实际上是一个字符串,而不是MongoDB ObjectId.

As you can notice student_id field in the second document is actually a string and not MongoDB ObjectId.

我在某件事上错了,或者Mongo无法导入它自己导出的CSV?

I am wrong on something or Mongo can't import it's own exported CSV??

推荐答案

该问题可以在MongoDB 2.4.1中重现.

The problem can be reproduced in MongoDB 2.4.1.

文档( http://docs.mongodb.org/manual/reference/mongoimport/)状态(我强调):

The documentation (http://docs.mongodb.org/manual/reference/mongoimport/) states (emphasis by me):

注意:请勿将mongoimport和mongoexport用于完整实例, 生产备份,因为它们将无法可靠地捕获数据类型 信息.按照备份"中的说明使用mongodump和mongorestore 这类功能的MongoDB系统策略.

Note Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in "Backup Strategies for MongoDB Systems" for this kind of functionality.

在此讨论中 https://groups.google. com/forum/?fromgroups =#!topic/mongodb-user/RcnumM5QyxM 回答了一个类似的问题,

In this discussion https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/RcnumM5QyxM a similar question was answered as this:

使用tsv或csv的Mongoimport可以仅导入字符串和数字,并且 并非[1]中指定的任何其他类型.如果您确实要导入 这些类型,以及是否可以为您生成JSON而不是TSV 导入文件,这将是一个好方法;否则,你可以写 后处理步骤,将字符串转换为适当的字符串 MongoDB类型(基于对某类型的值的某种知识 给定的字段应该是).

Mongoimport with tsv or csv can only import strings and numbers, and not any of the other types specified in [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; 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).

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

这篇关于如何使用mongoimport从CSV文件导入Mongodb ObjectId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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