func UserGroupCount(query interface{}) (result int) { session, col := db.GetCol("UserGroup") defer session.Close() result, _ = col.Find(query).Count() return }
func UserGroupRemoveByID(id string) (result *UserGroup, err error) { session, col := db.GetCol("UserGroup") defer session.Close() if !bson.IsObjectIdHex(id) { err = mgo.ErrNotFound return } err = col.RemoveId(bson.ObjectIdHex(id)) return }
func UserGroupFindAll(query interface{}, sortFields ...string) (result []*UserGroup, err error) { session, col := db.GetCol("UserGroup") defer session.Close() q := col.Find(query) usergroupSort(q, sortFields) err = q.All(&result) return }
func UserFindByID(id string) (result *User, err error) { session, col := db.GetCol("User") defer session.Close() if !bson.IsObjectIdHex(id) { err = mgo.ErrNotFound return } err = col.FindId(bson.ObjectIdHex(id)).One(&result) return }
func UserFind(query interface{}, limit int, offset int, sortFields ...string) (result []*User, err error) { session, col := db.GetCol("User") defer session.Close() q := col.Find(query).Limit(limit).Skip(offset) userSort(q, sortFields) err = q.All(&result) return }
func UserFindOne(query interface{}, sortFields ...string) (result *User, err error) { session, col := db.GetCol("User") defer session.Close() q := col.Find(query) userSort(q, sortFields) err = q.One(&result) return }
func UserGroupFindByIDs(id []string) (result []*UserGroup, err error) { session, col := db.GetCol("UserGroup") defer session.Close() ids := make([]bson.ObjectId, 0, len(id)) for _, i := range id { if bson.IsObjectIdHex(i) { ids = append(ids, bson.ObjectIdHex(i)) } } err = col.Find(db.M{"_id": db.M{"$in": ids}}).All(&result) return }
func initUserIndex() { session, collection := db.GetCol(UserTableName) defer session.Close() reEnsureUserName: if err := collection.EnsureIndex(mgo.Index{ Key: []string{"UserName"}, Unique: true, Sparse: true, }); err != nil { println("error ensureIndex User UserName", err.Error()) err = collection.DropIndex("UserName") if err != nil { panic(err) } goto reEnsureUserName } }
func UserGroupRemoveAll(query interface{}) (info *mgo.ChangeInfo, err error) { session, col := db.GetCol("UserGroup") defer session.Close() return col.RemoveAll(query) }
func UserGroupCol() (session *mgo.Session, col *mgo.Collection) { return db.GetCol(UserGroupTableName) }