我将Grails 3.3.5与GORM版本6.1.9一起使用
在我的应用程序中,我按如下所示创建域。
class Camera {
String cameraId
List<String> typesInclude
static constraints = {
typesInclude nullable: false
}
}
现在,我已在“摄像机关联”中添加了一些记录。
db.camera.find().pretty()
{
"_id" : NumberLong(1),
"version" : NumberLong(0),
"typesInclude" : [
"T1"
],
"cameraId" : "cam1"
}
{
"_id" : NumberLong(2),
"version" : NumberLong(0),
"typesInclude" : [
"T2"
],
"cameraId" : "cam2",
}
{
"_id" : NumberLong(3),
"version" : NumberLong(0),
"typesInclude" : [
"T2",
"T3"
],
"cameraId" : "cam3",
}
现在,当我尝试获取像T2这样的Camera By类型时。我无法使用以下功能获得结果。.
def getCameraListByType(String type){
def cameraInstanceList = Camera.createCriteria().list {
ilike("typesInclude","%${type}%")
}
return cameraInstanceList
}
任何帮助将不胜感激。
答案 0 :(得分:1)
我不会在mongo中使用标准查询,因为它们几乎不能反映面向文档的范例。
请改用native
查询,因为它们的功能更强大:
def getCameraListByType(String type){
Camera.collection.find( [ typesInclude:[ $regex:/$type/, $options:'i' ] ] ).collect{ it as Camera }
}