只需在 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}
在这里找到类似的问题,但我的字段名称或键没有无效字符
另一件“奇怪”的事情是当我找到记录时成功存储
任何想法表示赞赏:)