我编写了一个scala程序,用于创建新数据库,并在其中添加文档/视图。
object CouchDBTest extends App {
val dbSession = new Session("localhost", 5984)
val db = dbSession.createDatabase("couchschooltest")
val newC1 = new Document
newC1.put("Type", "Class")
newC1.put("ClassId", "C1")
newC1.put("ClassName", "C-2A")
newC1.put("ClassTeacher", "T1")
newC1.accumulate("Students", "S1");
newC1.accumulate("Students", "S2");
newC1.accumulate("Students", "S3");
db.saveDocument(newC1)
val viewDocClass = new Document
viewDocClass.addView("Class", "function(doc) {if(doc.Type == 'Class') { emit([doc.ClassId, doc.ClassName, doc.ClassTeacher, doc.Students], doc);}}")
db.saveDocument(viewDocClass)
}
当我运行此代码时,它在CouchDB中创建新数据库并在此数据库中添加类文档。但是,它不会将视图添加到数据库中。它在将viewDocClass添加为
时给出了运行时错误添加文档时出错 - null null
为此,我使用了couchdb4j API