Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何匹配列表中的项目_Database_Mongodb_Nosql - Fatal编程技术网

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
字段,并让我们假设符合此条件的项目的
颜色
字段?