mongo

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

import (
    "github.com/joshqu1985/fireman/pkg/mongo"
)

// 全局初始化调用一次
func init() {
    if err := mongo.Init(conf); err != nil {
        panic(err)
    }
}

// 使用示例
func DemoListMongoData(sid string, offset, limit int) (CommentList, error) {
    items := make(CommentList, 0)
    h := func(c *mgo.Collection) error {
        return c.Find(bson.M{"source_id": sid, "status": 0}).
            Select(bson.M{"replys": bson.M{"$slice": 2000}}).
            Sort("-created_at").Skip(offset).Limit(limit).All(&items)
    }
    return items, mongo.Doit("Comment", "comment", handle)
}

func DemoCountMongoData(sid string) (count int, err error) {
    h := func(c *mgo.Collection) error {
        count, err = c.Find(bson.M{"source_id": sid, "status": 0}).Count()
        return err
    }
    return count, mongo.Doit("Comment", "comment", h) // 库: Comment; 集合: comment
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host     string
	Auth     string
	Database []string
}

type Pool

type Pool struct {
	Sessions map[string]*mgo.Session
}

func NewPool

func NewPool(conf Config) *Pool

func (*Pool) Doit

func (p *Pool) Doit(c context.Context, db, collect string, h func(*mgo.Collection) error) error

Jump to

Keyboard shortcuts

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