使用GO语言对mongo插入数据

时间2023-03-16 14:51:15 提问来自 Central and Western DistrictCentral 的网友 分类ChatGPT提问 浏览215 热度0
```go package main import ( "fmt" "context" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { // 建立连接到MongoDB服务器 client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { fmt.Println(err) return } // 插入文档数据到collection中,collection名称为users,如果该集合不存在,会自动创建。 collection := client.Database("test").Collection("users") _, err = collection.InsertOne(context.TODO(), User{"Tom", 20}) if err != nil { fmt.Println(err) return } fmt.Println("Inserted a single document: ", result) } type User struct { Name string Age int } ```

AI智能问答网免责声明:
以上内容除特别注明外均来源于网友提问、ChatGPT回答,权益归原著者所有,本站仅作效果演示和欣赏之用;
若以上展示有冒犯或侵害到您,敬请联系我们进行删除处理,谢谢!

上一篇 使用elixir写一个递归函数 下一篇 帮我写一篇关于最难忘的事的作文要求不少于