mongodb

package
v0.0.0-...-3208bda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindCount

func FindCount(collectionName string, selector map[string]interface{}) (count int, err error)

查询文档数 @param collectionName string - Collection name @param selector map[string]interface{} - find condition

@return rs FindResults @return error

func Init

func Init()

包初始化时实例化一个mongo session

func Insert

func Insert(collectionName string, dataMap map[string]interface{}) (err error)

通用新增数据到mongo collection

@param collectionName string - Collection name @param dataMap map[string]interface{} - Insert data which is a map type

@return err error

func M

func M(collectionName string, f func(*mgo.Collection) error) error

简易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

func Remove(collectionName string, selector map[string]interface{}) (err error)

通用删除数据 只删除符合条件的第一个 @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

func RemoveId(collectionName string, id string) (err error)

通用删除数据 只删除符合条件的id @param collectionName string - Collection name @param id string - remove data with mongo id

@return error

func Session

func Session() *mgo.Session

copy session 实现连接池

func Update

func Update(collectionName string, selector, updater map[string]interface{}) (err error)

通用更新数据 @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

func UpdateId(collectionName, id string, updater map[string]interface{}) (err error)

通用更新数据 @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

func UpsertId

func UpsertId(collectionName, id string, updater map[string]interface{}) (changeInfo *mgo.ChangeInfo, err error)

更新插入数据 @param collectionName string - Collection name @param id string - mongo id string @param updater map[string]interface{} - upsert data

@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

func FindOne

func FindOne(collectionName string, selector map[string]interface{}) (rs FindResult, err error)

查找单个文档 @param collectionName string - Collection name @param selector map[string]interface{} - find condition

@return rs FindResult @return error

type FindResults

type FindResults []map[string]interface{}

func FindAll

func FindAll(collectionName string, selector map[string]interface{}) (rs FindResults, err error)

查找多个文档 @param collectionName string - Collection name @param selector map[string]interface{} - find condition

@return rs FindResults @return error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL