mongo aggregation query with mgo driver

huangapple go评论82阅读模式
英文:

mongo aggregation query with mgo driver

问题

我在mongodb中有以下查询:

db.devices.aggregate({
$match: {userId: "v73TuQqZykbxFXsWo", state: true}},
{
  $project: {
    userId: 1,
    categorySlug: 1,
    weight: { 
      $cond: [ 
        {"$or": [  
          {$eq: ["$categorySlug", "air_fryer"] }, 
          {$eq: ["$categorySlug", "iron"] } 
        ] }, 
      0, 1] } 
    } },  
    {$sort: {weight: 1}},
    { $limit : 10 }
);

我正在尝试使用mgo驱动程序将其转换为Go代码,但是完全无法理解!
请问如何将其转换为Go mgo查询?

英文:

I have the following query in mongodb:

db.devices.aggregate({
$match: {userId: "v73TuQqZykbxFXsWo", state: true}},
{
  $project: {
    userId: 1,
    categorySlug: 1,
    weight: { 
      $cond: [ 
        {"$or": [  
          {$eq: ["$categorySlug", "air_fryer"] }, 
          {$eq: ["$categorySlug", "iron"] } 
        ] }, 
      0, 1] } 
    } },  
    {$sort: {weight: 1}},
    { $limit : 10 }
);

I'm trying to write this in Go using the mgo driver but not able to wrap my head around this at all!

How do I translate this to a Go mgo query?

答案1

得分: 7

文档中的示例足以开始使用。然而,如果您对golang不熟悉,$cond部分可能有点棘手。请参考下面的示例代码:

collection := session.DB("dbName").C("devices")

stage_match := bson.M{"$match": bson.M{"userId":"v73TuQqZykbxFXsWo", "state": true}}

condition_weight := []interface{}{bson.M{"$or": []bson.M{
                   bson.M{"$eq": []string{"$categorySlug", "air_fryer"}},
                   bson.M{"$eq": []string{"$categorySlug", "iron"}},
}}, 0, 1}

stage_project:= bson.M{"$project": bson.M{"userId":1, "categorySlug":1, "weight": condition_weight}}

stage_sort := bson.M{"$sort": bson.M{"weight":1}}

stage_limit := bson.M{"$limit": 10}

pipe := collection.Pipe([]bson.M{stage_match, stage_project, stage_sort, stage_limit})

另请参阅mgo: type Pipe

英文:

The examples on the docs would be sufficient to get started. However, if you are not familiar with golang, the $cond part could be a bit tricky. See below example code:

<!--lang:lang-go-->

    collection := session.DB(&quot;dbName&quot;).C(&quot;devices&quot;)

    stage_match := bson.M{&quot;$match&quot;:bson.M{&quot;userId&quot;:&quot;v73TuQqZykbxFXsWo&quot;, &quot;state&quot;: true}}

    condition_weight := []interface{}{bson.M{&quot;$or&quot;: []bson.M{
                       bson.M{&quot;$eq&quot;: []string{&quot;$categorySlug&quot;, &quot;air_fryer&quot;}},
                       bson.M{&quot;$eq&quot;: []string{&quot;$categorySlug&quot;, &quot;iron&quot;}},
    }}, 0, 1}

    stage_project:= bson.M{&quot;$project&quot;: bson.M{&quot;userId&quot;:1, &quot;categorySlug&quot;:1, &quot;weight&quot;: condition_weight}}

    stage_sort := bson.M{&quot;$sort&quot;: bson.M{&quot;weight&quot;:1}}

    stage_limit := bson.M{&quot;$limit&quot;: 10}

    pipe := collection.Pipe([]bson.M{stage_match, stage_project, stage_sort, stage_limit})

See also mgo: type Pipe

huangapple
  • 本文由 发表于 2016年10月26日 17:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/40259171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定