From 4d5d45d555b99e14b60d674269acf1a816b066bd Mon Sep 17 00:00:00 2001 From: orgin Date: Fri, 1 Apr 2022 14:06:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96mongo=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sysmodule/mongodbmodule/mongodbmodule.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/sysmodule/mongodbmodule/mongodbmodule.go b/sysmodule/mongodbmodule/mongodbmodule.go index 2c4ffa3..12e4944 100644 --- a/sysmodule/mongodbmodule/mongodbmodule.go +++ b/sysmodule/mongodbmodule/mongodbmodule.go @@ -70,18 +70,17 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er } //indexKeys[索引][每个索引key字段] -func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool) error { - return s.ensureIndex(db, collection, indexKeys, bBackground, false) +func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { + return s.ensureIndex(db, collection, indexKeys, bBackground, false,sparse) } //indexKeys[索引][每个索引key字段] -func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool) error { - return s.ensureIndex(db, collection, indexKeys, bBackground, true) +func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { + return s.ensureIndex(db, collection, indexKeys, bBackground, true,sparse) } //keys[索引][每个索引key字段] -func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool) error { - +func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool,sparse bool) error { var indexes []mongo.IndexModel for _, keys := range indexKeys { keysDoc := bsonx.Doc{} @@ -89,10 +88,11 @@ func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string keysDoc = keysDoc.Append(key, bsonx.Int32(1)) } - indexes = append(indexes, mongo.IndexModel{ - Keys: keysDoc, - Options: options.Index().SetUnique(unique).SetBackground(bBackground), - }) + options:= options.Index().SetUnique(unique).SetBackground(bBackground) + if sparse == true { + options.SetSparse(true) + } + indexes = append(indexes, mongo.IndexModel{Keys: keysDoc, Options:options }) } ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut)