我是mongodb-go-driver的新手.但我被卡住了.
cursor, e := collection.Find(context.Background(), bson.NewDocument(bson.EC.String("name", id)))
for cursor.Next(context.Background()) {
e := bson.NewDocument()
cursor.Decode(e)
b, _ := e.MarshalBSON()
err := bson.Unmarshal(b, m[id])
}
Run Code Online (Sandbox Code Playgroud)
当查看m [id]的内容时,它没有内容 - 所有空值.
我的地图是这样的:m map [string]语言
和语言定义如下:
type Language struct {
ID string `json:"id" bson:"_id"` // is this wrong?
Name string `json:"name" bson:"name"`
Vowels []string `json:"vowels" bson:"vowels"`
Consonants []string `json:"consonants" bson:"consonants"`
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在学习使用这个例子:https://github.com/mongodb/mongo-go-driver/blob/master/examples/documentation_examples/examples.go
场景 1:postgres 服务未运行。节点启动。休息呼叫超时 => 没有问题
场景 2:postgres 服务未运行。节点启动。postgres 服务启动。休息通话工作 => 没有问题
场景 3:postgres 服务未运行。节点启动。postgres 服务启动。休息叫工作。postgres 服务停止 => 节点崩溃。
发生这种情况时的堆栈跟踪如下:
错误:由于 Connection.parseE (...\express.js\node_modules\pg\lib\connection.js:553:11) 处 Connection.parseMessage (...\express.js\node_modules\) 处的管理员命令而终止连接pg\lib\connection.js:378:19) 在套接字处。(...\express.js\node_modules\pg\lib\connection.js:119:22) 在emitOne (events.js:116:13) 在Socket.emit (events.js:211:7) 在addChunk ( _stream_readable.js:263:12) 在 readAddChunk (_stream_readable.js:250:11) 在 Socket.Readable.push (_stream_readable.js:208:10) 在 TCP.onread (net.js:594:20)
我使用具有以下配置的 Postgres 池:
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'sampleDB',
password: 'password',
port: 5432,
max: 1,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
})
Run Code Online (Sandbox Code Playgroud)
如何处理这种情况,以便节点不会崩溃并可以返回到 Scenario-1 然后移至 Scenario-2 ?