mango

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2019 License: MIT Imports: 9 Imported by: 1

README

mango

图片名称

GoDoc Build Status dependabot FOSSA Status

Use mongo-go-driver like mgo

installation

go get -u github.com/amorist/mango

usage

package main

import (
    "fmt"

    "github.com/amorist/mango"

    "github.com/amorist/mango/bson"
)

// Person person model
type Person struct {
    ID   bson.ObjectID `bson:"_id" json:"_id"`
    Name string        `bson:"name" json:"name"`
}

func main() {
    session := mango.New("mongodb://127.0.0.1")
    session.SetPoolLimit(10)

    if err := session.Connect(); err != nil {
        fmt.Println(err)
        return
    }

    // Find find all
    var result []Person
    if err := session.DB("test").Collection("persons").Find(bson.M{}).All(&result); err != nil {
        fmt.Println(err)
    }

    for _, r := range result {
        fmt.Println(r.Name)
    }

    // Update
    if err := session.DB("test").Collection("persons").Update(bson.M{"name": "name1"}, bson.M{"$set": bson.M{"name": "name2"}}); err != nil {
        fmt.Println(err)
    }

    // Update update all
    info, err := session.DB("test").Collection("persons").UpdateAll(bson.M{"name": "name1"}, bson.M{"$set": bson.M{"name": "name"}})
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(info)

    // Remove
    if err := session.DB("test").Collection("persons").Remove(bson.M{"name": "name"}); err != nil {
        fmt.Println(err)
    }

    // RemoveAll
    if err := session.DB("test").Collection("persons").RemoveAll(bson.M{"name": "name"}); err != nil {
        fmt.Println(err)
    }

    // Insert
    if err := session.DB("test").Collection("persons").Insert(bson.M{"name": "name"}); err != nil {
        fmt.Println(err)
    }

    // InsertAll
    var docs []interface{}
    for index := 0; index < 10; index++ {
        docs = append(docs, bson.M{"name": index})
    }

    if err := session.DB("test").Collection("persons").InsertAll(docs); err != nil {
        fmt.Println(err)
    }

    // Count
    count, err := session.DB("test").Collection("persons").Count(bson.M{"name": "name"})
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(count)
}

doc

mango

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection mongo-driver collection

func (*Collection) Count

func (c *Collection) Count(selector interface{}) (int64, error)

Count gets the number of documents matching the filter.

func (*Collection) Find

func (c *Collection) Find(filter interface{}) *Session

Find finds docs by given filter

func (*Collection) Insert

func (c *Collection) Insert(document interface{}) error

Insert inserts a single document into the collection.

func (*Collection) InsertAll

func (c *Collection) InsertAll(documents []interface{}) error

InsertAll inserts the provided documents.

func (*Collection) InsertAllWithResult

func (c *Collection) InsertAllWithResult(documents []interface{}) (result *mongo.InsertManyResult, err error)

InsertAllWithResult inserts the provided documents and returns insert many result.

func (*Collection) InsertWithResult

func (c *Collection) InsertWithResult(document interface{}) (result *mongo.InsertOneResult, err error)

InsertWithResult inserts a single document into the collection and returns insert one result.

func (*Collection) Remove

func (c *Collection) Remove(selector interface{}) error

Remove deletes a single document from the collection.

func (*Collection) RemoveAll

func (c *Collection) RemoveAll(selector interface{}) error

RemoveAll deletes multiple documents from the collection.

func (*Collection) RemoveID

func (c *Collection) RemoveID(id interface{}) error

RemoveID deletes a single document from the collection by id.

func (*Collection) Update

func (c *Collection) Update(selector interface{}, update interface{}, upsert ...bool) error

Update updates a single document in the collection.

func (*Collection) UpdateAll

func (c *Collection) UpdateAll(selector interface{}, update interface{}, upsert ...bool) (*mongo.UpdateResult, error)

UpdateAll updates multiple documents in the collection.

func (*Collection) UpdateID

func (c *Collection) UpdateID(id interface{}, update interface{}) error

UpdateID updates a single document in the collection by id

func (*Collection) UpdateWithResult

func (c *Collection) UpdateWithResult(selector interface{}, update interface{}, upsert ...bool) (result *mongo.UpdateResult, err error)

UpdateWithResult updates a single document in the collection and returns update result.

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database mongo-driver database

func (*Database) C

func (d *Database) C(collection string) *Collection

C returns collection.

func (*Database) Collection

func (d *Database) Collection(collection string) *Collection

Collection returns collection.

func (*Database) CollectionNames added in v0.1.7

func (d *Database) CollectionNames() (names []string, err error)

CollectionNames returns the collection names present in database.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session mongo session

func New

func New(uri string) *Session

New session

Relevant documentation:

https://docs.mongodb.com/manual/reference/connection-string/

func (*Session) All

func (s *Session) All(result interface{}) error

All find all

func (*Session) C

func (s *Session) C(collection string) *Collection

C Collection alias

func (*Session) Client

func (s *Session) Client() *mongo.Client

Client return mongo Client

func (*Session) Collection

func (s *Session) Collection(collection string) *Collection

Collection returns collection

func (*Session) Connect

func (s *Session) Connect() error

Connect mongo client

func (*Session) DB

func (s *Session) DB(db string) *Database

DB returns a value representing the named database.

func (*Session) Limit

func (s *Session) Limit(limit int64) *Session

Limit specifies a limit on the number of results. A negative limit implies that only 1 batch should be returned.

func (*Session) One

func (s *Session) One(result interface{}) error

One returns up to one document that matches the model.

func (*Session) Ping

func (s *Session) Ping() error

Ping verifies that the client can connect to the topology. If readPreference is nil then will use the client's default read preference.

func (*Session) SetPoolLimit

func (s *Session) SetPoolLimit(limit uint64)

SetPoolLimit specifies the max size of a server's connection pool.

func (*Session) Skip

func (s *Session) Skip(skip int64) *Session

Skip specifies the number of documents to skip before returning. For server versions < 3.2, this defaults to 0.

func (*Session) Sort

func (s *Session) Sort(sort interface{}) *Session

Sort specifies the order in which to return documents.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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