Golang操作MongoDB(2) DAO - 增删改查
package dao import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "golang_demo/mongo/db" "log" "time" ) // 数据结构体 type Student struct { Id int32 Name string Age int } var ( opt = "mongodb://root:ro:27018" // 带账号名的链接 name = "user1" // 数据库名 maxTime = time.Duration(2) // 链接超时时间 num uint64 = 50 // 链接数 table = "student" // 表名 toDB *mongo.Database // database 话柄 collection *mongo.Collection // collection 话柄 ) func init() { var err error toDB, err = db.ConnectToDB(opt, name,maxTime,num) if err!= nil { panic("链接数据库有误!") } collection = toDB.Collection(table) } // GetList 获取全量的数据 func GetList() { cur, err := collection.Find(context.Background(), bson.D{}) if err != nil { log.Fatal(err) } if err := cur.Err(); err != nil { log.Fatal(err) } var all []*Student err = cur.All(context.Background(), &all) if err != nil { log.Fatal(err) } cur.Close(context.Background()) log.Println("collection.Find curl.All: ", all) for _, one := range all { log.Println("Id:",one.Id," - name:",one.Name," - age:",one.Age) } } // AddOne 新增一条数据 func AddOne(s1 *Student) { objId, err := collection.InsertOne(context.TODO(), &s1) if err != nil { log.Println(err) return } log.Println("录入数据成功,objId:",objId) } // EditOne 编辑一条数据 func EditOne(student *Student,m bson.M) { update := bson.M{"$set": student} updateResult, err := collection.UpdateOne(context.Background(), m, update) if err != nil { log.Fatal(err) } log.Println("collection.UpdateOne:", updateResult) } // 更新数据 - 存在更新,不存在就新增 func Update(student *Student,m bson.M) { update := bson.M{"$set": student} updateOpts := options.Update().SetUpsert(true) updateResult, err := collection.UpdateOne(context.Background(), m, update, updateOpts) if err != nil { log.Fatal(err) } log.Println("collection.UpdateOne:", updateResult) } // 删除一条数据 func Del(m bson.M) { deleteResult, err := collection.DeleteOne(context.Background(), m) if err != nil { log.Fatal(err) } log.Println("collection.DeleteOne:", deleteResult) } // Sectle 模糊查询 // bson.M{"name": primitive.Regex{Pattern: "深入"}} func Sectle(m bson.M) { cur, err := collection.Find(context.Background(), m) if err != nil { log.Fatal(err) } if err := cur.Err(); err != nil { log.Fatal(err) } for cur.Next(context.Background()) { var s Student if err = cur.Decode(&s); err != nil { log.Fatal(err) } log.Println("collection.Find name=primitive.Regex{xx}: ", s) } cur.Close(context.Background()) } // 统计collection的数据总数 func Count() { count, err := collection.CountDocuments(context.Background(), bson.D{}) if err != nil { log.Fatal(count) } log.Println("collection.CountDocuments:", count) } // 准确搜索一条数据 func GetOne(m bson.M) { var one Student err := collection.FindOne(context.Background(), m).Decode(&one) if err != nil { log.Fatal(err) } log.Println("collection.FindOne: ", one) }
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19