I am struggling to import Mongodb's ObjectId from a CSV file using mongoimport:
I tried every combination and escape method I could think of but can't import ObjectId correctly from CSV.
First I tried importing exactly what I get exported from MongoDB to CSV. I am using MongoDB 2.2.1.
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"})
Then I used mongoexport command in shell:
mongoexport -h localhost:3002 -d yourdb -c classes --csv -f student_id,name > export.txt
Resulting CSV looks like this:
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
Quering class collection now returns:
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.
I am wrong on something or Mongo can't import it's own exported CSV??
If you have CSV files (or TSV files - they're conceptually the same) to import, use the --type=csv or --type=tsv option to tell mongoimport what format to expect. Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.
Use the mongoimport utility to import data into a MongoDB database. MongoDB provides the mongoimport utility that can be used to import JSON, CSV, or TSV files into a MongoDB database. mongoimport is located in the bin directory (eg, /mongodb/bin or wherever you installed it).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With