7

I have a schema, with a document structure that looks like this:

{ "_id" : ObjectId( "4f8dcb06ee21783d7400003c" ),
  "venue" : ObjectId( "4f8dcb06ee21783d7400003b" ),
  "name" : "Some event",
  "webite: "www.whatever.com",
  "attendees" : [
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503a" ), "firstName" : "Joe", "lastName" : "Blogs", "emailAddress" : "some@thing1.com" },
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503b" ), "firstName" : "John", "lastName" : "West", "emailAddress" : "some@thing2.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503c" ), "firstName" : "Simon", "lastName" : "Green", "emailAddress" : "some@thing3.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503d" ), "firstName" : "Harry", "lastName" : "Smith", "emailAddress" : "some@thing4.com" }
                ],
  "eventType" : "Party"
}

I have a CSV file, that I'd like to import into the attendees collection...

Is that possible using mongoimport? I read this: Using mongoimport to read CSV into nested structure? and it seems as though the answer could be no...

Since this is a one time operation, I wouldn't mind if I had to import it into an "attendees" collection, then run another command to insert into the attendees collection within my document? (there's only one event document at the moment....)

Community
  • 1
  • 1
Alex
  • 37,502
  • 51
  • 204
  • 332

2 Answers2

7

You are correct, and unfortunately the answer is no.

Mongoimport is a very simple program. (You can see for yourself here: https://github.com/mongodb/mongo/blob/master/src/mongo/tools/import.cpp) For users who would like to import data that is more complicated than a 2-dimensional table of strings and numbers, the official advice is to write a custom script that will read in your input file and create documents in exactly the format that you would like them. Hopefully the import.cpp file will give you some ideas on how to get started.

Marc
  • 5,488
  • 29
  • 18
  • If you can get the data that you would like to import in json format, then it might be easier to import. However, if you are going to write a script to translate a .csv document into json format, then you might as well have the script import the data directly into Mongo! – Marc Apr 25 '12 at 22:10
  • It's possible this feature may be added in the future. See the related bug report [dotted field names should produce nested objects in mongoimport](https://jira.mongodb.org/browse/SERVER-3691) – Mark Stosberg Jan 24 '14 at 21:49
7

This maybe very late, but will be useful for future users running mongoDB > 4.x

You can have sub-document imported using a csv file by using a dot operator on the name.

for eg. if you want a document with a sub document called extra containing taxes, state & created_at, then you can name your fields as extra.taxes.auto(), extra.state.auto(), and extra.created_at.date(2006-01-02).

I was stuck on this question and couldn't find help online so posting it here. Hope this helps someone.

Abhay Verma
  • 133
  • 1
  • 8