我试图在 mongodb 中存储一个类型 4 的 uuid,但是当我调用 InsertOne 函数时,这会将我的 uuid 存储为类型 0(通用)而不是类型 4。 我不知道我到底做错了什么。
我的代码:
type Document struct {
ID uuid.UUID `bson:"_id" json:"id"`
FcrID string `bson:"fcrId" json:"fcrId"`
ContextKey string `bson:"contextKey" json:"contextKey"`
PrivateKey string `bson:"privateKey" json:"privateKey"`
PublicKey string `bson:"publicKey" json:"publicKey"`
}
for _, message := range messages {
body := message.Body
fmt.Println("new messaged received, writing keys to database")
id := uuid.New()
var jsonDocument common.DocumentKey
err = json.Unmarshal(body, &jsonDocument)
if err != nil {
fmt.Println(err)
return
}
d, bsonErr := bson.Marshal(common.Document{
ID: id,
FcrID: jsonDocument.FcrID,
ContextKey: jsonDocument.ContextKey,
PrivateKey: jsonDocument.PrivateKey,
PublicKey: jsonDocument.PublicKey,
})
if bsonErr != nil {
fmt.Println(bsonErr)
return
}
err = databaseOperations.InsertData(ctx, mclient, "management", "keys", d)
if err != nil {
fmt.Println(err)
return
}
err = receiver.CompleteMessage(context.TODO(), message, nil)
if err != nil {
log.Println(err)
}
}
但是 id 被存储为 BinData 0 但我想要 BinData 4.
我知道 uuid 将返回一个 [16] 字节,但我不明白为什么 mongo 将其理解为要存储的通用数据而不是正确的 uuid 类型 0x04。
如何让 mongodb 将我的 uuid 存储为类型 4 而不是 0?
谢谢
成功了,非常简单
type Document struct {
ID primitive.Binary `bson:"_id" json:"id"`
FcrID string `bson:"fcrId" json:"fcrId"`
ContextKey string `bson:"contextKey" json:"contextKey"`
PrivateKey string `bson:"privateKey" json:"privateKey"`
PublicKey string `bson:"publicKey" json:"publicKey"`
}
id := uuid.New()
binId := primitive.Binary{
Subtype: 0x04,
Data: id[:],
}
如果您想使用 Golang 在 MongoDB 中将 UUID 存储为类型 0x04,则需要确保 UUID 在插入数据库之前被正确序列化为二进制子类型
0x04
。
在这里,我可以给你一个示例代码片段,展示如何生成一个 UUID 类型 0x04
并将其存储在 MongoDB 中:
示例代码
import (
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Defined my document struct
type Document struct {
ID uuid.UUID `bson:"_id" json:"id"`
FcrID string `bson:"fcrId" json:"fcrId"`
ContextKey string `bson:"contextKey" json:"contextKey"`
PrivateKey string `bson:"privateKey" json:"privateKey"`
PublicKey string `bson:"publicKey" json:"publicKey"`
}
func main() {
// then, Create a new UUID of type 0x04
id := uuid.New()
// Serialize the UUID as a binary subtype 0x04
idBytes := id.Bytes()
idBytes[6] = (idBytes[6] & 0x0f) | 0x40 // Set the version byte to 4
idBytes[8] = (idBytes[8] & 0x3f) | 0x80 // Set the variant byte to the RFC 4122 variant
idBinary := bson.Binary{
Subtype: 0x04,
Data: idBytes,
}
fmt.Println("🚀 idBinary : ", idBinary)
// now, Create a new document instance
doc := &Document{
ID: uuid.Nil, // this, ID will be automatically generated by MongoDB
FcrID: "my-fcr-id",
ContextKey: "my-context-key",
PrivateKey: "my-private-key",
PublicKey: "my-public-key",
}
fmt.Println("🚀 doc : ", doc)
// Serialize the document to BSON
bsonDoc, err := bson.Marshal(doc)
if err != nil {
fmt.Println("🚀 ~ marshal : ~ err : ", err)
}
// Set the ID field to the binary UUID value
bsonDoc[4] = 0x04 // Set the BSON type for the ID field to binary
bsonDoc = append(bsonDoc[:5], idBinary.Bytes()...)
bsonDoc = append(bsonDoc, 0x00) // Add a null terminator to the BSON document
// Insert the document into the MongoDB collection using insertOne() command
collection := mclient.Database("MyDB").Collection("MyCollection")
_, err = collection.InsertOne(ctx, bsonDoc)
if err != nil {
fmt.Println("🚀 ~ insertOne(): ~ err : ", err)
}
}
您首先使用 uuid.New()
函数生成一个新的
UUID。然后,通过将版本字节设置为 4 并将变体字节设置为 RFC 4122 变体,将
UUID序列化为二进制子类型
0x04
。
您创建一个新的文档实例并使用 bson.Marshal()
函数将其序列化为 BSON。最后,您将 ID 字段设置为二进制 UUID 值,并使用
collection.InsertOne()
函数将文档插入 MongoDB 集合中。