[英]Find item with `Contains` condition using Express and Mongoose
I want to search for items in my collection where the userName contains adm
.我想在我的集合中搜索 userName 包含adm
的项目。
I expect to get 2 results, because I have 2 records in my database where the userNames are admin0
and admin2
, but it does not return anything.我希望得到 2 个结果,因为我的数据库中有 2 条记录,其中 userNames 是admin0
和admin2
,但它不返回任何内容。
I'm using this query:我正在使用这个查询:
Person
.find({
userName: { $in: 'adm'}
})
What is the problem?问题是什么? How can I search and find persons where the userName contains adm
?如何搜索和查找 userName 包含adm
的人?
You have to use regular expression or $regex
when finding the Persons.查找人员时必须使用正则表达式或$regex
。
Here is the snippet:这是片段:
Person.find({username: {$regex: '(.*)adm(.*)'}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.