示例#1
2
func GetRestaurantById(id string) (*Restaurant, error) {
	s := db.NewMongoSession()
	defer s.Close()

	c := s.DB(db.DbName()).C(collection)
	var r Restaurant
	err := c.FindId(bson.ObjectIdHex(id)).One(&r)
	return &r, err
}
示例#2
0
func GetRestaurantByKey(key string) (*Restaurant, error) {
	s := db.NewMongoSession()
	defer s.Close()

	c := s.DB(db.DbName()).C(collection)
	var r Restaurant
	err := c.Find(bson.M{"key": key}).One(&r)
	return &r, err
}
示例#3
0
文件: user.go 项目: kranthic/justily
func GetUserByOAuthId(provider, id string) (*User, error) {
	s := db.NewMongoSession()
	defer s.Close()

	u := &User{}
	c := s.DB(db.DbName()).C(collection)
	err := c.Find(bson.M{"oap": provider, "uid": id}).One(u)

	return u, err
}
示例#4
0
文件: user.go 项目: kranthic/justily
func (user *User) Save() error {
	s := db.NewMongoSession()
	defer s.Close()

	var ins bool
	if user.Id == "" {
		user.Id = bson.NewObjectId()
		ins = true
	}
	c := s.DB(db.DbName()).C(collection)
	c.EnsureIndex(mgo.Index{Key: []string{"oap", "uid"}, Unique: true})
	if ins {
		return c.Insert(&user)
	} else {
		return c.UpdateId(user.Id, &user)
	}
}
示例#5
0
func (this *Restaurant) Save() error {
	s := db.NewMongoSession()
	defer s.Close()

	var ins bool
	now := time.Now()
	if this.Id == "" {
		this.Id = bson.NewObjectId()
		this.Key = bson.NewObjectId().Hex()
		this.CreateTime = now
		ins = true
	}
	this.UpdateTime = now
	c := s.DB(db.DbName()).C(collection)
	c.EnsureIndex(mgo.Index{Key: []string{"key"}, Unique: true})

	if ins {
		return c.Insert(&this)
	} else {
		return c.UpdateId(this.Id, &this)
	}
}
示例#6
0
func getCollection(s *mgo.Session) *mgo.Collection {
	return s.DB(db.DbName()).C(collName)
}