For example, I used Gin and linked to my local Mongodb database via ‘go.mongodb.org/mongo-driver/mongo’. Then I executed the query as follows
// end-restaurant-struct
type UrlNode struct {
Index int
Url string
}
func main() {
//now := time.Now()
Client, err := DB.MongodbPool()
//since := time.Since(now)
//log.Println("拿连接时间:", since)
if err != nil {
log.Fatal(err)
}
Collection := Client.Collection("img")
//index, err := indexView.CreateOne(ctx, indexModel)
//var temp common.UrlNode
var temp UrlNode
project := bson.D{{"url", 1}}
opts := options.FindOne().SetProjection(project)
err = Collection.FindOne(context2.TODO(), bson.M{"index": 2}, opts).Decode(&temp)
//since = time.Since(now)
//log.Println("拿222时间:", since)
fmt.Println(temp)
}
The DB.MongodbPool I used here is a link pool I built.
I did just that, but it took over 600 milliseconds to retrieve one piece of data!
I have about 200 documents in my img collection. So I want to ask, is there a way to optimize this query, I want to optimize it to 50-100 milliseconds.