如何在Mongo中与$匹配或$ switch语句进行$匹配

发布于 2025-01-31 04:05:22 字数 1387 浏览 6 评论 0原文

嗨,我正在尝试在开关内部进行多个搜索,所以如果以下用户之一:search_data || com:search_data ||水果:serch_data 匹配返回搜索结果。 我想这样做,

$match:{
          $or:[{ user:{$regex:"search_data"}},
          {com:{$regex:"search_data"}},
          {fruit:{$elemMatch: {name:{$regex:"search_data"}}}}
}

如果用户应该搜索用户,这是一个搜索,如果com应该搜索公司,我现在就可以做到这一点,如果我们将全部提供时,如果搜索数据在用户 com上退出, 水果应返回搜索结果

   {
            $match: {
              $expr: {
                $switch: {
                  branches: [
                    {
                      case: {
                        $eq: ['$search', 'all']
                      },
                      then: {
                       
                       //to this
                      }
                    },
                 ],
                 default: {}
              }
            }
          }

样本数据

 user:"rithuwa",
  com:"abc",
  fruit:[{name:"mango",des:"aaaaa"},{name:"mango",des:"nnnn"}]

样本输入

search_data:"rit"---->since this is in user it should output the record
search_data:"mango"--->since it in the fruit.name it should output the record
search_data:"ab"---->since it in the com it should output the record
search_data:"me"---->since it not in user,com or fruit should not give search record  

Hi I am trying to get multiple searches inside switch so i want to do if one of the following user:search_data||com:search_data||fruit:serch_data matches return search results.
i want to do,

$match:{
          $or:[{ user:{$regex:"search_data"}},
          {com:{$regex:"search_data"}},
          {fruit:{$elemMatch: {name:{$regex:"search_data"}}}}
}

this is a search if user it should search user, if com it should search company I have able to do that now i want when we gives all if search data exits on user OR Com OR fruit it should return the search results

   {
            $match: {
              $expr: {
                $switch: {
                  branches: [
                    {
                      case: {
                        $eq: ['$search', 'all']
                      },
                      then: {
                       
                       //to this
                      }
                    },
                 ],
                 default: {}
              }
            }
          }

sample data

 user:"rithuwa",
  com:"abc",
  fruit:[{name:"mango",des:"aaaaa"},{name:"mango",des:"nnnn"}]

sample input

search_data:"rit"---->since this is in user it should output the record
search_data:"mango"--->since it in the fruit.name it should output the record
search_data:"ab"---->since it in the com it should output the record
search_data:"me"---->since it not in user,com or fruit should not give search record  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2025-02-07 04:05:22

查询

  • 如果类型都是所有其他选中的等级,
  • 请根据类型

*在发送查询之前先知道类型其中一个(Bellow在服务器上这样做,并且查询更大,您可以使用4个较小的查询来完成)

Playmongo

aggregate(
[{"$set": {"searchData": "mango", "searchType": "tags"}},
 {"$match": 
   {"$expr": 
     {"$switch": 
       {"branches": 
         [{"case": {"$eq": ["$searchType", "all"]},
            "then": 
             {"$or": 
               [{"$regexMatch": {"input": "$name", "regex": "$searchData"}},
                 {"$regexMatch": {"input": "$company", "regex": "$searchData"}},
                 {"$reduce": 
                   {"input": "$tags",
                    "initialValue": false,
                    "in": 
                     {"$or": 
                       ["$value",
                         {"$regexMatch": 
                           {"input": "$this.name", "regex": "$searchData"}}]}}}]}},
           {"case": {"$eq": ["$searchType", "name"]},
            "then": 
             {"$regexMatch": {"input": "$name", "regex": "$searchData"}}},
           {"case": {"$eq": ["$searchType", "company"]},
            "then": 
             {"$regexMatch": {"input": "$company", "regex": "$searchData"}}},
           {"case": {"$eq": ["$searchType", "tags"]},
            "then": 
             {"$reduce": 
               {"input": "$tags",
                "initialValue": false,
                "in": 
                 {"$or": 
                   ["$value",
                     {"$regexMatch": 
                       {"input": "$this.name", "regex": "$searchData"}}]}}}}],
        "default": true}}}},
     {"$unset": ["searchData", "searchType"]}])

Query

  • if type is all check the regex in all
  • else check in each based on type

*you know the type before sending the query, so you can do this switch case on application code, and have 4 queries, so based on the type to send one of them (the bellow does this on the server and query is bigger, you can do it with 4 much smaller queries)

Playmongo

aggregate(
[{"$set": {"searchData": "mango", "searchType": "tags"}},
 {"$match": 
   {"$expr": 
     {"$switch": 
       {"branches": 
         [{"case": {"$eq": ["$searchType", "all"]},
            "then": 
             {"$or": 
               [{"$regexMatch": {"input": "$name", "regex": "$searchData"}},
                 {"$regexMatch": {"input": "$company", "regex": "$searchData"}},
                 {"$reduce": 
                   {"input": "$tags",
                    "initialValue": false,
                    "in": 
                     {"$or": 
                       ["$value",
                         {"$regexMatch": 
                           {"input": "$this.name", "regex": "$searchData"}}]}}}]}},
           {"case": {"$eq": ["$searchType", "name"]},
            "then": 
             {"$regexMatch": {"input": "$name", "regex": "$searchData"}}},
           {"case": {"$eq": ["$searchType", "company"]},
            "then": 
             {"$regexMatch": {"input": "$company", "regex": "$searchData"}}},
           {"case": {"$eq": ["$searchType", "tags"]},
            "then": 
             {"$reduce": 
               {"input": "$tags",
                "initialValue": false,
                "in": 
                 {"$or": 
                   ["$value",
                     {"$regexMatch": 
                       {"input": "$this.name", "regex": "$searchData"}}]}}}}],
        "default": true}}}},
     {"$unset": ["searchData", "searchType"]}])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文