mongo

package
v0.0.0-...-8bfda83 Latest Latest
Warning

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

Go to latest
Published: May 12, 2015 License: MIT Imports: 14 Imported by: 41

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{
		W:     1,
		FSync: false,
		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 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 NewMongoIterator

func NewMongoIterator(query Query) model.Iterator

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
	DocLabelSelectors []string
	DocLabelSeparator string
	// 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, documentLabelSelectors ...string) *Collection

func (*Collection) Collection

func (self *Collection) Collection() *Collection

func (*Collection) Count

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

func (*Collection) DeleteAllNotWithIDs

func (self *Collection) DeleteAllNotWithIDs(ids ...bson.ObjectId) (numDeleted int, err error)

func (*Collection) DeleteAllWithIDs

func (self *Collection) DeleteAllWithIDs(ids ...bson.ObjectId) (numDeleted int, err error)

func (*Collection) DeleteWithID

func (self *Collection) DeleteWithID(id bson.ObjectId) error

func (*Collection) DocumentIDs

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

func (*Collection) DocumentLabel

func (self *Collection) DocumentLabel(docId bson.ObjectId, labelSelectors ...string) (string, error)

func (*Collection) DocumentWithID

func (self *Collection) DocumentWithID(id bson.ObjectId, resultRef interface{}) error

func (*Collection) DocumentWithIDIterator

func (self *Collection) DocumentWithIDIterator(id bson.ObjectId) 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) FilterID

func (self *Collection) FilterID(ids ...bson.ObjectId) 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, refs ...Ref) Query

func (*Collection) FilterReferenced

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

FilterReferenced filters the collection for documents, that are referenced by refs.

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) Init

func (self *Collection) Init()

func (*Collection) InitAndSaveDocument

func (self *Collection) InitAndSaveDocument(doc Document) error

func (*Collection) InitDocument

func (self *Collection) InitDocument(doc Document)

func (*Collection) InitSubDocument

func (self *Collection) InitSubDocument(subDoc interface{})

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) OneDocument

func (self *Collection) OneDocument(resultRef interface{}) error

func (*Collection) OneDocumentID

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

func (*Collection) OneSubDocument

func (self *Collection) OneSubDocument(selector string, resultRef interface{}) error

func (*Collection) Or

func (self *Collection) Or() Query

func (*Collection) ParentQuery

func (self *Collection) ParentQuery() Query

func (*Collection) RefForID

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

func (*Collection) Refs

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

func (*Collection) RemoveAll

func (self *Collection) RemoveAll() (numRemoved int, err 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(selectors ...string) Query

func (*Collection) SortFunc

func (self *Collection) SortFunc(compareFunc interface{}) model.Iterator

func (*Collection) String

func (self *Collection) String() string

func (*Collection) SubDocument

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

func (*Collection) SubDocumentWithID

func (self *Collection) SubDocumentWithID(id bson.ObjectId, selector string, resultRef interface{}) error

func (*Collection) TryDocumentWithID

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

func (*Collection) TryDocumentWithIDIterator

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

func (*Collection) TryOneDocument

func (self *Collection) TryOneDocument(resultRef interface{}) (found bool, err error)

func (*Collection) TryOneDocumentID

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

func (*Collection) TryOneSubDocument

func (self *Collection) TryOneSubDocument(selector string, resultRef interface{}) (found bool, err error)

func (*Collection) TrySubDocumentWithID

func (self *Collection) TrySubDocumentWithID(id bson.ObjectId, selector string, resultRef interface{}) (found bool, err error)

func (*Collection) UpdateAll

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

func (*Collection) UpdateOne

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

func (*Collection) UpdateSubDocument

func (self *Collection) UpdateSubDocument(selector string, subDocument interface{}) error

func (*Collection) UpdateSubDocumentWithID

func (self *Collection) UpdateSubDocumentWithID(id bson.ObjectId, selector string, subDocument interface{}) error

func (*Collection) ValidateSelectors

func (self *Collection) ValidateSelectors(selectors ...string) (err error)

todo

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 DereferenceIterator

type DereferenceIterator struct {
	Refs []Ref
	// contains filtered or unexported fields
}

func NewDereferenceIterator

func NewDereferenceIterator(refs ...Ref) *DereferenceIterator

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

func (*DereferenceIterator) Err

func (self *DereferenceIterator) Err() error

func (*DereferenceIterator) Next

func (self *DereferenceIterator) Next(resultRef interface{}) bool

type Document

type Document interface {
	Init(collection *Collection, embeddingStruct interface{})
	Collection() *Collection
	ObjectId() bson.ObjectId
	SetObjectId(id bson.ObjectId)
	// Iterator returns an iterator that iterates only this document.
	Iterator() model.Iterator
	CreationTime() time.Time
	Ref() Ref
	Save() error
	Delete() 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) CreationTime

func (self *DocumentBase) CreationTime() (t time.Time)

func (*DocumentBase) Delete

func (self *DocumentBase) Delete() error

func (*DocumentBase) Init

func (self *DocumentBase) Init(collection *Collection, embeddingStructPtr 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) RemoveInvalidRefs

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

func (*DocumentBase) Save

func (self *DocumentBase) Save() (err 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(resultRef interface{}) bool

type Query

type Query interface {
	Selector() string

	ParentQuery() Query
	Collection() *Collection

	SubDocument(selector string) Query
	Skip(int) Query
	Limit(int) Query
	Sort(selectors ...string) Query // sort reverse by prefixing the selector with a minus -

	// SortFunc will sort the query according to compareFunc.
	// compareFunc must have two arguments that are assignable from
	// the iterated document type or pointers to such a type.
	// The result of compareFunc must be a bool indicating
	// if the first argument is less than the second.
	SortFunc(compareFunc interface{}) model.Iterator

	// 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, refs ...Ref) Query
	FilterID(ids ...bson.ObjectId) 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
	OneDocument(resultRef interface{}) error
	TryOneDocument(resultRef interface{}) (found bool, err error)

	OneSubDocument(selector string, resultRef interface{}) error
	TryOneSubDocument(selector string, resultRef interface{}) (found bool, err error)

	Iterator() model.Iterator

	OneDocumentID() (id bson.ObjectId, err error)
	TryOneDocumentID() (id bson.ObjectId, found bool, err error)
	DocumentIDs() (ids []bson.ObjectId, err error)
	Refs() (refs []Ref, err error)

	// Write
	UpdateOne(selector string, value interface{}) error
	UpdateAll(selector string, value interface{}) (numUpdated int, err error)
	// UpdateSubDocument updates a sub-part of queried documents.
	// selector can be an empty string if subDocument is on
	// the root level of the queried documents.
	UpdateSubDocument(selector string, subDocument interface{}) error

	// RemoveAll ignores Skip() and Limit()
	RemoveAll() (numRemoved int, err 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) DocumentIDs

func (self *QueryError) DocumentIDs() (ids []bson.ObjectId, 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) FilterID

func (self *QueryError) FilterID(id ...bson.ObjectId) 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) HasDocumentWithID

func (self *QueryError) HasDocumentWithID(id bson.ObjectId) (bool, 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) OneDocument

func (self *QueryError) OneDocument(resultRef interface{}) error

func (*QueryError) OneDocumentID

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

func (*QueryError) OneSubDocument

func (self *QueryError) OneSubDocument(selector string, resultRef interface{}) 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() (numRemoved int, err 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(selectors ...string) Query

func (*QueryError) SortFunc

func (self *QueryError) SortFunc(compareFunc interface{}) model.Iterator

func (*QueryError) SubDocument

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

func (*QueryError) TryOneDocument

func (self *QueryError) TryOneDocument(resultRef interface{}) (found bool, err error)

func (*QueryError) TryOneDocumentID

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

func (*QueryError) TryOneSubDocument

func (self *QueryError) TryOneSubDocument(selector string, resultRef interface{}) (found bool, err error)

func (*QueryError) UpdateAll

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

func (*QueryError) UpdateOne

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

func (*QueryError) UpdateSubDocument

func (self *QueryError) UpdateSubDocument(selector string, subDocument 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) DocumentLabel

func (self *Ref) DocumentLabel() (string, error)

func (*Ref) Get

func (self *Ref) Get(resultRef interface{}) 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) IsEmpty

func (self *Ref) IsEmpty() bool

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) SetStringID

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

func (*Ref) String

func (self *Ref) String() string

func (*Ref) StringID

func (self *Ref) StringID() string

func (*Ref) TryGet

func (self *Ref) TryGet(resultRef interface{}) (found bool, err error)

Returns an error if the reference is empty

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)

Jump to

Keyboard shortcuts

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