I am trying to import a Mongodb ObjectId from a CSV file using mongoimport:
I tried every combination and escape method that I could think of, but can't import the ObjectId from CSV correctly.
At first I tried to import exactly what I get from MongoDB to CSV. I am using MongoDB 2.2.1.
I just created two collections and pointed out one _id document 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"})
Then I used the mongoexport command in the shell:
mongoexport -h localhost:3002 -d yourdb -c classes --csv -f student_id,name > export.txt
The CSV result is as follows:
student_id,name
ObjectID(5143af326d44e1ceb372121d),"II-4"
Then I imported the resulting CSV using:
mongoimport -h localhost:3002 -d yourdb -c class --type csv --file export.txt --headerline
Now the collection of Query classes is returned:
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 see, the student_id field in the second document is actually a string, not a MongoDB ObjectId.
-, Mongo CSV