MongoDB 模糊查询

2年前 (2022) 程序员胖胖胖虎阿
232 0 0

在搜索接口的开发中,经常会遇到需要模糊匹配的场景,记录一下MySQL和MongoDB分别是怎么做的。

对比

-- SQL:
SELECT * FROM users WHERE username LIKE "%l%"

-- MongoDB:
db.users.find({username: /l/})
// 或者
db.users.find({username: { $regex: 'l'})

LIKE模糊查询username以字母"张"开头的数据(张%)

-- SQL:
SELECT * FROM users WHERE username LIKE "张%"

-- MongoDB:
db.users.find({username: /^张/})
版权声明:程序员胖胖胖虎阿 发表于 2022年11月4日 下午8:08。
转载请注明:MongoDB 模糊查询 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...