英文:
Mongodb - find all documents which contain value in array using $regex operator
问题
在我的Mongo数据库中执行搜索,找到所有包含特定字符串的文档。
代码的第一部分将用户输入按空格分割,然后将其推送到数组中。
代码的第二部分搜索数据库,查找数组中每个项与firstName
字段之间的匹配项。
我尝试使用正则表达式运算符来忽略大小写,并查看每个元素是否在字段的任何位置包含;即,数组元素字符串"f"
将产生与"frank"
、"doofus"
匹配,但不匹配"carlos"
等。
以下是我的代码:
var string = 'carlos f sdf';
string = string.split(" ");
var stringArray = new Array();
for(var i = 0; i < string.length; i++){
stringArray.push(string[i]);
}
User.find({firstName: { $regex: { $in: stringArray }, $options: 'i' }}).select("-password").then(user => console.log(user))
这是我遇到的错误:
UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{ '$in': [ 'carlos', 'f', 'sdf' ] }" at path "firstName" for model "users"
有什么想法,我做错了什么?
英文:
Looking to perform a search in my Mongo database which will find all documents that contain a certain string.
The first part of the code splits the user input by spaces, then pushes it to an array.
The second part of the code searches the database to find a match between each item in the array and the firstName
field.
I'm trying to use the regex operator to ignore case sensitivity and also see if each element is contained anywhere in the field; i.e. the array element string "f"
, would yield a match for "frank"
, "doofus"
, but not "carlos"
- etc.
Here is my code:
var string = 'carlos f sdf';
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
}
User.find({firstName: { $regex: {$in: stringArray}, $options: 'i'}}).select("-password").then(user=>console.log(user))
And this is the error I'm getting:
>UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{ '$in': [ 'carlos', 'f', 'sdf' ] }" at path "firstName" for model "users"
Any ideas what am I doing wrong?
答案1
得分: 1
我们可以使用"or"函数来获取结果:
User.find({
"$or": [{
firstName: { $regex: 'carlos', $options: 'i'}
},
{
firstName: { $regex: 'f', $options: 'i'}
},
{
firstName: { $regex: 'sdf', $options: 'i'}
}]
});
英文:
We can use or function for getting result
User.find({
"$or": [{
firstName: { $regex: 'carlos', $options: 'i'}
},
{
firstName: { $regex: 'f', $options: 'i'}
},
{
firstName: { $regex: 'sdf', $options: 'i'}
}]
});
答案2
得分: 0
你可以使用' | '作为正则表达式的'OR'运算符。
db.collection.find({ "firstName": { "$regex": "^carlos$|^f$|^sdf$", $options: "i" } })
上面的查询应该适用于你,如果你想匹配精确的值。
如果你想匹配包含给定字符串的文档,可以使用以下查询。
db.collection.find({ "firstName": { "$regex": "carlos|f|sdf", $options: "i" } })
至于你提到的动态长度输入,你只需要将所有这些字符串用'$|^'连接起来,然后一旦所有字符串都连接起来,你可以在开头加上'^'并在结尾加上'$'以实现精确匹配,如果要进行包含匹配,只需使用' | '运算符将所有字符串连接起来。
英文:
You can use '|' as an 'OR' operator for the regular expressions.
db.collection.find({ "firstName": { "$regex": "^carlos$|^f$|^sdf$", $options: "i" } })
The above query should work for you if you want to match the exact values.
If you want to match for those documents which contain given strings then use the below query.
db.collection.find({ "firstName": { "$regex": "carlos|f|sdf", $options: "i" } })
And as you asked about dynamic length input then you just have to Join all those string with '$|^' and once all the strings are joined you can append it with '$' and prepend it with '^' for the exact match in case of contain you just need to join all the string with '|' operator.
答案3
得分: 0
感谢大家的回复。以下是我解决问题的方法:
var string = req.params.postParams
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
}
var regex = stringArray.map(function (e) { return new RegExp(e, "i"); });
User.find({firstName: { $in: regex }).select("-password").then(user=>console.log(user))
英文:
Thank you for the responses everyone. Below is how I solved the problem
var string = req.params.postParams
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
}
var regex = stringArray.map(function (e) { return new RegExp(e, "i"); });
User.find({firstName: { $in: regex }).select("-password").then(user=>console.log(user))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论