簡單搞懂逆變與協變
阿新 • • 發佈:2021-10-18
1.查詢 Find
這裡查詢時間戳內,賬號為account,標籤為tag的資料並統計個數。 m := bson.M{ "create_time": bson.M{ "$gte": start, "$lte": end, }, "account": account, "tag": "tag", } session.DB("db").C("collect").Find(m).Count()
2.聚合管道在mgo中為Pipe(pipeline interface{})
這個和bash中使用的管道很像,資料可以被層層處理。一般傳入的引數為[]bson.M。這個[]bson.M裡如果還有巢狀則還要使用[]bson.M - 比如這裡首先匹配標籤和賬號 - 時間戳在一段時間內 - 然後根據名字分組統計數量 - 最後排序取最前面的三個。 這個就可以傳入Pipe m := []bson.M{ {"$match": bson.M{"tag": "tag", "account": account, "create_time": bson.M{"$gte": start, "$lte": end}}}, {"$group": bson.M{"_id": "$TagName", "count": bson.M{"$sum": 1}}}, {"$sort": bson.M{"count": -1}}, {"$limit": 3}, } //這裡就可以取到輸出的資料 var values []result session.DB("db").C("collect").Pipe(m).All(&values)
簡單介紹1
package main import ( "gopkg.in/mgo.v2" "log" "gopkg.in/mgo.v2/bson" ) type User struct { Id bson.ObjectId `bson:"_id"` Name string `bson:"name"` PassWord string `bson:"pass_word"` Age int `bson:"age"` } func main() { db, err := mgo.Dial("mongodb://192.168.2.28:27017,192.168.2.28:27018,192.168.2.28:27019/?replicaSet=howie") if err != nil { log.Fatalln(err) } defer db.Close() db.SetMode(mgo.Monotonic, true) c := db.DB("howie").C("person") //插入 /*c.Insert(&User{ Id: bson.NewObjectId(), Name: "JK_CHENG", PassWord: "123132", Age: 2, }, &User{ Id: bson.NewObjectId(), Name: "JK_WEI", PassWord: "qwer", Age: 5, }, &User{ Id: bson.NewObjectId(), Name: "JK_HE", PassWord: "6666", Age: 7, })*/ var users []User c.Find(nil).All(&users) //查詢全部資料 log.Println(users) c.FindId(users[0].Id).All(&users) //通過ID查詢 log.Println(users) c.Find(bson.M{"name": "JK_WEI"}).All(&users) //單條件查詢(=) log.Println(users) c.Find(bson.M{"name": bson.M{"$ne": "JK_WEI"}}).All(&users) //單條件查詢(!=) log.Println(users) c.Find(bson.M{"age": bson.M{"$gt": 5}}).All(&users) //單條件查詢(>) log.Println(users) c.Find(bson.M{"age": bson.M{"$gte": 5}}).All(&users) //單條件查詢(>=) log.Println(users) c.Find(bson.M{"age": bson.M{"$lt": 5}}).All(&users) //單條件查詢(<) log.Println(users) c.Find(bson.M{"age": bson.M{"$lte": 5}}).All(&users) //單條件查詢(<=) log.Println(users) /*c.Find(bson.M{"name": bson.M{"$in": []string{"JK_WEI", "JK_HE"}}}).All(&users) //單條件查詢(in) log.Println(users) c.Find(bson.M{"$or": []bson.M{bson.M{"name": "JK_WEI"}, bson.M{"age": 7}}}).All(&users) //多條件查詢(or) log.Println(users) c.Update(bson.M{"_id": users[0].Id}, bson.M{"$set": bson.M{"name": "JK_HOWIE", "age": 61}}) //修改欄位的值($set) c.FindId(users[0].Id).All(&users) log.Println(users) c.Find(bson.M{"name": "JK_CHENG", "age": 66}).All(&users) //多條件查詢(and) log.Println(users) c.Update(bson.M{"_id": users[0].Id}, bson.M{"$inc": bson.M{"age": -6,}}) //欄位增加值($inc) c.FindId(users[0].Id).All(&users) log.Println(users)*/ //c.Update(bson.M{"_id": users[0].Id}, bson.M{"$push": bson.M{"interests": "PHP"}}) //從陣列中增加一個元素($push) c.Update(bson.M{"_id": users[0].Id}, bson.M{"$pull": bson.M{"interests": "PHP"}}) //從陣列中刪除一個元素($pull) c.FindId(users[0].Id).All(&users) log.Println(users) c.Remove(bson.M{"name": "JK_CHENG"})//刪除 }
簡單介紹2
以下所有例子中結構定義如下: type User struct { Id_ bson.ObjectId `bson:"_id"` Name string `bson:"name"` Age int `bson:"age"` JoinedAt time.Time `bson:"joined_at"` Interests []string `bson:"interests" } 1、查詢 通過func (c *Collection) Find(query interface{}) *Query來進行查詢,返回的Query struct可以有附加各種條件來進行過濾。 通過Query.All()可以獲得所有結果,通過Query.One()可以獲得一個結果,注意如果沒有資料或者數量超過一個,One()會報錯。 條件用bson.M{key: value},注意key必須用MongoDB中的欄位名,而不是struct的欄位名。 1.1、查詢所有 var users []User c.Find(nil).All(&users) 上面程式碼可以把所有Users都查出來: 1.2、根據ObjectId查詢 id := "5204af979955496907000001" objectId := bson.ObjectIdHex(id) user := new(User) c.Find(bson.M{"_id": objectId}).One(&user) 更簡單的方式是直接用FindId()方法: c.FindId(objectId).One(&user) 1.3、單條件查詢 =($eq) c.Find(bson.M{"name": "Jimmy Kuu"}).All(&users) !=($ne) c.Find(bson.M{"name": bson.M{"$ne": "Jimmy Kuu"}}).All(&users) >($gt) c.Find(bson.M{"age": bson.M{"$gt": 32}}).All(&users) <($lt) c.Find(bson.M{"age": bson.M{"$lt": 32}}).All(&users) >=($gte) c.Find(bson.M{"age": bson.M{"$gte": 33}}).All(&users) <=($lte) c.Find(bson.M{"age": bson.M{"$lte": 31}}).All(&users) in($in) c.Find(bson.M{"name": bson.M{"$in": []string{"Jimmy Kuu", "Tracy Yu"}}}).All(&users) no in($nin) 同$in, 是否包含這個鍵($exists) c.Find(bson.M{"name": bson.M{"$exists": true}}).All(&users) 查詢鍵值為null的欄位 c.Find(bson.M{"name": bson.M{"$in":[]interface{}{null}, "$exists": true}}).All(&users) 模糊查詢($regex) c.Find(bson.M{"name": bson.M{"$regex": "^[0-9]+"}}).All(&users) $size 查詢鍵值為長度是size的陣列 c.Find(bson.M{"Interests": bson.M{"$size": 3}}).All(&users) 上面就是查詢Interests陣列長度為3的所有人 $all 查詢陣列中包含所有值得匹配(不分順序,只看包含與否) c.Find(bson.M{"Interests": bson.M{"$all": []string{"11","22","33"}}}).All(&users) 上面就是查詢Interests中包含11,22,33的所有人 key.index 如果要查詢陣列指定位置 c.Find(bson.M{"Interests.2": "33"}).All(&users) 以上就是查詢Interests的第二個元素為"33"的所有人 1.4、多條件查詢 and($and) c.Find(bson.M{"name": "Jimmy Kuu", "age": 33}).All(&users) or($or) c.Find(bson.M{"$or": []bson.M{bson.M{"name": "Jimmy Kuu"}, bson.M{"age": 31}}}).All(&users) 1.5 and 和or 同時使用 (from_account==‘123’ or to_account=‘123’) and(timestamp < '查詢時間') weher1:=bson.M{"$or":[]bson.M{bson.M{"from_account": 123}, bson.M{"to_account": 123}}} weher2:=bson.M{"timestamp":bson.M{"$lt":'查詢時間'}} where_contanct :=bson.M{"$and":[]bson.M{weher1,weher2}} c.Find(where_contanct ).All(&users) 2、修改 通過func (*Collection) Update來進行修改操作。 func (c *Collection) Update(selector interface{}, change interface{}) error 注意修改單個或多個欄位需要通過$set操作符號,否則集合會被替換。 2.1、($set) 修改欄位的值 c.Update( bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")}, bson.M{"$set": bson.M{ "name": "Jimmy Gu", "age": 34, }} ) 2.2、inc($inc) 欄位增加值 c.Update( bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")}, bson.M{"$inc": bson.M{ "age": -1, }} ) 2.3、push($push) 從陣列中增加一個元素 c.Update( bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")}, bson.M{"$push": bson.M{ "interests": "Golang", }} ) 2.4、pull($pull) 從陣列中刪除一個元素 c.Update( bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")}, bson.M{"$pull": bson.M{ "interests": "Golang", }} ) 2.5、刪除 c.Remove(bson.M{"name": "Jimmy Kuu"}) 3.對資料進行排序 使用Sort函式 func (q *Query) Sort(fields ...string) *Query var users []User // 按照age欄位降序排列,如果升序去掉橫線"-"就可以了 err := c.Find(nil).Sort("-age").All(&users) if err != nil { panic(err) } 4.分頁查詢 使用Skip函式和Limit函式 func (q *Query) Skip(n int) *Query func (q *Query) Limit(n int) *Query var users []User // 表示從偏移位置為2的地方開始取兩條記錄 err := c.Find(nil).Sort("-age").Skip(2).Limit(2).All(&users) if err != nil { panic(err) }