登录
首页 >  Golang >  Go问答

比较 Golang 中所有值而不使用 MongoDB

来源:stackoverflow

时间:2024-02-24 18:06:26 240浏览 收藏

哈喽!今天心血来潮给大家带来了《比较 Golang 中所有值而不使用 MongoDB》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我使用 golang 作为 mongodb 的后端

我的收藏是

**department**
    {
     dept_id:1,
     dept_name:'cse',
     dept_overview:'overview'
    }
   ................

**employee**
    {
    emp_id:1,
    emp_name:'abc',
    qualification:'phd',
    emp_dept:'cse',
    city:'xyz'
    }
    {
    emp_id:2,
    emp_name:'xyz',
    qualification:'phd',
    emp_dept:['cse','me'],
    city:'xyz',
    status:1
    }
    ..........

下面是我使用管道的 go 代码

var conditionParam []bson.M
        if city == "" {
            conditionParam = []bson.M{
                bson.M{"$eq": []string{"$$element.qualification", "PHD"}},
                bson.M{"$in": []interface{}{"$$element.emp_dept", ["CSE"]}},
                bson.M{"$or": []interface{}{"$$element.emp_dept", "CSE"}},
                bson.M{"$eq": []string{"$$element.city", "xyz"}},
                bson.M{"$or": []interface{}{"$exists", []interface{}{"$$element.status", false}}},
                bson.M{"$or": []interface{}{"$$element.status", 1}}, 
            }
    } else if(){
    --------------------
}

matchStage:=bson.M{"$match":bson.M{'dept_id':1}}
lookupStage:=bson.M{"$lookup": bson.M{
    "from":         "employee",
    "localField":   "dept_name",
    "foreignField": "emp_dept",
    "as":           "result_list",
}}
    pipeline := getCollection.Pipe([]bson.M{
            matchStage,
            lookupStage,
            {"$addFields": bson.M{
                "result_list": bson.M{
                    "$filter": bson.M{
                        "input": "$result_list",
                        "as":    "element",
                        "cond": bson.M{
                            "$and": conditionParam,
                        },
                    },
                },
            }},
        })

在我的集合中,emp_dept 的一些数据存储在 string 中,部分数据存储在 string slice 中。因此,我使用 $in 来比较切片值,使用 $or 来比较 emp_dept 的字符串值。$in 运算符适用于数据库字符串值,但不适用于比较切片中的 emp_dept 数据库值。我们如何为 mongodb 集合的这个特定键返回切片和字符串的值。


解决方案


我理解的问题是,您需要通过 $result_list.emp_dept 过滤数组,它有两种不同的类型,即 stringarray。您可以使用 $type 来检查它是数组还是字符串。如果场景正确,则以下查询可以正常工作。由于您已经在go中编写了管道,因此我希望您可以将以下查询转换为go。 bcs 我不是 go 开发人员。

[
  {
    "$lookup": {
      "from": "employee",
      "localField": "dept_name",
      "foreignField": "emp_dept",
      "as": "result_list"
    }
  },
  {
    $addFields: {
      "result_list": {
        $filter: {
          input: "$result_list",
          cond: {
            $cond: [
              {
                $eq: [ { $type: "$$this.emp_dept" }, "string" ]
              },
              { // if the type is string
                $and: [
                  {
                    $eq: [ "CSE", "$$this.emp_dept" ]
                  },
                  {
                    $eq: [ "$$this.qualification", "PHD" ]
                  },
                  {
                    $eq: [ "$$this.city", "xyz" ]
                  }
                  // Other comparison
                ]
              },
              {  // if the type is NOT string
                $and: [
                  {
                    $in: [ "CSE", "$$this.emp_dept" ]
                  },
                  {
                    $eq: [ "$$this.qualification", "PHD" ]
                  },
                  {
                    $eq: [ "$$this.city", "xyz" ]
                  }
                  // Other comparison
                ]
              }
            ]
          }
        }
      }
    }
  }
]

工作Mongo playground

今天关于《比较 Golang 中所有值而不使用 MongoDB》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>