func TestUpdate(t *testing.T) { db, _ := mgox.GetDatabase() o1 := bson.M{ // "ip" : "113.106.106.98", "_id": bson.ObjectIdHex("5656b4833e5a2c071d00005b"), "client.country": bson.M{"$exists": true}, } o2 := bson.M{"$set": bson.M{ "client.country": "china111", "client.area": "华南1", // "client.region":"广东1", "client.city": "珠海", "client.hee": "11112", }} // o2 := bson.M{"$set":bson.M{"client":bson.M{ // "country": "china111", // "area": "华南", // "region":"广东1", // "city":"珠海", // "hee":"1111", // }}} results := []bson.M{} // _ = mgox.Dao().Set("downloadlog", o1, o2) db.C("downloadlog").UpdateAll(o1, o2) db.C("downloadlog").Find(o1).All(&results) db.C("app").UpdateAll(bson.M{"appid": "55bb3026e138230ee700000e"}, bson.M{"$set": bson.M{"downloadcount": 11, "viewcount": 555}}) // db.C("").Pipe() if len(results) > 0 { output, _ := json.MarshalIndent(results, "", " ") println("update result is :" + string(output)) } var ips []string mgox.New().Find().Distinct("downloadlog", "client.ip", &ips) // db.C("downloadlog").UpdateId(bson.ObjectIdHex("5656b4e13e5a2c071d00005d"),o2) }
func TestFindAppInfo(t *testing.T) { db, _ := mgox.GetDatabase() o1 := bson.M{ "$match": bson.M{ "appid": "55bb3026e138230ee700000e", }, } o2 := bson.M{ "$project": bson.M{ "viewcount": "$viewcount", "downloadcount": "$downloadcount", "_id": 0, }, } // operations := []bson.M{o1} operations := []bson.M{o1, o2} // =========3. do data analyse =============================================== // Prepare the query to run in the MongoDB aggregation pipeline results := []bson.M{} db.C("app").Pipe(operations).All(&results) var countinfo []CountInfo db.C("app").Pipe(operations).All(&countinfo) if len(countinfo) > 0 { println("len is :%d", len(countinfo)) println("downloadCount is :%d", countinfo[0].DownloadCount) println("viewCount is :%d", countinfo[0].ViewCount) } if len(results) > 0 { output, _ := json.MarshalIndent(results, "", " ") println("result is :" + string(output)) } }
func TestDayDownload(t *testing.T) { // 模拟数据 startDayStr := "2015-11-26" endDayStr := "2016-11-27" // //时间段,不转换时区 startDayStr += " 00:00:00" endDayStr += " 00:00:00" beginDay, _ := time.Parse("2006-01-02 15:04:05", startDayStr) endDay, _ := time.Parse("2006-01-02 15:04:05", endDayStr) // // 时间段,转换时区 // startDay, _ := time.Parse("2006-01-02", startDayStr) // endDay, _ := time.Parse("2006-01-02", en,dDayStr) // diff := int(endDay.Sub(startDay).Hours() / 24) // endDayStr += " 16:59:59" // endDay, _ = time.Parse("2006-01-02 15:04:05", endDayStr) // beginDay := endDay.AddDate(0, 0, -diff) // fmt.Println("diff is:",diff) fmt.Println("start day is:", beginDay) fmt.Println("end day is:", endDay) // 条件 o1 := bson.M{ "$match": bson.M{ "client": bson.M{"$exists": true}, "firstcreated": bson.M{"$gt": beginDay, "$lt": endDay}, // "client.ip" : "113.106.106.126", }, } o2 := bson.M{ "$group": bson.M{ "_id": bson.M{"dayInfo": bson.M{ "month": bson.M{"$month": "$localcreated"}, "day": bson.M{"$dayOfMonth": "$localcreated"}, "year": bson.M{"$year": "$localcreated"}, }}, "total": bson.M{ "$sum": 1, }, "date": bson.M{ "$first": "$localcreated", }, }, } o3 := bson.M{ "$project": bson.M{ // "day":"$_id.dayInfo.day", // "month":"$_id.dayInfo.month", // "year":"$_id.dayInfo.year", // "fullDate":bson.M{"$concat":[]string{ // {"$substr":[]int{"$_id.dayInfo.month", 0, 2}}, // "/", // {"$substr":[]int{"$_id.dayInfo.day", 0, 2}}, // {"$substr":[]int{"$_id.dayInfo.year", 0, 4}}, // todo: 参考http://stackoverflow.com/questions/26736928/mongodb-group-by-duration-span // }}, "_id": 0, "count": "$total", "date": bson.M{"$substr": []interface{}{"$date", 0, 10}}, }} o4 := bson.M{"$sort": bson.M{"date": 1}} // { $project: { element_id: '$_id.ord_dt.month', count: '$total' } }, operations := []bson.M{o1, o2, o3, o4} db, err := mgox.GetDatabase() defer db.Session.Close() if err != nil { return } results := []bson.M{} err = db.C("downloadlog").Pipe(operations).All(&results) output, _ := json.MarshalIndent(results, "", " ") println("result is :" + string(output)) var ds []DayInfoS err = db.C("downloadlog").Pipe(operations).All(&ds) // println("result is :%d" , ds[1].Day) // println("result is :%d" , ds[1].Month) // println("result is :%d" , ds[1].Year) // println("result is :%d" , ds[1].Count) // println(strconv.Itoa(ds[1].Year)+"-"+strconv.Itoa(ds[1].Month)+"-"+strconv.Itoa(ds[1].Day)) // output, _ := json.MarshalIndent(results, "", " ") // println("result is :" + string(output)) // for i:=0;i<len(results);i++{ // output, _ := json.MarshalIndent(results[i], "", " ") // c1 := gojson.Json(string(output)).Get("_id").Get("dayInfo").Get("day") // println(c1) // } }