我想使用Pharo,Voyage和Mongodb Teapot构建一个相对简单的Web应用程序。在开始项目之前,我进行了很多研究,还有一个问题仍然存在:我最初如何将一堆数据上传到MongoDB中?我基本上具有CSV格式的数据。我是否必须在Smalltalk中对进口商进行编程?如果我在没有Smalltalk的情况下做到这一点,它将缺少所有对象ID等。您如何处理这样的事情?
谢谢henrik
如果您具有CSV
格式的数据,那么我建议创建一个简单的进口商。您可以使用NeoCSV
,然后通过Pharo保存。我想您知道如何设置Mongo存储库(@WorkSpace)做:
| repository |
repository := VOMongoRepository
host: VOMongoRepository defaultHost
database: 'MyMongoDb'.
VORepository setRepository: repository.
首先为航行创建两个类方法:
Kid class >> isVoyageRoot
^ true "instances of this object will be root"
Kid class >> voyageCollectionName
^ 'Kids' "The collection name in MongoDB"
孩子类应具有firstName(:)
,surname(:)
,age(:)
ACCES和同名的实例变量。
然后只需从CSV
读取,然后将其保存到mongoDB
中:
| personalInformation readData columnName columnData aKid |
"init variable"
personalInformation := OrderedDictionary new.
"emulate CSV reading"
readData := (NeoCSVReader on: 'firstName, surname, ageJohn, 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"
这只是给您大概的想法
在您的MongoDB中查询:
db.Kids.find()
您应该看到存储的信息。
免责声明:即使您的代码也可以,我没有时间在mongodb上进行对其进行测试。