func remove(DB string, C string, args string, w http.ResponseWriter) { if args == "" { out := "{\"nRemoved\":0}" w.Write([]byte(out)) } else { 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 } removeinfo, err := c.RemoveAll(filter) if err != nil { w.Write([]byte("remove失败")) return } out := "{\"nRemove\":" + Object.Tostring(removeinfo.Removed) + "}" w.Write([]byte(out)) } }
func insertmany(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) /*args [ {"name":"lipeng"},{"name":"test"} ] */ var inserterlist []interface{} var ds = "[" + args + "]" log.Print(ds) err = json.Unmarshal([]byte(ds), &inserterlist) if err != nil { w.Write([]byte("无法 序列化为 []json")) return } err = c.Insert(inserterlist...) if err != nil { w.Write([]byte("insertmany失败")) return } out := "{\"nInsert\":" + Object.Tostring(len(inserterlist)) + "}" w.Write([]byte(out)) }
func getmongo() string { if mongodb == "" { conbs, err := File.ReadAllBytes(root + "/config.json") if err != nil { log.Print("无法读取配置文件") panic("无法读取配置文件") } else { var config JSON.JSON = JSON.JSON{} json.Unmarshal(conbs, &config) log.Print(config) mongodb = Object.Tostring(config["mongodb"]) port = Object.Tostring(config["port"]) return mongodb } } else { log.Print(mongodb) return mongodb } }
func count(DB string, C string, w http.ResponseWriter) { s, err := MgoSession() if err != nil { w.Write([]byte(err.Error())) return } c := s.DB(DB).C(C) count_, _ := c.Count() out := Object.Tostring(count_) 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 update(DB string, C string, args string, w http.ResponseWriter) { if args == "" { out := "{\"nUpdate\":0}" w.Write([]byte(out)) } else { /*args [ {"name":"lipeng"},{"name":"test"} ] */ var updatearg []JSON.JSON err := json.Unmarshal([]byte("["+args+"]"), &updatearg) if err != nil || len(updatearg) != 2 { w.Write([]byte("条件无法序列化为 2个json")) return } s, err := MgoSession() if err != nil { w.Write([]byte(err.Error())) return } c := s.DB(DB).C(C) log.Print(updatearg[0], updatearg[1]) updateinfo, err := c.UpdateAll(updatearg[0], updatearg[1]) if err != nil { w.Write([]byte("更新失败")) return } out := "{\"nUpdate\":" + Object.Tostring(updateinfo.Updated) + "}" w.Write([]byte(out)) } }
func save(DB string, C string, args string, w http.ResponseWriter) { if args == "" { out := "{\"nInsert\":0,\"nUpdate\":0}" w.Write([]byte(out)) } else { s, err := MgoSession() if err != nil { w.Write([]byte(err.Error())) return } c := s.DB(DB).C(C) var saver bson.M err = json.Unmarshal([]byte(args), &saver) out := "" if err != nil { w.Write([]byte("条件无法 序列化为 json")) return } if saver["_id"] == nil { err = c.Insert(saver) if err != nil { w.Write([]byte("插入失败")) return } out = "{\"nInsert\":1}" } else { if bson.IsObjectIdHex(Object.Tostring(saver["_id"])) == false { w.Write([]byte("saver 中 _id 不正确")) return } else { filter := bson.M{"_id": bson.ObjectIdHex(Object.Tostring(saver["_id"]))} count, _ := c.Find(filter).Count() saver["_id"] = bson.ObjectIdHex(Object.Tostring(saver["_id"])) if count >= 1 { rinfo, err := c.RemoveAll(filter) if err != nil { w.Write([]byte("删除失败" + JSON.ToJsonstring(rinfo))) return } err = c.Insert(saver) if err != nil { w.Write([]byte("插入失败")) return } out = "{\"nUpdate\":1}" } else { err = c.Insert(saver) if err != nil { w.Write([]byte("插入失败")) return } out = "{\"nInsert\":1}" } } } w.Write([]byte(out)) } }