2016-07-19 39 views
2

我有一条SQL语句,我试图在使用MongoDB数据库的golang中重新创建。我试图重新声明如下:golang mongodb中的Transact-SQL等效集合

select date, 
     sum(case when field1 = "true" then 1 else 0) trueField1, 
     sum(case when field1 = "false" then 1 else 0) falseField1, 
     sum(case when field2 = "true" then 1 else 0) trueField2, 
     sum(case when field2 = "false" then 1 else 0) falseField2 
from myTable 
group by date 

我需要总出几个组合在给定日期和倾倒出来,但我不能确定如何通过golang/MongoDB的完成它。

编辑:这里是我每过最后一次请求的起点。从o1中可以看出,它显示了我在第一个总和/次数后要做什么。我还想总结另一个字段是送出并在相同的日期将它们相加,并返回同一天的计数。我可以得到一些关于我如何完成这项任务的方向?

o1 := bson.M{ 
     "$match" : bson.M { 
      "retailer" : clientId, 
      "isComplete" : true, 
      "pkgStatus.finalized" : true, 
      }, 
    } 

    o2 := bson.M { 
     "$project" : bson.M { 
      "_id" : 1, 
      "createdAt" : 1, 
     }, 
    } 

    o3 := bson.M{ 
     "$group": bson.M{ 
      "_id" : bson.M{ "$dayOfYear": "$createdAt" }, 
      "total" : bson.M{ "$sum" : 1}, 
      "first" : bson.M{ "$min" : "$createdAt" }, 
     }, 
    } 

    o4 := bson.M { 
     "$sort" : bson.M { "_id" : 1 }, 
    } 

    totalMessages := []bson.M{msgsStarted, o2, o3, o4} 

    operations := []bson.M{o1, o2, o3, o4} 

    pipe := cMessages.Pipe(operations) 

    results := []bson.M{} 
    err := pipe.All(&results) 

    if err != nil { 
     fmt.Printf("Error: %s\n", err) 
     return 
    } 

    for _, resultRec := range results { 
     myDate := fmt.Sprintf("%s", resultRec["first"]) 
     fmt.Printf("%s, %d\n", myDate[0:10], resultRec["total"]) 
    } 

EDIT2

Schema定义

messages { 
    "_id" : { 
    "$oid" : bson.ObjectId 
    }, 
    "isComplete" : bool, 
    "status" : { 
    "cancelled" : bool, 
    "finalized" : bool, 
    "delivered" : bool 
    }, 
    "createdDate" : { 
    "$date" : ISODate 
    } 

我试图把你之前提供指导$ COND报表和嵌套其与$和命令,使我可以做到以下几点:

sum(case when isComplete = true and status.finalized = true then 1 else 0) 

我b een玩以下内容:

tf1c := bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"iscomplete", true}}, 1, 0}} 

但不确定的语法。我相信它应该遵循这样的事情,但不会再知道如何它到底(下面是从另一个计算器线程)翻译

"$cond": [{ 
    "$and": [{ 
     "$eq": ["isComplete", true] 
    }, { 
     "$eq": ["pkgStatus.finalized", true] 
    }] 
}, 1, 0] 

感谢您的指导!

解缠地图

你如何解开地图?

map[_id:map[date:2014-12-25 retailer:ObjectIdHex("548a9de8a4ea9d690f6df8e4")]] 

检索值。我试过以下,但它返回null。

fmt.Printf("%s\n", resultRec["_id.date"]) 
+0

有你甚至试图读取[MongoDB的聚合框架文件(HTTPS: //docs.mongodb.com/manual/aggregation/)在提问之前?顺便说一句,NoSQL并不意味着我们不需要关于文档结构的信息。 –

+0

是的,我确实阅读了文档,但仍然遇到问题,因此我发布了该问题。这应该是我们在遇到问题时得到帮助的地方。 – CRob

+0

我在这里看不到问题。问题是当你试图做某件事情时,你的尝试结果不起作用。问题是,当你不明白事情是如何工作的,而这件事情没有记录。就你而言,我所看到的只是为你免费为你做一些工作的请求,而不需要你方的任何努力来自己解决。当然,除了写这篇文章外。向我们展示您尝试过的查询/代码,我们会看到它有什么问题。 –

回答

0

好的。这样更好,但仍然没有关于数据库模式的信息来了解您正在使用的数据类型。这里是蒙戈的相当于SQL查询的情况下,如果所有字段是字符串格式:

package main 

import (
    "gopkg.in/mgo.v2" 
    "gopkg.in/mgo.v2/bson" 
    "fmt" 
) 

func main() { 
    session, err := mgo.Dial("mongodb://127.0.0.1:27017/db") 

    if err != nil { 
     panic(err) 
    } 
    defer session.Close() 
    session.SetMode(mgo.Monotonic, true) 

    db := session.DB("db") 
    c := db.C("MyTable") 

    tf1c := bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$field1", "true"}}, 1, 0}} 
    ff1c := bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$field1", "false"}}, 1, 0}} 
    tf2c := bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$field2", "true"}}, 1, 0}} 
    ff2c := bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$field2", "false"}}, 1, 0}} 

    pipe := c.Pipe(
     []bson.M{ 
      bson.M{ 
       "$group": bson.M{ 
        "_id": "$date", 
        "trueField1": bson.M{"$sum": tf1c}, 
        "falseField1": bson.M{"$sum": ff1c}, 
        "trueField2": bson.M{"$sum": tf2c}, 
        "falseField2": bson.M{"$sum": ff2c}, 
       }, 
      }, 
      bson.M{ 
       "$sort": bson.M{"_id": -1}, 
      }, 
     }, 
    ) 
    result := []bson.M{} 
    err = pipe.All(&result) 
    if err != nil { 
     panic(err) 
    } 
    fmt.Printf("%+v", result) 
} 

更新

pipe := c.Pipe(
     []bson.M{ 
      bson.M{ 
       "$project": bson.M{ 
        "year": bson.M{"$year": "$createdDate"}, 
        "month": bson.M{"$month": "$createdDate"}, 
        "day": bson.M{"$dayOfMonth": "$createdDate"}, 
        "val": bson.M{"$cond":[]interface{}{ 
         bson.M{ 
          "$and": []interface{}{ 
           bson.M{"$eq": []interface{}{"$isComplete", true}}, 
           bson.M{"$eq": []interface{}{"$status.finalized", true}}, 
          }, 
         }, 
         1, 
         0, 
        }}, 
       }, 
      }, 
      bson.M{ 
       "$group": bson.M{ 
        "_id": bson.M{ 
         "$concat": []interface{}{ 
          bson.M{"$substr": []interface{}{"$year", 0, -1}}, 
          "-", 
          bson.M{"$substr": []interface{}{"$month", 0, -1}}, 
          "-", 
          bson.M{"$substr": []interface{}{"$day", 0, -1}}, 
         }, 
        }, 
        "cnt": bson.M{"$sum": "$val"}, 
       }, 
      }, 
      bson.M{ 
       "$sort": bson.M{"_id": -1}, 
      }, 
     }, 
    ) 
+0

这是非常有用的,我想投票你的答案,但不能由于我的名誉太低。否则,它肯定会完成。 – CRob

+0

Roman,我在网上寻找帮助扩展$ cond以包括$和。我没有看到太多的例子,你能指出我在哪里寻找正确的方向?我阅读了上面指出的文档。我还发现了另一个stackoverflow线程,但它没有为bson格式化。我应该开始一个新的线程来展示我尝试过的东西,还是扩展这个东西? – CRob

+0

@CROB我认为将会更容易保留在这里。并且不要忘记链接SQL版本的查询,因为它更容易理解您期待的最终结果。 –