Search code examples
mongodbsmalltalkpharo

Pharo, Voyage and MongoDB


I want to build a relatively simple web app using Pharo, Voyage and MongoDB + TeaPot. Before I start the project I did a lot of research and one question remains: How do I initially upload a bunch of data into the MongoDB? I basically have the data in CSV format. Do I have to program an importer in Smalltalk that does that? If I were to do it without smalltalk it would be missing all the object IDs etc. How do you go about things like that?

Thanks, Henrik


Solution

  • If you have data in CSV format, then I would recommend creating a simple importer. You could use NeoCSV and then save it via Pharo. I presume you know how to setup Mongo repository (@workspace) do:

    | repository |
    repository := VOMongoRepository 
                  host: VOMongoRepository defaultHost 
                  database: 'MyMongoDb'.
    VORepository setRepository: repository.
    

    First create your two class methods for Voyage:

    Kid class >> isVoyageRoot
        ^ true "instances of this object will be root"
    
    Kid class >> voyageCollectionName
        ^ 'Kids' "The collection name in MongoDB"
    

    The Kid class should have firstName(:), surname(:), age(:) accesors and instance variables of the same name.

    Then simply have a reading from CSV and then saving it into mongoDB:

    | personalInformation readData columnName columnData aKid |
    "init variable"
    personalInformation := OrderedDictionary new.
    "emulate CSV reading"
    readData := (NeoCSVReader on: 'firstName, surname, age\John, Smith, 5' withCRs readStream) upToEnd.
    columnName := readData first.
    columnData := readData second.
    "Repeat for as many number of columns you may have"
    1 to: columnName size do: [ :index |
        personalInformation at: (columnName at: index) put: (columnData at: index)
    ].
    aKid := Kid new.   
    "Storing Kid object information"
    personalInformation keysAndValuesDo: [ :key :value |
        aKid perform: (key asString,$:) asSymbol with: value "For every column store the information into a Kid object (you have to have accessors for that)"
    ].
    aKid save "Saving into mongoDB"
    

    This is only to give you rough idea

    To query in your MongoDB do:

    db.Kids.find()

    You should see the stored information.

    Disclaimer: Even thou the code should be fine, I did not have time to actually test it on mongoDB.