This repository was archived by the owner on Mar 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcollection_find.go
More file actions
86 lines (77 loc) · 1.71 KB
/
collection_find.go
File metadata and controls
86 lines (77 loc) · 1.71 KB
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
package httpmongo
import (
"encoding/json"
"net/http"
"strings"
"github.com/golangframework/JSON"
"github.com/golangframework/Object"
)
func find(DB string, C string, args string, w http.ResponseWriter) {
s, err := MgoSession()
if err != nil {
w.Write([]byte(err.Error()))
return
}
c := s.DB(DB).C(C)
js := []JSON.JSON{} //结果集合
var filter JSON.JSON
if args == "" {
args = "{}"
}
err = json.Unmarshal([]byte(args), &filter)
if err != nil {
w.Write([]byte("find 条件,无法 序列化为 json"))
return
}
err = c.Find(&filter).All(&js)
if err != nil {
w.Write([]byte("find失败"))
return
}
jsonlist := JSON.ToJsonstringArray(js) //结果json字符串集合
out := "[" + strings.Join(jsonlist, ",") + "]"
w.Write([]byte(out))
}
func findcount(DB string, C string, args string, w http.ResponseWriter) {
s, err := MgoSession()
if err != nil {
w.Write([]byte(err.Error()))
return
}
c := s.DB(DB).C(C)
var filter JSON.JSON
err = json.Unmarshal([]byte(args), &filter)
if err != nil {
w.Write([]byte("无法 序列化为 json"))
return
}
count_, err := c.Find(&filter).Count()
if err != nil {
w.Write([]byte("find.count失败"))
return
}
out := Object.Tostring(count_)
w.Write([]byte(out))
}
func findOne(DB string, C string, args string, w http.ResponseWriter) {
s, err := MgoSession()
if err != nil {
w.Write([]byte(err.Error()))
return
}
c := s.DB(DB).C(C)
js := JSON.JSON{} //结果
var filter JSON.JSON
err = json.Unmarshal([]byte(args), &filter)
if err != nil {
w.Write([]byte("无法 序列化为 json"))
return
}
err = c.Find(&filter).One(&js)
if err != nil {
w.Write([]byte("findOne失败"))
return
}
out := js.ToJsonstring()
w.Write([]byte(out))
}