云数据库中某字段类型为string,如何在查询中使用where语句筛选该字段包含指定子串的记录?

发布于 8 年前作者 fugang7777 次浏览最后编辑 8 年前来自 issues

例如针对记录:

{    title: “abcdefd”

    num: 4

}

想要用where条件筛选title字段包含指定子串如"cde"的记录,该如何编写条件呢?

2 回复
csu
csu1 楼6 年前

db.collection(‘xxxx’).where({

    title: /(.*)?cde(.*)?/i

}).get()

或者

db.collection(‘xxxx’).aggregate()

.addFields({

    matched: $.gte([$.indexOfCP([‘$title’, ‘cde’]), 0])

})

.match({

    matched: !0

})

.end()

xiuyingliao
xiuyingliao2 楼6 年前

大佬有解答,膜拜