mongoose MongoDB $switch大小写匹配$regex

cuxqih21  于 2023-06-23  发布在  Go
关注(0)|答案(2)|浏览(98)

我想使用$addFields基于正则表达式匹配开关的情况下。
逻辑是,如果title匹配regex,则返回title,或者如果firstNamelastName匹配regex,则返回firstNamelastName以及$concat
我还有一些例子,这里是两个例子。
我试过下面的方法,但不知道如何做到这一点。
试验场地: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'] },
                },
                ],
            },
        },
    },
},

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

试用样本数据

[
  {
    "_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"
    }
  }
]
xj3cbfub

xj3cbfub1#

我终于找到了解决办法。
不过,我还是会期待更好的建议。

{
    $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'] },
            },
          ],
        },
      },
    },
},
s3fp2yjn

s3fp2yjn2#

{
      case: { $regexMatch: { input: '$title', regex: search, options: 'i' } },
      then: '$title',
},

应该也一样有效

相关问题