2 回答
TA贡献1785条经验 获得超4个赞
这有很多错误。
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Userinfo struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
USER []string `json:"user" bson:"user"`
SALES []string `json:"sales" bson:"sales"`
DATEADDED time.Time `json:"dateAdded" bson:"dateAdded"`
NAME string `json:"name" bson:"name"`
}
type User struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
FIRSTNAME string `json:"firstName" bson:"firstName"`
LASTNAME string `json:"lastName" bson:"lastName"`
AGE int `json:"age" bson:"age"`
}
type Sales struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
FIRSTNAME string `json:"firstName" bson:"firstName"`
LASTNAME string `json:"lastName" bson:"lastName"`
AGE int `json:"age" bson:"age"`
}
var session *mgo.Session
func main() {
var err error
session, err = mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
fmt.Fprintln(w, "conn")
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/post/", post)
router.HandleFunc("/getusers/", getusers)
http.ListenAndServe(":8080", router)
}
func post(w http.ResponseWriter, r *http.Request) {
ms := session.Copy()
defer ms.Close()
cui := session.DB("userdb").C("userinfo")
cu := session.DB("userdb").C("user")
cs := session.DB("userdb").C("sales")
u := User{FIRSTNAME: "sam", LASTNAME: "billing", AGE: 25}
s := Sales{FIRSTNAME: "joe", LASTNAME: "root", AGE: 23}
if e := cu.Insert(u); e != nil {
log.Println(e.Error)
w.WriteHeader(500)
return
}
if e := cs.Insert(s); e != nil {
log.Println(e.Error)
w.WriteHeader(500)
return
}
ui := new(Userinfo)
ui.ID = bson.NewObjectId()
ui.NAME = "admin"
ui.USER = []string{u.Id.Hex()}
ui.SALES = []string{s.Id.Hex()}
ui.DATEADDED = time.Now()
if e := cui.Insert(ui); e != nil {
log.Println(e.Error)
w.WriteHeader(500)
return
}
w.WriteHeader(201)
}
func getusers(w http.ResponseWriter, r *http.Request) {
ms := session.Copy()
defer ms.Close()
cui := session.DB("userdb").C("userinfo")
cu := session.DB("userdb").C("user")
cs := session.DB("userdb").C("sales")
// Query for users of userinfo
uadm := new(Userinfo)
if e := cui.Find(bson.M{"name": "admin"}).One(uadm); e != nil {
log.Println(e.Error)
}
for _, userid := range uadm.USER {
tempu := new(User)
if e := cu.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(tempu); e != nil {
log.Println(e.Error)
w.WriteHeader(500)
return
}
enc := json.NewEncoder(w)
if e := enc.Encode(tempu); e != nil {
log.Println(e.Error)
w.WriteHeader(500)
return
}
}
}
会议
所有型号的单独集合
我已将 Userinfo 的用户和销售字段更改为 []string,因为每个可独立编辑和可查询的模型都应该有自己的集合。id 引用被保存而不是整个对象,因此您可以通过 ObjectId 查询用户。
用户信息应为大写
TA贡献1820条经验 获得超2个赞
您的语法在几个地方都关闭了。但是代码很乱,所以我会这样拆分:
users := {USERID: new ObjectId(), FIRSTNAME: "sam",LASTNAME: "billing", AGE: 25}
sales := {SALESID: new ObjectId(),FIRSTNAME: "joe", LASTNAME: "root", AGE: 23}
info := &userinfo{ID:new ObjectId(), NAME:"Admin", USER: users, SALES: sales, DATEADDED: time.Now()}
关于销售和用户的创建,这不是创建切片和结构的正确方法。Golang 中类型 T 的结构的实例化是T{}. 您可以以相同的方式创建切片。因此sales和users成为
users := []User{User{USERID: new ObjectId(), FIRSTNAME: "sam",LASTNAME: "billing", AGE: 25}}
sales := []Sales{Sales{SALESID: new ObjectId(),FIRSTNAME: "joe", LASTNAME: "root", AGE: 23}}
然后,您应该查看 mgo 的文档。创建 ObjectId 的方式是bson.NewObjectId(). 现在,进行所有修改:
func main() {
users := []User{User{USERID: bson.NewObjectId(), FIRSTNAME: "sam", LASTNAME: "billing", AGE: 25}}
sales := []Sales{Sales{SALESID: bson.NewObjectId(), FIRSTNAME: "joe", LASTNAME: "root", AGE: 23}}
info := &userinfo{ID: bson.NewObjectId(), NAME: "Admin", USER: users, SALES: sales, DATEADDED: time.Now()}
data, _ := json.MarshalIndent(info, "", " ")
fmt.Println(string(data))
}
/* Prints
{
"_id": "57402f27e13823740d742417",
"user": [
{
"userid": "57402f27e13823740d742415",
"firstName": "sam",
"lastName": "billing",
"age": 25
}
],
"sales": [
{
"salesid": "57402f27e13823740d742416",
"firstName": "joe",
"lastName": "root",
"age": 23
}
],
"dateAdded": "2016-05-21T11:49:27.507636096+02:00",
"name": "Admin"
}
*/
- 2 回答
- 0 关注
- 136 浏览
添加回答
举报