mongodb 单个document最大限制为16M。
验证代码如下
调整 sizee大小为16M,ReplaceOne返回错误 “an inserted document is too large”
type Stru struct {
Key int
Data []byte
}
func main(){
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb URI写这里"))
if err != nil {
return
}
err = client.Ping(ctx, nil)
if err != nil {
return
}
c := client.Database("database name")
data := []byte{}
sizee := 1 // 1M
for i := 0; i < sizee*1024*1024; i++ {
data = append(data, byte('a'))
}
opts := options.Replace().SetUpsert(true)
for i := 0; i < 20; i++ {
filter := bson.D{{Key: "id", Value: i}}
valueBinary, err := bson.Marshal(&Stru {Key: i, Data: data})
if err != nil {
fmt.Println(i, err)
return
}
_, err = c.Collection("sizeTest").ReplaceOne(ctx, filter, valueBinary, opts)
if err != nil {
fmt.Println(i, err)
return
}
}
}