gen key at collection

This commit is contained in:
kingecg 2025-06-08 14:07:38 +08:00
parent 76b132a8b7
commit 00ec4fd001
1 changed files with 12 additions and 1 deletions

View File

@ -1,10 +1,12 @@
package api
import (
"fmt"
"sync"
"git.pyer.club/kingecg/godocdb/document"
"git.pyer.club/kingecg/godocdb/index" // "fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// "go.mongodb.org/mongo-driver/bson"
@ -44,9 +46,18 @@ func NewCollection(name string, storagePath string) (*Collection, error) {
func (coll *Collection) InsertOne(doc interface{}) error {
// 自动生成文档ID
// docID := generateID()
docMap, ok := doc.(map[string]interface{})
if !ok {
return fmt.Errorf("document must be a map[string]interface{}")
}
docID, exists := docMap["_id"].(primitive.ObjectID)
if !exists {
docID = primitive.NewObjectID()
docMap["_id"] = docID
}
// 将collection信息传递给文档管理层和索引管理层
if err := coll.documentStore.StoreDocument(coll.name, "", doc); err != nil {
if err := coll.documentStore.StoreDocument(coll.name, docID.String(), doc); err != nil {
return err
}