-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.go
93 lines (72 loc) · 2.64 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package module
import (
"github.com/zhufuyi/pkg/mongo"
"github.com/globalsign/mgo/bson"
)
// DemoCollectionName 在mongo表名,约定首字母小写
const DemoCollectionName = "demo"
// Demo 用户消息
type Demo struct {
mongo.PublicFields `bson:",inline"`
UserID string `bson:"userID" json:"userID"` // 用户id
FileID string `bson:"fileID" json:"fileID"` // 文件id
TaskID string `bson:"taskID" json:"taskID"` // 策略运行id,需要建立索引
PolicyName string `bson:"policyName" json:"policyName"` // 用户命名的策略名称
Message interface{} `bson:"message" json:"message"` // 信息内容
}
// Insert 插入一条新的记录
func (object *Demo) Insert() (err error) {
mconn := mongo.GetSession()
defer mconn.Close()
object.SetFieldsValue()
return mconn.Insert(DemoCollectionName, object)
}
// FindDemo 获取单条记录
func FindDemo(selector bson.M, field bson.M) (*Demo, error) {
mconn := mongo.GetSession()
defer mconn.Close()
object := &Demo{}
return object, mconn.FindOne(DemoCollectionName, object, selector, field)
}
// FindDemos 获取多条记录
func FindDemos(selector bson.M, field bson.M, page int, limit int, sort ...string) ([]*Demo, error) {
mconn := mongo.GetSession()
defer mconn.Close()
// 默认从第一页开始
if page < 1 {
page = 1
}
objects := []*Demo{}
return objects, mconn.FindAll(DemoCollectionName, &objects, selector, field, page-1, limit, sort...)
}
// UpdateDemo 更新单条记录
func UpdateDemo(selector, update bson.M) (err error) {
mconn := mongo.GetSession()
defer mconn.Close()
return mconn.UpdateOne(DemoCollectionName, selector, mongo.UpdatedTime(update))
}
// UpdateDemos 更新多条记录
func UpdateDemos(selector, update bson.M) (n int, err error) {
mconn := mongo.GetSession()
defer mconn.Close()
return mconn.UpdateAll(DemoCollectionName, selector, mongo.UpdatedTime(update))
}
// FindAndModifyDemo 更新并返回最新记录
func FindAndModifyDemo(selector bson.M, update bson.M) (*Demo, error) {
mconn := mongo.GetSession()
defer mconn.Close()
object := &Demo{}
return object, mconn.FindAndModify(DemoCollectionName, object, selector, mongo.UpdatedTime(update))
}
// CountDemos 统计数量,不包括删除记录
func CountDemos(selector bson.M) (n int, err error) {
mconn := mongo.GetSession()
defer mconn.Close()
return mconn.Count(DemoCollectionName, mongo.ExcludeDeleted(selector))
}
// DeleteDemo 删除记录
func DeleteDemo(selector bson.M) (int, error) {
mconn := mongo.GetSession()
defer mconn.Close()
return mconn.UpdateAll(DemoCollectionName, selector, mongo.DeletedTime(bson.M{}))
}