Beispiel #1
0
func GetAccount(query interface{}) (account *model.Account, err error) {
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return nil, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	defer session.Close()
	account = new(model.Account)
	err = collection.Find(query).One(&account)
	return account, err
}
Beispiel #2
0
func GetAccounts(query interface{}, skip int, limit int) (accounts []model.Account, err error) {
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return nil, err
	}
	defer session.Close()
	mquery := tool.MgoQuerySkipLimit(collection.Find(query), skip, limit)
	err = mquery.Iter().All(&accounts)
	return
}
Beispiel #3
0
func InsertAccount(account model.Account) error {
	//TODO 以后写个连接池
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	defer session.Close()
	err = collection.Insert(&account)
	return err
}
Beispiel #4
0
func RemoveAccounts(selector interface{}) (int, error) {
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return 0, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	defer session.Close()
	info, err := collection.RemoveAll(selector)
	if err != nil {
		return 0, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	return info.Removed, err
}
Beispiel #5
0
func UpdateAccountByMgoId(id interface{}, ismgoid bool, update interface{}) (bool, error) {
	id, err := tool.GetTrueQueryId(id, ismgoid)
	if err != nil {
		return false, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return false, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	defer session.Close()
	err = collection.UpdateId(id, update)
	if err != nil {
		return false, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	return true, nil
}
Beispiel #6
0
func GetAccountsPage(meta tool.Meta, query interface{}) (*tool.ResponsePage, error) {
	session, _, collection, err := tool.GetCollection(nil, accountdao.DBNAME, accountdao.COLLECTIONNAME)
	if err != nil {
		return nil, err
	}
	defer session.Close()
	if meta.Total == 0 {
		count, _ := collection.Find(query).Count()
		meta.Total = count
	}
	result, err := accountdao.GetAccounts(query, meta.Offset, meta.Limit)
	meta.Length = len(result)
	meta.SetRemaining()
	//	return &accountMeta{Meta: meta, List: result}, err
	return &tool.ResponsePage{Meta: meta, List: result}, err
}
Beispiel #7
0
func GetAccountByMgoId(id interface{}, ismgoid bool) (*model.Account, error) {
	id, err := tool.GetTrueQueryId(id, ismgoid)
	if err != nil {
		return nil, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	session, _, collection, err := tool.GetCollection(nil, DBNAME, COLLECTIONNAME)
	if err != nil {
		return nil, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	account := new(model.Account)
	defer session.Close()
	err = collection.FindId(id).One(account)
	if err != nil {
		return nil, errors.ErrorCodeMongoError.WithArgs(err.Error())
	}
	return account, nil
}