Grails で mongodb を試してみてください。mongodb:1.3.0 プラグインを使用しています。
私はこれらのドメインを持っています:
class User {
String username
String passwordHash
String email
String pic
static hasMany = [ roles: Role ]
static constraints = {
username(nullable: false, blank: false, unique: true)
passwordHash(nullable: false)
email(nullable: true, unique: true)
pic(nullable: true)
}
static mapping = {
username index: true
}
}
と
class RespondentProfile {
String id
User userAccount
List<RespondentGoldHistory> goldHistory = []
String referer
List<String> references = []
long trust = 0
long gold = 0
static embedded = ["userAccount", "goldHistory"]
static constraints = {
referer (nullable: true)
}
}
BootStrapでそれらを初期化しようとしましたが、
...
def defaultUser = User.findByUsername('user123')?:new User(username: "user123", passwordHash: new Sha256Hash("password").toHex())
defaultUser.addToRoles(surveyorRole)
defaultUser.addToRoles(respondentRole)
defaultUser.save()
def foo = RespondentProfile.createCriteria().list {userAccount{eq 'username','user123'}}
def defaultRespondentProfile = foo && foo.size() > 0 ? foo.get(0) : new RespondentProfile(userAccount: defaultUser)
BasicDBList profileItems = (BasicDBList) com.mongodb.util.JSON.parse('[]')
profileItems.add([code:'PI_ADDR001', value: 'nowhere'] )
profileItems.add([code:'PI_OCCUPATION001', value: 'jobless'] )
profileItems.add([code:'PI_COUNTRY001', value: 'ID'] )
profileItems.add([code:'PI_DOB001', value: Date.parse('dd/MM/yyyy', '30/06/1985')])
profileItems.add([code:'PI_HEIGHT001', value: 179] )
profileItems.add([code:'PI_WEIGHT001', value: 65] )
profileItems.add([code:'PI_EDU001', value: 'DEGREE'] )
profileItems.add([code:'PI_HOBBY001', value: 'Game'] )
defaultRespondentProfile['profileItems'] = profileItems
defaultRespondentProfile.save()
...
defaultRespondentProfile.save()
私はいつもこのエラーに遭遇した後:
Error 2013-07-07 12:15:09,708 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Could not commit Datastore transaction; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}; nested exception is com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}
Message: Could not commit Datastore transaction; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}; nested exception is com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not okForStorage" , "code" : 12527 , "n" : 0 , "connectionId" : 17 , "ok" : 1.0}
ここで同様の質問を見つけますが、フィールド名またはキーに無効な文字はありません
もう1つの「奇妙な」ことは、見つけたときにレコードが正常に保存されることです
どんなアイデアでも大歓迎です:)