|
@@ -191,6 +191,11 @@ func (ms *MgoSess) RemoveId(_id interface{}) error {
|
|
|
func (ms *MgoSess) RemoveAll(filter interface{}) (*mongo.DeleteResult, error) {
|
|
|
return ms.M.C.Database(ms.db).Collection(ms.coll).DeleteMany(ms.M.Ctx, filter)
|
|
|
}
|
|
|
+func (ms *MgoSess) Upsert(filter, update interface{}) (*mongo.UpdateResult, error) {
|
|
|
+ ct := options.Update()
|
|
|
+ ct.SetUpsert(true)
|
|
|
+ return ms.M.C.Database(ms.db).Collection(ms.coll).UpdateOne(ms.M.Ctx, filter, update, ct)
|
|
|
+}
|
|
|
func (ms *MgoSess) UpsertId(filter, update interface{}) (*mongo.UpdateResult, error) {
|
|
|
ct := options.Update()
|
|
|
ct.SetUpsert(true)
|
|
@@ -587,13 +592,14 @@ func (m *MongodbSim) Find(c string, query interface{}, order interface{}, fields
|
|
|
defer catch()
|
|
|
m.Open()
|
|
|
defer m.Close()
|
|
|
- res := make([]map[string]interface{}, 1)
|
|
|
+ var res []map[string]interface{}
|
|
|
coll := m.C.Database(m.DbName).Collection(c)
|
|
|
if single {
|
|
|
of := options.FindOne()
|
|
|
of.SetProjection(ObjToOth(fields))
|
|
|
of.SetSort(ObjToM(order))
|
|
|
if sr := coll.FindOne(m.Ctx, ObjToM(query), of); sr.Err() == nil {
|
|
|
+ res = make([]map[string]interface{}, 1)
|
|
|
sr.Decode(&res[0])
|
|
|
}
|
|
|
} else {
|
|
@@ -606,6 +612,7 @@ func (m *MongodbSim) Find(c string, query interface{}, order interface{}, fields
|
|
|
}
|
|
|
cur, err := coll.Find(m.Ctx, ObjToM(query), of)
|
|
|
if err == nil && cur.Err() == nil {
|
|
|
+ res = []map[string]interface{}{}
|
|
|
cur.All(m.Ctx, &res)
|
|
|
}
|
|
|
}
|
|
@@ -718,3 +725,12 @@ func StringTOBsonId(id string) (bid primitive.ObjectID) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func ToObjectIds(ids []string) []primitive.ObjectID {
|
|
|
+ _ids := []primitive.ObjectID{}
|
|
|
+ for _, v := range ids {
|
|
|
+ _id, _ := primitive.ObjectIDFromHex(v)
|
|
|
+ _ids = append(_ids, _id)
|
|
|
+ }
|
|
|
+ return _ids
|
|
|
+}
|