我有一个要求,如果布尔变量(已爬网)的值为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" }
}
答案 0 :(得分:0)
你应该使用
Website.mongo.update(...)
或让框架注入它:
class ResourceController {
def mongo
def list(){
mongo.website.update(...)
}
}
答案 1 :(得分:0)
这对我有用。如果其他人有类似的要求,请在此处发布。
@Grab(group='com.gmongo', module='gmongo', version='0.9.3')
import com.gmongo.GMongo
@Transactional(readOnly = true)
class ResourceController {
def mongo = new GMongo()
def db = mongo.getDB("resources")
def p = Website.findByWebsite(website)
if(p['crawled']==false){
db.website.update([crawled:false],[$set:[crawled:true]])
}