Hi @Raymond_Brack,
Step 1
Transform the CSV header line so it looks like this:
id.string(),name.string(),addressLine1.string(),addressLine2.string(),suburb.string(),state.string(),postcode.string(),manager.string(),managerPhone.string(),managerEmail.string(),active.boolean()
"24a74f5f-d418-4c82-9185-6e0b3fb6742c","MX HALLAM Logistics ","74-80 Melverton Drive","Unit 10","Hallam","Vic","3803","Barney Rubble","1300 116 339","Barney.Rubble@Med-Xsolutions.com.au",1
"50f69c47-8e51-4448-97d6-23b67edc5b38","MX NEWCASTLE Logistics","194 Cormorant Rd"," ","Kooragang","NSW","2304","Fred Flintstone","1300 116 339","Fred.Flintstone@Med-XSolutions.com.au",1
Step 2
mongoimport --drop -d test -c coll --type csv --headerline --columnsHaveTypes --file lol.csv
2021-02-08T18:27:19.100+0100 connected to: mongodb://localhost/
2021-02-08T18:27:19.100+0100 dropping: test.coll
2021-02-08T18:27:19.133+0100 2 document(s) imported successfully. 0 document(s) failed to import.
Result
test:PRIMARY> db.coll.find().pretty()
{
"_id" : ObjectId("6021747779ecf1295f3ab01c"),
"id" : "24a74f5f-d418-4c82-9185-6e0b3fb6742c",
"name" : "MX HALLAM Logistics ",
"addressLine1" : "74-80 Melverton Drive",
"addressLine2" : "Unit 10",
"suburb" : "Hallam",
"state" : "Vic",
"postcode" : "3803",
"manager" : "Barney Rubble",
"managerPhone" : "1300 116 339",
"managerEmail" : "Barney.Rubble@Med-Xsolutions.com.au",
"active" : true
}
{
"_id" : ObjectId("6021747779ecf1295f3ab01d"),
"id" : "50f69c47-8e51-4448-97d6-23b67edc5b38",
"name" : "MX NEWCASTLE Logistics",
"addressLine1" : "194 Cormorant Rd",
"addressLine2" : " ",
"suburb" : "Kooragang",
"state" : "NSW",
"postcode" : "2304",
"manager" : "Fred Flintstone",
"managerPhone" : "1300 116 339",
"managerEmail" : "Fred.Flintstone@Med-XSolutions.com.au",
"active" : true
}
Note
Because I can handle any type I want with this, I can deal with the boolean “active” for example.
Another option:
If you are looking for an over engineered CSV Bazooka, you can give this a shot! I did this for a hackathon and it’s capable to handle any CSV you throw at it. It can regroup column together (if you have a date split on multiple columns day, month, year for example) and you can basically execute python code for each column so the parsing can be a lot smarter.
Cheers,
Maxime.