mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-08 01:34:41 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63199bf862 | ||
|
|
4d5d45d555 |
@@ -65,23 +65,25 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er
|
||||
|
||||
ctxTimeout, cancel := s.GetDefaultContext()
|
||||
defer cancel()
|
||||
err := s.Client.Database(db).Collection(collection).FindOneAndUpdate(ctxTimeout, bson.M{"_id": id}, bson.M{"$inc": bson.M{"Seq": 1}}).Decode(&res)
|
||||
|
||||
after := options.After
|
||||
updateOpts := options.FindOneAndUpdateOptions{ReturnDocument: &after}
|
||||
err := s.Client.Database(db).Collection(collection).FindOneAndUpdate(ctxTimeout, bson.M{"_id": id}, bson.M{"$inc": bson.M{"Seq": 1}},&updateOpts).Decode(&res)
|
||||
return res.Seq, err
|
||||
}
|
||||
|
||||
//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 +91,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)
|
||||
|
||||
Reference in New Issue
Block a user