[英]Fetch documents where at least one element of an array contains a substring
I have a document of the following form in MongoDB:我在 MongoDB 中有以下形式的文档:
product = {
items : Array
}
....sample ....样本
{
"items": ["bananas","apples","grapes","guavas","pineapples"]
}
I want to fetch all documents where one of the items' elements contains a substring called apple
我想获取其中一个项目元素包含名为
apple
的 substring 的所有文档
Ideally, apples
and pineapples
in the array contain the substring apple
so the document is supposed to match.理想情况下,数组中的
apples
和pineapples
包含 substring apple
,因此文档应该匹配。
What I've tried with no success我尝试过的没有成功
let prods = await Product.find({items: {"$in":["apple"]}); //But not working
Any ideas of a better way would be appreciated.任何更好的方法的想法将不胜感激。
Thanks谢谢
You simply need to check regular expression condition,您只需要检查正则表达式条件,
$regex
operator to check the condition$regex
运算符检查条件let prods = await Product.find({
items: {
$regex: "apple"
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.