5

I'm trying to query an array within a collection and project the value "true" if the object exists within the array. If the object doesn't exist within the array, project "false". I am working within MongoDB and I'm not too familiar with it.

In my scenario, I have two collections that I am working with. I am aggregating a collection of 'staff' members and I am performing a $lookup function to a collection of 'businesses'. Within 'businesses' I have an array of the businesses capabilities.

For example, I have staff collection

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

Businesses collection

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

And have a $lookup setup like

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

How can I $project per staff member to a value like $canClaim: true, if "claiming" appears within "capabilities"?

2 Answers 2

8

You can use $in aggregation operator to check whether the array contains the value or not.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thanks I will try this soon. Out of curiousity, does the $unwind function affect calling fields in $business_Info?
No it will only Deconstructs your array field
1
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
                    }
                }
            }
        },

    ]



);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.