With the advent of the big data era, NoSQL databases have become one of the focuses of attention. MongoDB is one of the best, it is a NoSQL document database. As an efficient programming language, golang is also accepted by more and more people. Therefore, querying MongoDB with golang has become a very practical skill.
Features of MongoDB
MongoDB is an open source high-availability NoSQL document database written in C language. Its characteristics are as follows:
1. High performance: MongoDB support High concurrency, can easily handle high-traffic read and write operations.
The above characteristics make MongoDB a NoSQL database worthy of respect. But for programmers who use golang language, how to use golang to query MongoDB?
Golang operates MongoDB
There are two ways to use golang to interact with MongoDB: use the mongo-go-driver officially provided by MongoDB in golang; use the third-party library mgo. This article will take the first approach.
mongo-go-driver installation
go get go.mongodb.org/mongo-driver
Import package
import "go.mongodb.org/mongo-driver/mongo"
Establish connection
func main() { // 设置客户端连接配置 clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") // 连接到MongoDB client, err := mongo.Connect(context.Background(), clientOptions) if err != nil { log.Fatalf("Could not connect to MongoDB: %v", err) } // 断开连接 defer func() { if err = client.Disconnect(context.Background()); err != nil { panic(err) } }() }
Query data
// 查询单条数据 func findOne() { collection := client.Database("test").Collection("users") filter := bson.M{"name": "John Doe"} var result bson.M if err := collection.FindOne(context.Background(), filter).Decode(&result); err != nil { log.Fatalf("Failed to find document: %v", err) } fmt.Println(result) } // 查询多条数据 func find() { collection := client.Database("test").Collection("users") filter := bson.M{"age": bson.M{"$gt": 18}} cur, err := collection.Find(context.Background(), filter) if err != nil { log.Fatalf("Failed to find documents: %v", err) } defer cur.Close(context.Background()) for cur.Next(context.Background()) { var result bson.M if err := cur.Decode(&result); err != nil { log.Fatalf("Failed to decode document: %v", err) } fmt.Println(result) } }
Insert data
func insert() { collection := client.Database("test").Collection("users") user := bson.M{"name": "Alice", "age": 20, "email": "alice@example.com"} if _, err := collection.InsertOne(context.Background(), user); err != nil { log.Fatalf("Failed to insert document: %v", err) } }
Update data
func update() { collection := client.Database("test").Collection("users") filter := bson.M{"name": "John Doe"} update := bson.M{"$set": bson.M{"age": 28}} if _, err := collection.UpdateOne(context.Background(), filter, update); err != nil { log.Fatalf("Failed to update document: %v", err) } }
Delete data
func delete() { collection := client.Database("test").Collection("users") filter := bson.M{"name": "Alice"} if _, err := collection.DeleteOne(context.Background(), filter); err != nil { log.Fatalf("Failed to delete document: %v", err) } }
The above are some basic operations for data interaction between golang and MongoDB.
Conclusion
This article introduces how to use golang to query MongoDB. It uses the Mongo official driver for Go language to process MongoDB and implements basic operations for MongoDB, such as query, insert, update and delete. wait. Through the above series of operations, it will be of great help to the data management of the database and the realization of subsequent related requirements.
The above is the detailed content of How to query MongoDB using golang. For more information, please follow other related articles on the PHP Chinese website!