2
【笔记】Go 语言操作 Mongodb 数据库
source link: https://loli.fj.cn/2023/07/01/Go%E8%AF%AD%E8%A8%80%E6%93%8D%E4%BD%9CMongodb%E6%95%B0%E6%8D%AE%E5%BA%93/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【笔记】Go 语言操作 Mongodb 数据库
2023-07-01
Go 语言操作 Mongodb 数据库
go get go.mongodb.org/mongo-driver/mongo
clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
Ping 测试
err := client.Ping(context.TODO(), nil)
defer client.Disconnect(context.TODO())
对数据库的操作
获取数据库对象
db := client.Database("数据库名")
对集合的操作
获取集合对象
collection := db.Collection("数据表名")
collection := client.Database("数据库名").Collection("数据表名")
对文档的操作
新增单个文档
新增多个文档
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK