xmgo

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func C

func C(c interface{}) (*mgo.Collection, error)

获取集合

func Config

func Config(name ...string) *xmgoConfig

获取mongo数据库唯一配置

func Drop

func Drop(c interface{}) error

删除集合

Example

删除集合

err := Drop("test")
fmt.Printf("drop err:%v", err)
Output:

func FindById

func FindById(c interface{}, id interface{}, selects ...map[string]interface{}) (interface{}, error)

根据id查找记录

Example
one, err := FindById("test", testId)
fmt.Printf("findById result:%v err:%v\n", one, err)
Output:

func FindOne

func FindOne(c interface{}, where map[string]interface{}, selects ...interface{}) (interface{}, error)

获取一条数据

Example
one, err := FindOne("test", nil)
fmt.Printf("findOne result:%v err:%v\n", one, err)
Output:

func Insert

func Insert(c interface{}, docs map[string]interface{}) error

添加记录

Example
err := Insert("test", map[string]interface{}{
	"_id":      testId,
	"name":     "张三",
	"age":      32,
	"birthday": time.Now(),
	"audit":    true,
})
fmt.Printf("insert err:%v\n", err)
Output:

func Query

func Query(c interface{}, where map[string]interface{}) (*mgo.Query, error)

查询数据

Example
query, err := Query("test", nil)
if err != nil {
	fmt.Printf("query err:%v\n", err)
}
var rows []map[string]interface{}
err = query.Select(map[string]interface{}{
	"name": 1,
	"age":  1,
}).All(&rows)
fmt.Printf("query rows:%v, err:%v\n", rows, err)
Output:

func Update

func Update(c interface{}, where map[string]interface{}, data interface{}) error

更新一条记录

func UpdateAll

func UpdateAll(c interface{}, where map[string]interface{}, data interface{}) (*mgo.ChangeInfo, error)

更新所有记录

func UpdateById

func UpdateById(c interface{}, id interface{}, data interface{}) error

根据ID更新记录

Example
err := UpdateById("test", testId, bson.M{
	"name": "李四",
	"age":  18,
})
fmt.Printf("updateById result err:%v", err)
Output:

Types

type Ixmgo

type Ixmgo interface {
	// 获取数据库连接
	DB(db ...string) *mgo.Database
	// 打开连接
	Open(cfg ...interface{}) error
	// 关闭数据库连接
	Close() error
	// 配置mongo数据库连接
	Config(cfg interface{}) error
	// 获取数据库连接
	GetConfig() interface{}
}

func InitXmgo

func InitXmgo(cfg *gcfg.Config) (Ixmgo, error)

func Xmgo

func Xmgo(name ...string) Ixmgo

获取mongo客户端单一实例

type IxmgoCollection

type IxmgoCollection interface {
	Name() string
}

mongo collection集合名称

Jump to

Keyboard shortcuts

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