mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-03 04:07:39 +08:00
优化mongo模块
This commit is contained in:
@@ -70,18 +70,17 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
//indexKeys[索引][每个索引key字段]
|
//indexKeys[索引][每个索引key字段]
|
||||||
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool) error {
|
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error {
|
||||||
return s.ensureIndex(db, collection, indexKeys, bBackground, false)
|
return s.ensureIndex(db, collection, indexKeys, bBackground, false,sparse)
|
||||||
}
|
}
|
||||||
|
|
||||||
//indexKeys[索引][每个索引key字段]
|
//indexKeys[索引][每个索引key字段]
|
||||||
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool) error {
|
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error {
|
||||||
return s.ensureIndex(db, collection, indexKeys, bBackground, true)
|
return s.ensureIndex(db, collection, indexKeys, bBackground, true,sparse)
|
||||||
}
|
}
|
||||||
|
|
||||||
//keys[索引][每个索引key字段]
|
//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
|
var indexes []mongo.IndexModel
|
||||||
for _, keys := range indexKeys {
|
for _, keys := range indexKeys {
|
||||||
keysDoc := bsonx.Doc{}
|
keysDoc := bsonx.Doc{}
|
||||||
@@ -89,10 +88,11 @@ func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string
|
|||||||
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
indexes = append(indexes, mongo.IndexModel{
|
options:= options.Index().SetUnique(unique).SetBackground(bBackground)
|
||||||
Keys: keysDoc,
|
if sparse == true {
|
||||||
Options: options.Index().SetUnique(unique).SetBackground(bBackground),
|
options.SetSparse(true)
|
||||||
})
|
}
|
||||||
|
indexes = append(indexes, mongo.IndexModel{Keys: keysDoc, Options:options })
|
||||||
}
|
}
|
||||||
|
|
||||||
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut)
|
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut)
|
||||||
|
|||||||
Reference in New Issue
Block a user