- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在为 Go 测试新的官方 MongoDB 驱动程序,我注意到第一次调用 collection.InsertOne 总是花费大量时间,而所有后续调用都非常快。为什么?以及如何避免这种破坏性行为?
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Trainer struct {
Name string
Age int
City string
}
func main() {
t1 := time.Now()
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
log.Println("Setting client options took", time.Now().Sub(t1))
t1 = time.Now()
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
log.Println("Connecting took", time.Now().Sub(t1))
t1 = time.Now()
// Some dummy data to add to the Database
ash := Trainer{"Ash", 30, "Pallet Town"}
// Get a handle for your collection
collection := client.Database("test").Collection("trainers")
// Insert a single document
log.Println("Getting the collection took", time.Now().Sub(t1))
t1 = time.Now()
for i := 0; i < 10; i++ {
_, err := collection.InsertOne(context.TODO(), ash)
if err != nil {
log.Fatal(err)
}
log.Println("Inserting document took", time.Now().Sub(t1))
t1 = time.Now()
}
err = client.Disconnect(context.TODO())
}
我预计所有插入操作都需要几毫秒或纳秒,而第一个大约需要 0.6 秒。这是带时间的日志:
2019/07/31 17:41:39 Setting client options took 0s
2019/07/31 17:41:39 Connecting took 0s
2019/07/31 17:41:39 Getting the collection took 0s
2019/07/31 17:41:40 Inserting document took 606.0339ms
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 875.2µs
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
2019/07/31 17:41:40 Inserting document took 0s
最佳答案
只是为了结束评论中的问题:
MongoDB 驱动程序使用 lazy connection .查看MongoDB docs ,具体来说:
Calling Connect does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to, use the Ping method:
ctx, _ = context.WithTimeout(context.Background(), 2*time.Second)
err = client.Ping(ctx, readpref.Primary())
这将强制连接并从您的第一个插入中删除插入延迟。
关于mongodb - 为什么 collection.InsertOne 在插入第一个元素时这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57294734/
我想先说一下,我已经阅读了这里的几篇关于这个问题的帖子。 我有一个 node/express/mongo 应用,包含以下内容: 应用程序.js: var express = require('
我正在将文档插入到名为 users 的集合中。插入后,我想返回文档 ID 以便继续使用它。所以我写了这段代码: this.db.collection("users") .insertOne({ x
我正在为 Go 测试新的官方 MongoDB 驱动程序,我注意到第一次调用 collection.InsertOne 总是花费大量时间,而所有后续调用都非常快。为什么?以及如何避免这种破坏性行为? p
我需要在 MongoDb 中插入/更新的所有文档都具有自动更新 currentDate 因此,假设我有以下 Json shipment 对象(我从第 3 方 Restful API 获取): Stri
我收到错误 duplicate key error index: my.own.$_id_ dup key: { : ObjectId('57d2c4857c137b20e40c633f') 这个 O
最初的问题已经解决,谢谢! 现在,每当我插入文档时,它都会像这样出现: { _id: "5e3b64b6655fc51454548421", todos: [ ], title: "title" }
我正在尝试将我的旧版应用程序从 Java 驱动程序 2.10.1 升级到 3.0.0所以将insert方法改为insertOne。但是 DBCollection.insert() 返回了结果,我可以在
MongoDB 上的 insert()、insertOne() 和 insertMany() 方法有什么区别。我应该在什么情况下使用它们? 我阅读了文档,但不清楚何时使用每个文档。 最佳答案 What
我在 bulkWrite 中有 insertOne 和 updateOne 如下: async function bulkWrite(users) { await users.bulkWrite(
我有一个应用程序正在尝试从旧版驱动程序更新到新版驱动程序。当触发以下代码时,我遇到了集合被损坏的问题。我想我已经缩小了范围。 function update($collection,$crit
我有以下模型: Public class UserInfo{ public string Id{get;} public Dictionary Metadata{ get; } } 当我尝试向
我正在用node学习mongodb,我正在玩下面的代码 var assert = require('assert') var url = 'mongodb://localhost:27017/lear
我有以下结构 type Account struct { ID primitive.ObjectID `json:"id" bson:"_id"` Email str
我在 node express 上运行 MongoDB Atlas,当我使用 postman 进行测试时出现此错误。 const express = require('express'); const
我正在为 Ionic 前端开发 LoopBack 后端。它使用 MongoDB 作为数据库。 在 package.json 中,我尝试将依赖项 loopback-connector-mongodb 从
我正忙于创建一个项目,允许用户输入“电子邮件”和“密码”来注册网站, 当我尝试使用“电子邮件”和“密码”输入用户进入该站点时,出现以下错误: MongooseError: Operation user
在我看到的文档中,我们可以使用返回的 insertOneWriteOpResultObject.ops 来检索插入的文档。 ( Docs ),但这样做时我会回来 undefined . 复制代码: c
我目前正在使用 MongoDB 和 Express 创建一个新的 API,并且我目前遇到了这个问题“操作 disneys.insertOne() 缓冲在 10000 毫秒后超时。”我目前正在使用 ro
我以为我可以阅读这个解决方案,但我看不出我做错了什么。 这是我的模型: var mongoose = require('mongoose'); var Schema = mon
所以我正在创建一个用户对象并使用 async/await 将其插入到我的 mongo 数据库中。 像这样: await db.collection('users').insertOne({
我是一名优秀的程序员,十分优秀!