mongodb - 如何在 MongoDB 中使用正则表达式访问数组中的数组
问题描述
所以我想从规范中访问值并过滤掉具有所需值的变体。 产品架构
({
title: { type: String },
brandName : { type : String },
ratings : { type: Number, default : 0 },
specification: [{
attribute: { type : String },
values: [{ type : String }],
}],
variant: [{
specifications: [{
attribute: { type : String },
values: { type : String }
}],
}],
});
我想根据请求的参数过滤掉产品。
过滤对象
{
"title": "string",
"brandName": "string",
"ratings": 0,
"color": [
"string"
],
"seat": [
"string"
]
}
这是我的过滤器对象,我将此对象传递给一个函数,然后在数据库中查找它。
过滤功能
public async getAllProducts(param){
const skip = (page - 1) * limit;
const users = (await this.productModel
.find(
{
$or: [
{
title : param.title
},
{
brandName : param.brandName
},
{
ratings: param.ratings
},
{
"variant.specifications.values" : {"$in": param.color }
},
{
"variant.specifications.values" : {"$in" : param.seat }
}
]
}
)
.limit(limit)
.skip(skip)
return users;
}
在这里,我尝试传入 Filter Obj 并使用 $in 运算符来检查 Filter Obj 的颜色和座位数组中的每个值。它会正确地向我返回带有标题、品牌名称和评级的产品,但它不适用于颜色和座椅阵列。
解决方案
推荐阅读
- javascript - 有没有办法让机器人知道公会成员何时登录不和谐服务器?
- xml - 从打印文件中捕获文本
- javascript - React:如何在使用 react-scroll 滚动时禁用单击?
- python - 为什么我的while循环会再次要求输入,即使它符合条件
- css - 为什么我的网格项目在 safari 中没有在 chrome 和 firefox 中拉伸
- lightningchart - 如何配置类似于 TradingView 的 LightningChart JS 鼠标交互
- android - Hilt - 我应该@Provides 我的基本 URL 还是 API 密钥?
- xamarin.android - 如何开发 O365 Active Directory 登录 Android?
- count - Flink 可以归零吗?
- java - iText 7绝对位置文本仅出现在最后一页