MongoDB golang驱动程序的InsertOne时间是从哪里来的?

huangapple go评论75阅读模式
英文:

Where does the MongoDB golang driver InsertOne time come from?

问题

我使用以下代码来获取MongoDB的客户端:

package connectors

import (
	"context"
	"log"
	"os"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func GetMongoDBConnection() *mongo.Client {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URI")))
	if err != nil {
		log.Fatal(err)
	}
	return client
}

我使用以下代码来获取连接并将一个文档插入到数据库中:

dbClient := connectors.GetMongoDBConnection()
coll := dbClient.Database("test").Collection("test")
coll.InsertOne(context.TODO(), bson.M{"key": "value"})

我尝试通过以下方式测量时间:

ConnectionTime := time.Now()

dbClient := connectors.GetMongoDBConnection()

fmt.Println(time.Since(ConnectionTime).Milliseconds())

CollSelectionTime := time.Now()

coll := dbClient.Database("test").Collection("test")

fmt.Println(time.Since(CollSelectionTime).Milliseconds())

InsetionTime := time.Now()

coll.InsertOne(context.TODO(), bson.M{"type": "congelada", "type2": "Masalado"})

fmt.Println(time.Since(InsetionTime).Milliseconds())

控制台输出:

0

0

629

所以629毫秒来自于"InsertOne"操作...但我不明白为什么会这样,因为我尝试插入10000个文档,只花费了大约2000毫秒,所以我实际上不明白那大约600毫秒是从哪里来的...

如何减少这个时间呢?

英文:

I use this code to get the client for mongodb:

package connectors

import (
	"context"
	"log"
	"os"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func GetMongoDBConnection() *mongo.Client {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGODB_URI")))
	if err != nil {
		log.Fatal(err)
	}
	return client

}

And im using it like this, to get the connection and insert one document into the db:

dbClient := connectors.GetMongoDBConnection()
coll := dbClient.Database("test").Collection("test")
coll.InsertOne(context.TODO(), bson.M{"key": "value"})

I tried to measure the time by doing:

ConnectionTime := time.Now()

dbClient := connectors.GetMongoDBConnection()

fmt.Println(time.Since(ConnectionTime).Milliseconds())

CollSelectionTime := time.Now()

coll := dbClient.Database("test").Collection("test")

fmt.Println(time.Since(CollSelectionTime).Milliseconds())

InsetionTime := time.Now()

coll.InsertOne(context.TODO(), bson.M{"type": "congelada", "type2": "Masalado"})

fmt.Println(time.Since(InsetionTime).Milliseconds())

The console prints:

0

0

629

So that 629 milliseconds are from the "InsertOne"... But I don't understand why because I tried to insert 10,000 documents and it took like 2000 milliseconds, so I actually don't undestand where that ~600 milliseconds came from...

How can I reduce that time...

答案1

得分: 0

连接、数据库选择或集合选择只是捕获参数,而不执行任何实际操作。它们在进行实际的数据库操作时使用。因此,对InsertOne的第一次调用将在未连接时进行连接,并执行操作。

连接后,调用Ping,这将实际连接到数据库。

英文:

Connection, DB selection, or collection selection simply capture the arguments but do not perform any actual operation. Those are used when you make the actual database operation. Thus, the first call to InsertOne will connect if not already connected, and perform the operation.

After connecting, call Ping, and that will actually connect to the DB.

huangapple
  • 本文由 发表于 2023年1月3日 09:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74988317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定