Home > Software design >  MongoDB $switch case match with $regex
MongoDB $switch case match with $regex

Time:06-15

I want use $addFields based on regex match in switch case.

Logic is if title match regex then return title or if firstName and lastName match regex then return firstName and lastName with $concat.

I have some more case here is two for example.

I tried below way but not sure how to achieve this.

Playground for Test: Link

{
    $addFields: {
        text: {
            $switch: {
                branches: [
                { 
                    case: { title: { $regex: "search", $options: 'i' } }, then: '$title' },
                {
                    case: {
                        $or: [
                            { 'user.firstName': { $regex: "search", $options: 'i' } },
                            { 'user.lastName': { $regex: "search", $options: 'i' } },
                        ],
                    },
                    then: { $concat: ['$user.firstName', ' ', '$user.lastName'] },
                },
                ],
            },
        },
    },
},

OR

{ case: { $eq: ['$title', { $regex: search, $options: 'i' }] }, then: '$title' },

Sample Data for Try

[
  {
    "_id": "628c774c0ffe2cd088654ddd",
    "title": "project 1 test",
    "user": {
      "firstName": "cust",
      "lastName": "cust"
    }
  },
  {
    "_id": "628e08bbc92d4b969cf4c92e",
    "title": "test 3",
    "user": {
      "firstName": "test",
      "lastName": "cust"
    }
  },
  {
    "_id": "62971ae1d4e0df6adade2998",
    "title": "test new project optimise",
    "user": {
      "firstName": "cust",
      "lastName": "cust"
    }
  },
  {
    "_id": "629ed780d1e6eabef7b82c70",
    "title": "test 1 project",
    "user": {
      "firstName": "test",
      "lastName": "test"
    }
  },
  {
    "_id": "629776b5d4e0df6adade3283",
    "title": "test pro",
    "user": {
      "firstName": "cust",
      "lastName": "cust"
    }
  },
  {
    "_id": "62971d80d4e0df6adade2b96",
    "title": "new project invite",
    "user": {
      "firstName": "cust",
      "lastName": "cust"
    }
  },
  {
    "_id": "6294b28b045eeaa3a8db88b5",
    "title": "final test",
    "user": {
      "firstName": "cust",
      "lastName": "cust"
    }
  }
]

CodePudding user response:

Finally i got some solution.

Still i will look forward for better suggestions.

{
    $addFields: {
      text: {
        $switch: {
          branches: [
            {
              case: { $eq: [true, { $regexMatch: { input: '$title', regex: search, options: 'i' } }] },
              then: '$title',
            },
            {
              case: {
                $eq: [
                  true,
                  {
                    $or: [
                      { $regexMatch: { input: '$user.firstName', regex: search, options: 'i' } },
                      { $regexMatch: { input: '$user.lastName', regex: search, options: 'i' } },
                    ],
                  },
                ],
              },
              then: { $concat: ['$user.firstName', ' ', '$user.lastName'] },
            },
          ],
        },
      },
    },
},
  • Related