gen key at collection
This commit is contained in:
parent
76b132a8b7
commit
00ec4fd001
|
@ -1,10 +1,12 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.pyer.club/kingecg/godocdb/document"
|
"git.pyer.club/kingecg/godocdb/document"
|
||||||
"git.pyer.club/kingecg/godocdb/index" // "fmt"
|
"git.pyer.club/kingecg/godocdb/index" // "fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
// "go.mongodb.org/mongo-driver/bson"
|
// "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 {
|
func (coll *Collection) InsertOne(doc interface{}) error {
|
||||||
// 自动生成文档ID
|
// 自动生成文档ID
|
||||||
// docID := generateID()
|
// 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信息传递给文档管理层和索引管理层
|
// 将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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue