|
@@ -103,6 +103,9 @@ func (ms *MgoSess) Pipe(p []map[string]interface{}) *MgoSess {
|
|
|
ms.pipe = p
|
|
|
return ms
|
|
|
}
|
|
|
+func (ms *MgoSess) Count() (int64, error) {
|
|
|
+ return ms.M.C.Database(ms.db).Collection(ms.coll).CountDocuments(ms.M.Ctx, ms.query)
|
|
|
+}
|
|
|
func (ms *MgoSess) All(v *[]map[string]interface{}) {
|
|
|
cur, err := ms.M.C.Database(ms.db).Collection(ms.coll).Aggregate(ms.M.Ctx, ms.pipe)
|
|
|
if err == nil && cur.Err() == nil {
|
|
@@ -383,7 +386,7 @@ func (m *MongodbSim) UpdateById(c string, id interface{}, set interface{}) bool
|
|
|
|
|
|
//批量更新
|
|
|
func (m *MongodbSim) UpdateBulkAll(db, c string, doc ...[]map[string]interface{}) bool {
|
|
|
- return m.upSertBulk(db, c, false, doc...)
|
|
|
+ return m.NewUpdateBulk(db, c, false, false, doc...)
|
|
|
}
|
|
|
|
|
|
func (m *MongodbSim) UpdateBulk(c string, doc ...[]map[string]interface{}) bool {
|
|
@@ -392,22 +395,30 @@ func (m *MongodbSim) UpdateBulk(c string, doc ...[]map[string]interface{}) bool
|
|
|
|
|
|
//批量插入
|
|
|
func (m *MongodbSim) UpSertBulk(c string, doc ...[]map[string]interface{}) bool {
|
|
|
- return m.upSertBulk(m.DbName, c, true, doc...)
|
|
|
+ return m.NewUpdateBulk(m.DbName, c, true, false, doc...)
|
|
|
}
|
|
|
|
|
|
//批量插入
|
|
|
-func (m *MongodbSim) upSertBulk(db, c string, upsert bool, doc ...[]map[string]interface{}) bool {
|
|
|
+func (m *MongodbSim) NewUpdateBulk(db, c string, upsert, multi bool, doc ...[]map[string]interface{}) bool {
|
|
|
defer catch()
|
|
|
m.Open()
|
|
|
defer m.Close()
|
|
|
coll := m.C.Database(db).Collection(c)
|
|
|
var writes []mongo.WriteModel
|
|
|
for _, d := range doc {
|
|
|
- write := mongo.NewUpdateOneModel()
|
|
|
- write.SetFilter(d[0])
|
|
|
- write.SetUpdate(d[1])
|
|
|
- write.SetUpsert(upsert)
|
|
|
- writes = append(writes, write)
|
|
|
+ if multi {
|
|
|
+ write := mongo.NewUpdateManyModel()
|
|
|
+ write.SetFilter(d[0])
|
|
|
+ write.SetUpdate(d[1])
|
|
|
+ write.SetUpsert(upsert)
|
|
|
+ writes = append(writes, write)
|
|
|
+ } else {
|
|
|
+ write := mongo.NewUpdateOneModel()
|
|
|
+ write.SetFilter(d[0])
|
|
|
+ write.SetUpdate(d[1])
|
|
|
+ write.SetUpsert(upsert)
|
|
|
+ writes = append(writes, write)
|
|
|
+ }
|
|
|
}
|
|
|
br, e := coll.BulkWrite(m.Ctx, writes)
|
|
|
if e != nil {
|