在这里,我只显示了一个文件,但数组有这样多个文件,我想得到所有的文件,有内部存储(内存->内部)的128 GB
数据类型:
[
{
"_id": "654686dc65bc1013eae01dad",
"name": "Samsung Galaxy Tab A9+",
"detailSpec": [
{
"category": "Network",
"specifications": [
{
"name": "Technology",
"value": "GSM / HSPA / LTE / 5G",
"_id": "654686dc65bc1013eae01daf"
},
{
"name": "2G bands",
"value": "GSM 850 / 900 / 1800 / 1900 ",
"_id": "654686dc65bc1013eae01db0"
}
],
"_id": "654686dc65bc1013eae01dae"
},
{
"category": "Memory",
"specifications": [
{
"name": "Card slot",
"value": "microSDXC (dedicated slot)",
"_id": "654686dc65bc1013eae01dc6"
},
{
"name": "Internal",
"value": "64GB 4GB RAM, 128GB 8GB RAM",
"_id": "654686dc65bc1013eae01dc7"
}
],
"_id": "654686dc65bc1013eae01dc5"
},
],
"quickSpec": [
{
"name": "Display size",
"value": "11.0\"",
"_id": "654686dc65bc1013eae01de2"
}
],
"__v": 0
}
]
字符串
我已经尝试过这个查询,但问题是,它正在考虑所有类别的值字段,但我只想考虑“内存”类别,然后只检查“内存”类别内的值字段:
const filteredData = await Models.devices.find({
$and: [
{
$and: [
{"detailSpec.category": "Memory"},
{"detailSpec.specifications.name": "Internal"},
{"detailSpec.specifications.value": new RegExp(informationAboutFilter.storage) }
]
}
]
})
console.log(new RegExp(informationAboutFilter.storage))
Output: /128/
的数据
1条答案
按热度按时间aij0ehis1#
您可以使用$elemMatch只匹配
detailSpec
数组中的对象。然后您可以在投影对象中使用positional operator$
,因为:位置$运算符将数组的内容限制为返回与数组上的查询条件匹配的第一个元素。当在选定文档中只需要一个特定数组元素时,在find()方法或findOne()方法的投影文档中使用$。
将您的查询更改为:
字符串
你会注意到
"$regex": "128"
的使用,因为new RegExp
函数在mongoplayground上不可用,但它们是等价的。