diff --git a/api/collection.go b/api/collection.go index 148cb35..c0fce80 100644 --- a/api/collection.go +++ b/api/collection.go @@ -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 }