mongo

package
v0.0.0-...-9e0c27b Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2012 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Wraps MongoDB to make it compatible with the model package.

Built on top of Gustavo Niemeyer's great mgo package: http://labix.org/mgo

Index

Constants

This section is empty.

Variables

View Source
var Collections = map[string]*Collection{}
View Source
var Config = Configuration{
	Safe:                mgo.Safe{FSync: true, J: true},
	CheckQuerySelectors: true,
}
View Source
var Database *mgo.Database

Functions

func CheckRefs

func CheckRefs(refs []Ref) (validRefs []Ref, invalidRefs []Ref, err error)

nil refs will be ignored

func DereferenceIterator

func DereferenceIterator(refs ...Ref) model.Iterator

Returns an iterator of dereferenced refs, or an error iterator if there was an error

func FailsafeDereferenceIterator

func FailsafeDereferenceIterator(refs ...Ref) (i model.Iterator, errors []error)

Returns an iterator for all refs without error and a slice of the errors

func FindRefWithID

func FindRefWithID(refs []Ref, id bson.ObjectId) (index int, found bool)

func InitLocalhost

func InitLocalhost(database, user, password string) (err error)

func InitRefs

func InitRefs(document interface{})

func MatchBsonField

func MatchBsonField(bsonName string) reflection.MatchStructFieldFunc

func RemoveInvalidRefsInAllCollections

func RemoveInvalidRefsInAllCollections() (invalidCollectionRefs map[string][]InvalidRefData, err error)

func ReverseBsonD

func ReverseBsonD(d bson.D)

func SortRefs

func SortRefs(refs []Ref, lessFunc func(a, b *Ref) bool)

Types

type Collection

type Collection struct {
	Name         string
	DocumentType reflect.Type
	// contains filtered or unexported fields
}

Collection represents a MongoDB collection and implements mongo.Query for all documents in the collection.

Example for creating, modifying and saving a document:

user := models.Users.NewDocument().(*models.User)

user.Name.First.Set("Erik")
user.Name.Last.Set("Unger")

err := user.Save()

func CollectionByName

func CollectionByName(name string) (collection *Collection, ok bool)

func NewCollection

func NewCollection(name string, documentPrototype interface{}) *Collection

func (*Collection) Collection

func (self *Collection) Collection() *Collection

func (*Collection) Count

func (self *Collection) Count() (n int, err error)

func (*Collection) DocumentWithID

func (self *Collection) DocumentWithID(id bson.ObjectId, subDocSelectors ...string) (document interface{}, err error)

func (*Collection) DocumentWithIDIterator

func (self *Collection) DocumentWithIDIterator(id bson.ObjectId, subDocSelectors ...string) model.Iterator

func (*Collection) Explain

func (self *Collection) Explain() string

func (*Collection) Filter

func (self *Collection) Filter(selector string, value interface{}) Query

func (*Collection) FilterAllIn

func (self *Collection) FilterAllIn(selector string, values ...interface{}) Query

func (*Collection) FilterArraySize

func (self *Collection) FilterArraySize(selector string, size int) Query

func (*Collection) FilterContains

func (self *Collection) FilterContains(selector string, str string) Query

func (*Collection) FilterContainsCaseInsensitive

func (self *Collection) FilterContainsCaseInsensitive(selector string, str string) Query

func (*Collection) FilterEndsWith

func (self *Collection) FilterEndsWith(selector string, str string) Query

func (*Collection) FilterEndsWithCaseInsensitive

func (self *Collection) FilterEndsWithCaseInsensitive(selector string, str string) Query

func (*Collection) FilterEqualCaseInsensitive

func (self *Collection) FilterEqualCaseInsensitive(selector string, str string) Query

func (*Collection) FilterExists

func (self *Collection) FilterExists(selector string, exists bool) Query

func (*Collection) FilterFunc

func (self *Collection) FilterFunc(passFilter model.FilterFunc) model.Iterator

func (*Collection) FilterGreater

func (self *Collection) FilterGreater(selector string, value interface{}) Query

func (*Collection) FilterGreaterEqual

func (self *Collection) FilterGreaterEqual(selector string, value interface{}) Query

func (*Collection) FilterIn

func (self *Collection) FilterIn(selector string, values ...interface{}) Query

func (*Collection) FilterLess

func (self *Collection) FilterLess(selector string, value interface{}) Query

func (*Collection) FilterLessEqual

func (self *Collection) FilterLessEqual(selector string, value interface{}) Query

func (*Collection) FilterModulo

func (self *Collection) FilterModulo(selector string, divisor, result interface{}) Query

func (*Collection) FilterNotEqual

func (self *Collection) FilterNotEqual(selector string, value interface{}) Query

func (*Collection) FilterNotIn

func (self *Collection) FilterNotIn(selector string, values ...interface{}) Query

func (*Collection) FilterRef

func (self *Collection) FilterRef(selector string, ref ...Ref) Query

func (*Collection) FilterReferenced

func (self *Collection) FilterReferenced(refs ...Ref) Query

func (*Collection) FilterStartsWith

func (self *Collection) FilterStartsWith(selector string, str string) Query

func (*Collection) FilterStartsWithCaseInsensitive

func (self *Collection) FilterStartsWithCaseInsensitive(selector string, str string) Query

func (*Collection) FilterWhere

func (self *Collection) FilterWhere(javascript string) Query

func (*Collection) GetOrCreateOne

func (self *Collection) GetOrCreateOne() (document interface{}, found bool, err error)

func (*Collection) IDs

func (self *Collection) IDs() (ids []bson.ObjectId, err error)

func (*Collection) Init

func (self *Collection) Init()

func (*Collection) InitDocument

func (self *Collection) InitDocument(doc interface{}, subDocSelectors ...string)

func (*Collection) Insert

func (self *Collection) Insert(document interface{}) (id bson.ObjectId, err error)

Inserts document regardless if it's already in the collection If document has a DocumentBase, the ID will be updated to the newly created one

func (*Collection) IsFilter

func (self *Collection) IsFilter() bool

func (*Collection) Iterator

func (self *Collection) Iterator() model.Iterator

func (*Collection) Limit

func (self *Collection) Limit(limit int) Query

func (*Collection) NewDocument

func (self *Collection) NewDocument(subDocSelectors ...string) interface{}

return error?

func (*Collection) One

func (self *Collection) One() (document interface{}, err error)

func (*Collection) OneID

func (self *Collection) OneID() (id bson.ObjectId, err error)

func (*Collection) Or

func (self *Collection) Or() Query

func (*Collection) ParentQuery

func (self *Collection) ParentQuery() Query

func (*Collection) Ref

func (self *Collection) Ref(id bson.ObjectId) Ref

func (*Collection) Refs

func (self *Collection) Refs() (refs []Ref, err error)

func (*Collection) Remove

func (self *Collection) Remove(ids ...bson.ObjectId) (err error)

func (*Collection) RemoveAll

func (self *Collection) RemoveAll() error

func (*Collection) RemoveAllNotIn

func (self *Collection) RemoveAllNotIn(ids ...bson.ObjectId) error

func (*Collection) RemoveInvalidRefs

func (self *Collection) RemoveInvalidRefs() (invalidRefs []InvalidRefData, err error)

RemoveInvalidRefs removes invalid refs from all documents and saves the changes.

func (*Collection) Selector

func (self *Collection) Selector() string

func (*Collection) Skip

func (self *Collection) Skip(skip int) Query

func (*Collection) Sort

func (self *Collection) Sort(selector string) Query

func (*Collection) SortFunc

func (self *Collection) SortFunc(lessFunc func(a, b interface{}) bool) model.Iterator

func (*Collection) SortReverse

func (self *Collection) SortReverse(selector string) Query

func (*Collection) String

func (self *Collection) String() string

func (*Collection) SubDocument

func (self *Collection) SubDocument(selector string) Query

func (*Collection) TryDocumentWithID

func (self *Collection) TryDocumentWithID(id bson.ObjectId, subDocSelectors ...string) (document interface{}, found bool, err error)

func (*Collection) TryDocumentWithIDIterator

func (self *Collection) TryDocumentWithIDIterator(id bson.ObjectId, subDocSelectors ...string) model.Iterator

func (*Collection) TryOne

func (self *Collection) TryOne() (document interface{}, found bool, err error)

func (*Collection) TryOneID

func (self *Collection) TryOneID() (id bson.ObjectId, found bool, err error)

func (*Collection) Update

func (self *Collection) Update(id bson.ObjectId, document interface{}) (err error)

func (*Collection) UpdateAll

func (self *Collection) UpdateAll(selector string, value interface{}) error

func (*Collection) UpdateOne

func (self *Collection) UpdateOne(selector string, value interface{}) error

func (*Collection) ValidateSelector

func (self *Collection) ValidateSelector(subDocSelectors ...string) (err error)

type Configuration

type Configuration struct {
	Host                string
	Database            string
	User                string
	Password            string
	Safe                mgo.Safe
	CheckQuerySelectors bool
}

func (*Configuration) Close

func (self *Configuration) Close() error

func (*Configuration) Init

func (self *Configuration) Init() error

func (*Configuration) Name

func (self *Configuration) Name() string

type Document

type Document interface {
	Init(collection *Collection, embeddingStruct interface{})
	Collection() *Collection
	ObjectId() bson.ObjectId
	SetObjectId(id bson.ObjectId)
	Iterator() model.Iterator
	Ref() Ref
	Save() error
	Remove() error
	// RemoveInvalidRefs sets all invalid mongo.Ref instances to empty,
	// but does not save the document.
	RemoveInvalidRefs() (invalidRefs []InvalidRefData, err error)
}

type DocumentBase

type DocumentBase struct {
	ID bson.ObjectId `bson:"_id,omitempty" gostart:"-"`
	// contains filtered or unexported fields
}

func (*DocumentBase) Collection

func (self *DocumentBase) Collection() *Collection

func (*DocumentBase) Init

func (self *DocumentBase) Init(collection *Collection, embeddingStruct interface{})

func (*DocumentBase) Iterator

func (self *DocumentBase) Iterator() model.Iterator

func (*DocumentBase) ObjectId

func (self *DocumentBase) ObjectId() bson.ObjectId

func (*DocumentBase) Ref

func (self *DocumentBase) Ref() Ref

func (*DocumentBase) Remove

func (self *DocumentBase) Remove() error

func (*DocumentBase) RemoveInvalidRefs

func (self *DocumentBase) RemoveInvalidRefs() (invalidRefs []InvalidRefData, err error)

func (*DocumentBase) Save

func (self *DocumentBase) Save() error

func (*DocumentBase) SetObjectId

func (self *DocumentBase) SetObjectId(id bson.ObjectId)

type ForeignRef

type ForeignRef struct {
	Collection *Collection
	Selector   string
}

type InvalidRefData

type InvalidRefData struct {
	Ref      *Ref
	MetaData *model.MetaData
}

func RemoveInvalidRefs

func RemoveInvalidRefs(document interface{}) (invalidRefs []InvalidRefData, err error)

type MongoIterator

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

func (*MongoIterator) Err

func (self *MongoIterator) Err() error

func (*MongoIterator) Next

func (self *MongoIterator) Next() interface{}

type Query

type Query interface {
	Selector() string

	ParentQuery() Query
	Collection() *Collection

	SubDocument(selector string) Query
	Skip(int) Query
	Limit(int) Query
	Sort(selector string) Query                               // Chain Sort() and SortReverse() for multi value sorting
	SortReverse(selector string) Query                        // Chain Sort() and SortReverse() for multi value sorting
	SortFunc(less func(a, b interface{}) bool) model.Iterator // Last query of chain

	// FilterX must be the first query on a Collection
	IsFilter() bool
	Filter(selector string, value interface{}) Query
	FilterWhere(javascript string) Query

	// Filter via a Go function. Note that all documents have to be loaded
	// in memory in order for Go code to be able to filter it.
	FilterFunc(passFilter model.FilterFunc) model.Iterator
	FilterRef(selector string, ref ...Ref) Query
	FilterEqualCaseInsensitive(selector string, str string) Query
	FilterNotEqual(selector string, value interface{}) Query
	FilterLess(selector string, value interface{}) Query
	FilterGreater(selector string, value interface{}) Query
	FilterLessEqual(selector string, value interface{}) Query
	FilterGreaterEqual(selector string, value interface{}) Query
	FilterModulo(selector string, divisor, result interface{}) Query
	FilterIn(selector string, values ...interface{}) Query
	FilterNotIn(selector string, values ...interface{}) Query
	FilterAllIn(selector string, values ...interface{}) Query
	FilterArraySize(selector string, size int) Query
	FilterStartsWith(selector string, str string) Query
	FilterStartsWithCaseInsensitive(selector string, str string) Query
	FilterEndsWith(selector string, str string) Query
	FilterEndsWithCaseInsensitive(selector string, str string) Query
	FilterContains(selector string, str string) Query
	FilterContainsCaseInsensitive(selector string, str string) Query
	FilterExists(selector string, exists bool) Query

	Or() Query

	// Statistics
	Count() (n int, err error)
	// Distinct() int
	Explain() string

	// Read
	One() (document interface{}, err error)
	TryOne() (document interface{}, found bool, err error)
	GetOrCreateOne() (document interface{}, found bool, err error)

	Iterator() model.Iterator
	OneID() (id bson.ObjectId, err error)
	TryOneID() (id bson.ObjectId, found bool, err error)
	IDs() (ids []bson.ObjectId, err error)
	Refs() (refs []Ref, err error)

	// Write
	UpdateOne(selector string, value interface{}) error
	UpdateAll(selector string, value interface{}) error

	// RemoveAll ignores Skip() and Limit()
	RemoveAll() error
	// contains filtered or unexported methods
}

Query is the interface with all query methods for mongo.

type QueryError

type QueryError struct {
	Err error
	// contains filtered or unexported fields
}

QueryError implements Query, but all it does is hold and return an error generated by another query.

func (*QueryError) Collection

func (self *QueryError) Collection() *Collection

func (*QueryError) Count

func (self *QueryError) Count() (n int, err error)

func (*QueryError) Explain

func (self *QueryError) Explain() string

func (*QueryError) Filter

func (self *QueryError) Filter(selector string, value interface{}) Query

func (*QueryError) FilterAllIn

func (self *QueryError) FilterAllIn(selector string, values ...interface{}) Query

func (*QueryError) FilterArraySize

func (self *QueryError) FilterArraySize(selector string, size int) Query

func (*QueryError) FilterContains

func (self *QueryError) FilterContains(selector string, str string) Query

func (*QueryError) FilterContainsCaseInsensitive

func (self *QueryError) FilterContainsCaseInsensitive(selector string, str string) Query

func (*QueryError) FilterEndsWith

func (self *QueryError) FilterEndsWith(selector string, str string) Query

func (*QueryError) FilterEndsWithCaseInsensitive

func (self *QueryError) FilterEndsWithCaseInsensitive(selector string, str string) Query

func (*QueryError) FilterEqualCaseInsensitive

func (self *QueryError) FilterEqualCaseInsensitive(selector string, str string) Query

func (*QueryError) FilterExists

func (self *QueryError) FilterExists(selector string, exists bool) Query

func (*QueryError) FilterFunc

func (self *QueryError) FilterFunc(passFilter model.FilterFunc) model.Iterator

func (*QueryError) FilterGreater

func (self *QueryError) FilterGreater(selector string, value interface{}) Query

func (*QueryError) FilterGreaterEqual

func (self *QueryError) FilterGreaterEqual(selector string, value interface{}) Query

func (*QueryError) FilterIn

func (self *QueryError) FilterIn(selector string, values ...interface{}) Query

func (*QueryError) FilterLess

func (self *QueryError) FilterLess(selector string, value interface{}) Query

func (*QueryError) FilterLessEqual

func (self *QueryError) FilterLessEqual(selector string, value interface{}) Query

func (*QueryError) FilterModulo

func (self *QueryError) FilterModulo(selector string, divisor, result interface{}) Query

func (*QueryError) FilterNotEqual

func (self *QueryError) FilterNotEqual(selector string, value interface{}) Query

func (*QueryError) FilterNotIn

func (self *QueryError) FilterNotIn(selector string, values ...interface{}) Query

func (*QueryError) FilterRef

func (self *QueryError) FilterRef(selector string, ref ...Ref) Query

func (*QueryError) FilterStartsWith

func (self *QueryError) FilterStartsWith(selector string, str string) Query

func (*QueryError) FilterStartsWithCaseInsensitive

func (self *QueryError) FilterStartsWithCaseInsensitive(selector string, str string) Query

func (*QueryError) FilterWhere

func (self *QueryError) FilterWhere(javascript string) Query

func (*QueryError) GetOrCreateOne

func (self *QueryError) GetOrCreateOne() (document interface{}, found bool, err error)

func (*QueryError) IDs

func (self *QueryError) IDs() (ids []bson.ObjectId, err error)

func (*QueryError) IsFilter

func (self *QueryError) IsFilter() bool

func (*QueryError) Iterator

func (self *QueryError) Iterator() model.Iterator

func (*QueryError) Limit

func (self *QueryError) Limit(int) Query

func (*QueryError) One

func (self *QueryError) One() (document interface{}, err error)

func (*QueryError) OneID

func (self *QueryError) OneID() (id bson.ObjectId, err error)

func (*QueryError) Or

func (self *QueryError) Or() Query

func (*QueryError) ParentQuery

func (self *QueryError) ParentQuery() Query

func (*QueryError) Refs

func (self *QueryError) Refs() (refs []Ref, err error)

func (*QueryError) RemoveAll

func (self *QueryError) RemoveAll() error

func (*QueryError) Selector

func (self *QueryError) Selector() string

func (*QueryError) Skip

func (self *QueryError) Skip(int) Query

func (*QueryError) Sort

func (self *QueryError) Sort(selector string) Query

func (*QueryError) SortFunc

func (self *QueryError) SortFunc(less func(a, b interface{}) bool) model.Iterator

func (*QueryError) SortReverse

func (self *QueryError) SortReverse(selector string) Query

func (*QueryError) SubDocument

func (self *QueryError) SubDocument(selector string) Query

func (*QueryError) TryOne

func (self *QueryError) TryOne() (document interface{}, found bool, err error)

func (*QueryError) TryOneID

func (self *QueryError) TryOneID() (id bson.ObjectId, found bool, err error)

func (*QueryError) UpdateAll

func (self *QueryError) UpdateAll(selector string, value interface{}) error

func (*QueryError) UpdateOne

func (self *QueryError) UpdateOne(selector string, value interface{}) error

type Ref

type Ref struct {
	ID             bson.ObjectId `gostart:"-"`
	CollectionName string        `gostart:"-"`
}

Ref holds a mongo.ID and the name of the collection of the referenced document. Only the ID will be saved in MongoDB, the collection name is for validation and convenience functions only.

func RemoveRefWithIDFromSlice

func RemoveRefWithIDFromSlice(refs []Ref, id bson.ObjectId) ([]Ref, bool)

func (*Ref) CheckID

func (self *Ref) CheckID() (bool, error)

func (*Ref) Collection

func (self *Ref) Collection() *Collection

func (*Ref) DebugString

func (self *Ref) DebugString() string

func (*Ref) Get

func (self *Ref) Get() (doc interface{}, err error)

Returns nil and no error if the reference is empty

func (Ref) GetBSON

func (self Ref) GetBSON() (interface{}, error)

Implements bson.Getter

func (*Ref) GetOrError

func (self *Ref) GetOrError() (doc interface{}, err error)

Returns an error if the reference is empty

func (*Ref) IsEmpty

func (self *Ref) IsEmpty() bool

func (*Ref) Reference

func (self *Ref) Reference()

Dummy function to implement model.Reference

func (*Ref) References

func (self *Ref) References(doc Document) (ok bool, err error)

func (*Ref) Required

func (self *Ref) Required(metaData *model.MetaData) bool

func (*Ref) Set

func (self *Ref) Set(document Document)

nil is valid and sets the reference to empty

func (*Ref) SetBSON

func (self *Ref) SetBSON(raw bson.Raw) error

Implements bson.Setter

func (*Ref) SetString

func (self *Ref) SetString(str string) error

func (*Ref) String

func (self *Ref) String() string

func (*Ref) Validate

func (self *Ref) Validate(metaData *model.MetaData) error

type SortableRefs

type SortableRefs struct {
	Refs     []Ref
	LessFunc func(a, b *Ref) bool
}

func (*SortableRefs) Len

func (self *SortableRefs) Len() int

func (*SortableRefs) Less

func (self *SortableRefs) Less(i, j int) bool

func (*SortableRefs) Swap

func (self *SortableRefs) Swap(i, j int)

type SubDocument

type SubDocument interface {
	Init(collection *Collection, selector string, embeddingStruct interface{})
	Collection() *Collection
	RootDocumentObjectId() bson.ObjectId
	RootDocumentSetObjectId(id bson.ObjectId)
	Iterator() model.Iterator
	Save() error
	RemoveRootDocument() error
}

type SubDocumentBase

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

func (*SubDocumentBase) Collection

func (self *SubDocumentBase) Collection() *Collection

func (*SubDocumentBase) Init

func (self *SubDocumentBase) Init(collection *Collection, selector string, embeddingStruct interface{})

func (*SubDocumentBase) Iterator

func (self *SubDocumentBase) Iterator() model.Iterator

func (*SubDocumentBase) RemoveRootDocument

func (self *SubDocumentBase) RemoveRootDocument() error

func (*SubDocumentBase) RootDocumentObjectId

func (self *SubDocumentBase) RootDocumentObjectId() bson.ObjectId

Implements DocumentInterface

func (*SubDocumentBase) RootDocumentSetObjectId

func (self *SubDocumentBase) RootDocumentSetObjectId(id bson.ObjectId)

func (*SubDocumentBase) Save

func (self *SubDocumentBase) Save() error

Jump to

Keyboard shortcuts

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