mongodb - 从 Grails 应用程序更新 Mongo 文档字段

标签 mongodb grails groovy updates

我有一个要求,如果 bool 变量(已抓取)的值为 false,我必须检查数据库。如果是这样,我必须将其设置为 true。我正在根据字符串变量(网站)的值查找记录。我引用了this link但这没有帮助。请告诉我我做错了什么。

我试过这个:

def p = Website.findByWebsite(website);
if(p['crawled'] == true) {
    println "already crawled"
} else {
    mongo.website.update( p, new BasicDBObject( '$set', new BasicDBObject( 'crawled', 'false' ) ) )
    println "updated object"
}

它给了我一个错误No such property: mongo for class: cmsprofiler.ResourceController
我的域类如下:
  class Website{
    String website
    User user
    Boolean crawled
    static belongsTo = [user: User]
    static constraints = {
        website( url:true, unique: ['user'])
    }
    static hasMany = [resource: Resource]
    static mapping = {resource cascade:"all-delete-orphan" }
}

最佳答案

你应该使用

Website.mongo.update(...)

或者让框架注入(inject)它:
class ResourceController {
  def mongo

  def list(){
     mongo.website.update(...)
  }

}

关于mongodb - 从 Grails 应用程序更新 Mongo 文档字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738416/

相关文章:

java - Gradle : Could not find org. codehaus.groovy :groovy:1. 0.0

MongoDB:输出 'id' 而不是 '_id'

python - 使用 Python Eve Rest 和 Mongo 过滤嵌入式文档

mongodb - 如何在子属性上使用 $setIsSubset (或其他集合操作)

grails - Grails 3在intellij中的应用

debugging - grails 邮件 : inline images working on localhost and not in test environment

intellij-idea - 带有@GrailsCompileStatic 注解的Grails GORM 类在静态映射闭包表、版本、autoTimestamp 中显示为未解析符号

grails - 如何获取具有指定条件的所有详细信息对象?

mongodb - mongodb中的嵌入式评论分页

java - chalice 或玩!对于前 RoR 开发人员?