繁体   English   中英

mongodb 匹配数组中的项目作为聚合管道的一部分

[英]mongodb match an item in an array as part of aggregation pipeline

如果一个值在文档数组中或满足其他条件,我正在尝试匹配记录。 我没有收到错误消息,但没有返回任何记录。

感谢任何帮助(我已经查看了有关数组和聚合的其他帖子 - 我也无法让它们工作 - 所以这可能是重复的,抱歉)

数据:

_id:5c93f908aa338366bd00b966
name:"Davidson"
type:["G"]


_id:5c93f908aa338366bd00b977
name:"Robertson"
type:["G","R"]

询问:

thing.aggregate([{
    $match: {
        $or: [
            {
                "name": { 
                    $regex: "xx", 
                    $options: "i" 
                }
            },
            { 
                "type": {
                    $elemMatch: {
                        "type": "G"
                    }
                }
            }
        ]            
    }   
}])

你不需要$elemMatch

db.col.aggregate([{
    $match: {
        $or: [
            {
                "name": { 
                    $regex: "xx", 
                    $options: "i" 
                }
            },
            { 
                "type": "G"
            }
        ]            
    }   
}])

它检查type数组是否包含任何"G"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM