【问题标题】:MongoDB: Check for array for object and if exists return true, otherwise falseMongoDB:检查对象的数组,如果存在则返回真,否则返回假
【发布时间】:2019-09-28 01:15:21
【问题描述】:

如果对象存在于数组中,我正在尝试查询集合中的数组并投影值“true”。如果该对象在数组中不存在,则投影为“false”。我在 MongoDB 中工作,对它不太熟悉。

在我的场景中,我有两个正在使用的集合。我正在聚合一组“员工”成员,并且正在对一组“企业”执行 $lookup 函数。在“业务”中,我拥有一系列业务能力。

比如我有员工收藏

staff = [
  ...
  {_id: 1, businessId: 11},
  {_id: 2, businessId: 22},
  ....
]

企业集合

businesses = [
  ...
  {_id: 11, capabilities: ["claiming", "pushing"]},
  {_id: 22, capabilities: ["claiming", "popping"]},
  ....
]

并且有一个类似的 $lookup 设置

db.getCollection('staff').aggregate([
    {
        $lookup:
            {
                from: "businesses",
                localField: "businessId",
                foreignField: "_id",
                as: "business_Info"
            }
    },

如果“claiming”出现在“capabilities”中,我如何将每个员工的 $project 设置为 $canClaim: true 之类的值?

【问题讨论】:

    标签: arrays mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您可以使用$in聚合运算符来检查数组是否包含该值。

    db.getCollection("staff").aggregate([
      { "$lookup": {
        "from": "businesses",
        "localField": "businessId",
        "foreignField": "_id",
        "as": "business_Info"
      }},
      { "$unwind": "$business_Info" },
      { "$addFields": {
        "canClaim": { "$in": ["claiming", "$business_Info.capabilities"] }
      }}
    ])
    

    MongoPlayground

    【讨论】:

    • 太棒了!谢谢,我很快就会试试这个。出于好奇,$unwind 函数会影响 $business_Info 中的调用字段吗?
    • 不,它只会解构你的数组字段
    【解决方案2】:
    db.getCollection("staff").aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $lookup: // Equality Match
                {
                    from: "businesses",
                    localField: "businessId",
                    foreignField: "_id",
                    as: "businessInfo"
                }
    
                // Uncorrelated Subqueries
                // (supported as of MongoDB 3.6)
                // {
                //    from: "<collection to join>",
                //    let: { <var_1>: <expression>, …, <var_n>: <expression> },
                //    pipeline: [ <pipeline to execute on the collection to join> ],
                //    as: "<output array field>"
                // }
            },
    
            // Stage 2
            {
                $project: {
                    businessId: 1,
                    businessInfo: {
                        $arrayElemAt: ['$businessInfo', 0]
                    }
                }
            },
    
            // Stage 3
            {
                $addFields: {
                    "businessInfo.canClaim": {
                        $cond: {
                            if: {
                                $gte: [{
                                    $indexOfArray: ["$businessInfo.capabilities", 'claiming']
                                }, 0]
                            },
                            then: true,
                            else: false
                        }
                    }
                }
            },
    
        ]
    
    
    
    );
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2016-05-03
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2017-09-17
      相关资源
      最近更新 更多