Browse Source

工具类新增方法

张金坤 9 years ago
parent
commit
9343f7db8f
1 changed files with 28 additions and 0 deletions
  1. 28 0
      common/src/qfw/util/mongodbutil/mongodbutil.go

+ 28 - 0
common/src/qfw/util/mongodbutil/mongodbutil.go

@@ -300,6 +300,34 @@ func SaveBulk(c, alias, db string, doc ...map[string]interface{}) bool {
 	return b
 }
 
+//统计
+func Count(c, alias, db string, query interface{}) int {
+	sess := GetMgoConn(alias)
+	var n int = 0
+	if sess != nil {
+		defer func() {
+			if r := recover(); r != nil {
+				log.Println("[E]", r)
+				for skip := 1; ; skip++ {
+					_, file, line, ok := runtime.Caller(skip)
+					if !ok {
+						break
+					}
+					go log.Printf("%v,%v\n", file, line)
+				}
+			}
+		}()
+		defer DestoryMongoConn(alias, sess)
+		coll := sess.DB(db).C(c)
+		var err error
+		n, err = coll.Find(ObjToM(query)).Count()
+		if nil != err {
+			log.Println("CountError", err)
+		}
+	}
+	return n
+}
+
 //对象转数组
 func ObjToArr(obj interface{}) []string {
 	if s, ok := obj.(string); ok {