1

A document:

{
   objectId: 35,
   properties: [
     {
       pset: 'A1',
       property: 'Fire',
       value: '60'
     },
     {
       pset: 'B1',
       property: 'Fire',
       value: '70'
     }  
   ]
}

Input to query:

{ 
   psetsWithProperties: [{ pset: 'A1', property: 'Fire'}, { pset: 'abc', property: 'fff'}]
}

The combination of pset and property is always uniq. So there can not be two 'A1' and 'Fire' in the properties collection.

Expected output:

[{
  objectId: 35,
  properties: {
    'A1/Fire': '60'
  }
}]

So the second item (B1, Fire) is not included since it's not in the input.

If there is no match at all, the properties object is empty:

[{
  objectId: 35,
  properties: {}
}]

The MongoDB aggregation framework should be used here.

Something with $in, $group, $unwind I think.

Don't really know where to start.

Does not have to be exactly as in my examples.

Any ideas?

2
  • Just to clarify: why objectId: 35 and do you really need to convert from '60' to 60 ? Commented May 9, 2019 at 16:23
  • 1
    @mickl Sorry, mistyping. Updated question. Commented May 9, 2019 at 16:33

1 Answer 1

1

To match an array against another array you can use $filter with $anyElementTrue and $map. Then to generate JSON key values dynamically you need $arrayToObject operator, try:

db.col.aggregate([
    {
        $project: {
            _id: 0,
            objectId: 1,
            data: {
                $filter: {
                    input: "$properties",
                    as: "docProp",
                    cond: {
                        $anyElementTrue: {
                            $map: {
                                input: [{ pset: 'A1', property: 'Fire'}, { pset: 'abc', property: 'fff'}],
                                as: "paramProp",
                                in: {
                                    $and: [
                                        { $eq: [ "$$docProp.pset", "$$paramProp.pset" ] },
                                        { $eq: [ "$$docProp.property", "$$paramProp.property" ] },
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    {
        $project: {
            objectId: 1,
            properties: {
                $arrayToObject: {
                    $map: {
                        input: "$data",
                        in: {
                            k: { $concat: [ "$$this.pset", "/", "$$this.property" ] },
                            v: "$$this.value"
                        }
                    }
                }
            }
        }
    }
])
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thats fast. Workday has come to an end. I'll try it out tomorrow (and accept answer if it works). Thx!

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.