Database 如何匹配列表中的项目
我试图展示的项目,有特定的名称品牌。 这是该字段的外观:Database 如何匹配列表中的项目,database,mongodb,nosql,Database,Mongodb,Nosql,我试图展示的项目,有特定的名称品牌。 这是该字段的外观: "brand" : [ [ "Samsung", "Iphone", "Huawei" ] ] 尝试了该查询,但我得到0个结果: db.collection.aggregate([{$match: { brand: "Samsung" }}]) 有什么想法吗,有什么问题吗?您的数据模型是一个数组数组,因此您必须处理二维数据。您可以先使用with with
"brand" : [
[
"Samsung",
"Iphone",
"Huawei"
]
]
尝试了该查询,但我得到0个结果:
db.collection.aggregate([{$match: { brand: "Samsung" }}])
有什么想法吗,有什么问题吗?您的数据模型是一个数组数组,因此您必须处理二维数据。您可以先使用with with,然后使用查看是否有与您的条件匹配的子数组:
db.collection.aggregate([
{
$match: {
$expr: {
$anyElementTrue: {
$map: {
input: "$brand",
in: { $in: [ "Samsung", "$$this" ] }
}
}
}
}
},
{
$project: {
_id: 1,
color: 1
}
}
]);
编辑:用于仅显示特定字段必须添加哪些内容才能仅显示
id
字段,并让我们假设符合此条件的项目的颜色
字段?