为了连接Mongodb数据库,首先需要安装Mongodb驱动,然后就可以使用下面的代码来连接数据库了:
packagemain
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// 连接字符串
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// 连接到MongoDB
client, err := mongo