我的数据库中的“用户”集合/表数据如下所示:-
{
_id: 1,
name: "Vikas",
subject: "Computers"
},
{
_id: 2,
name: "Hello",
subject: "Computers"
},
{
_id: 3,
name: "Abc",
subject: "Mechanical"
}
以下查询将给出机械数据的结果:-
Mongodb:-
db.test.find({subject:/mech/i})
->
Mysql:-
select * from test where subject like "%mech%"
但是我想要这样的搜索,如果我搜索“mechanicalData”,那么这不会给出机械数据的结果。
我希望我能够解答我的问题。 提前致谢!
最佳答案
For MongoDB
db.test.find({subject: /^mech$/i}) //Search exact word
db.test.find({subject: /mech$/i}) //Search word which ending with "mech"
db.test.find({subject: /^mech/i}) //Search word which start with "mech"
db.test.find({subject: /mech/i}) //Search word at any position
For Mysql
select * from test where subject like "mech" //Search exact word
select * from test where subject like "%mech" //Search word which ending with "metch"
select * from test where subject like "mech%" //Search word which start with "metch"
select * from test where subject like "%mech%" //Search word at any position
关于mysql - 如何在mongodb和mysql中搜索搜索到的子字符串中的某个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53628185/