Documentation ¶
Index ¶
- func FindCount(collectionName string, selector map[string]interface{}) (count int, err error)
- func Init()
- func Insert(collectionName string, dataMap map[string]interface{}) (err error)
- func M(collectionName string, f func(*mgo.Collection) error) error
- func Remove(collectionName string, selector map[string]interface{}) (err error)
- func RemoveAll(collectionName string, selector map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func RemoveId(collectionName string, id string) (err error)
- func Session() *mgo.Session
- func Update(collectionName string, selector, updater map[string]interface{}) (err error)
- func UpdateAll(collectionName string, selector, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func UpdateId(collectionName, id string, updater map[string]interface{}) (err error)
- func Upsert(collectionName string, selector, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func UpsertId(collectionName, id string, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
- type FindResult
- type FindResults
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindCount ¶
查询文档数 @param collectionName string - Collection name @param selector map[string]interface{} - find condition
@return rs FindResults @return error
func Insert ¶
通用新增数据到mongo collection
@param collectionName string - Collection name @param dataMap map[string]interface{} - Insert data which is a map type
@return err error
func M ¶
简易mongo操作方法,执行完自动释放mongo连接 @param collectionName string - Collection name @param f func(*mgo.Collection) error - A function which execute mongo curd or other operation
@return error
使用示例:
定义一个接收器 oneData := make(map[string]interface{}) 每执行一个M函数都从连接池拷贝一个连接,闭包内有集合collection的操作句柄实例,直接在闭包里mongo操作 M("student", func(collection *mgo.Collection) { err := collection.FindId(bson.ObjectIdHex("5c8608a7baa5203fc198859f")).One(&oneData) ErrorHandler(err, "Find().One()") fmt.Println("查询一个结果:", oneData) })
func Remove ¶
通用删除数据 只删除符合条件的第一个 @param collectionName string - Collection name @param selector map[string]interface{} - remove condition
@return error
func RemoveAll ¶
func RemoveAll(collectionName string, selector map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
通用删除数据 删除所有符合条件的数据 @param collectionName string - Collection name @param selector map[string]interface{} - remove condition
@return changeInfo *mgo.ChangeInfo @return error
func RemoveId ¶
通用删除数据 只删除符合条件的id @param collectionName string - Collection name @param id string - remove data with mongo id
@return error
func Update ¶
通用更新数据 @param collectionName string - Collection name @param selector map[string]interface{} - update condition @param updater map[string]interface{} - update data
@return err error
func UpdateAll ¶
func UpdateAll(collectionName string, selector, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
通用更新数据 @param collectionName string - Collection name @param selector map[string]interface{} - update condition @param updater map[string]interface{} - update data
@return changeInfo *mgo.ChangeInfo @return err error
func UpdateId ¶
通用更新数据 @param collectionName string - Collection name @param id string - mongo id string @param updater map[string]interface{} - update data
@return err error
func Upsert ¶
func Upsert(collectionName string, selector, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)
更新插入数据 @param collectionName string - Collection name @param selector map[string]interface{} - upsert condition @param updater map[string]interface{} - upsert data
@return changeInfo *mgo.ChangeInfo @return err error
Types ¶
type FindResult ¶
type FindResult map[string]interface{}
func FindById ¶
func FindById(collectionName, id string) (rs FindResult, err error)
根据id查找 @param collectionName string - Collection name @param id string - find data with mongo id
@return error
type FindResults ¶
type FindResults []map[string]interface{}