Documentation ¶
Overview ¶
Package Marango provides an intuitive ODM (Object Document Model) library for working with MongoDB documents. It builds on top of the awesome mgo library
Index ¶
- func ObjectId(id string) bson.ObjectId
- type D
- type Document
- func (d *Document) Apply(update interface{}) error
- func (d *Document) IsValid() bool
- func (d *Document) OnCreate()
- func (d *Document) OnResult()
- func (d *Document) Populate(fields ...string) error
- func (d *Document) PopulateOne(field string, value interface{}) error
- func (d *Document) PopulateQuery(path string, q *Query, value interface{}) error
- func (d *Document) Populated(path string, result interface{}) bool
- func (d *Document) PostRemove()
- func (d *Document) PostSave()
- func (d *Document) PreRemove()
- func (d *Document) PreSave()
- func (d *Document) Remove() error
- func (d *Document) Save() error
- type M
- type Marango
- func (z *Marango) C(model string) (*mgo.Collection, bool)
- func (z *Marango) CreateDoc(doc interface{})
- func (z *Marango) Model(name string) *Model
- func (z *Marango) ObjectId(id string) bson.ObjectId
- func (z *Marango) Register(schema interface{}, collectionName string) *Model
- func (z *Marango) SetModelTag(key string)
- type Model
- func (m *Model) CreateDoc(i interface{})
- func (m *Model) Find(query interface{}) *Query
- func (m *Model) FindId(id interface{}) *Query
- func (m *Model) RemoveId(id interface{}) error
- func (m *Model) UpdateId(id interface{}, change interface{}) error
- func (m *Model) UpsertId(id interface{}, change interface{}) (*mgo.ChangeInfo, error)
- type Query
- func (query *Query) Exec(result interface{}) error
- func (q *Query) Limit(lim int) *Query
- func (q *Query) Populate(fields ...string) *Query
- func (q *Query) PopulateQuery(field string, query *Query) *Query
- func (q *Query) Select(selection interface{}) *Query
- func (q *Query) Skip(skip int) *Query
- func (q *Query) Sort(fields ...string) *Query
- type Virtual
- func (v *Virtual) Get(name string) (interface{}, bool)
- func (v *Virtual) GetBool(name string) (bool, bool)
- func (v *Virtual) GetFloat(name string) (float64, bool)
- func (v *Virtual) GetInt(name string) (int, bool)
- func (v *Virtual) GetObjectId(name string) (bson.ObjectId, bool)
- func (v *Virtual) GetString(name string) (string, bool)
- func (v *Virtual) GetTime(name string) (time.Time, bool)
- func (v *Virtual) Set(name string, val interface{})
- func (v *Virtual) SetBool(name string, val bool)
- func (v *Virtual) SetFloat(name string, val float64)
- func (v *Virtual) SetInt(name string, val int)
- func (v *Virtual) SetObjectId(name string, val bson.ObjectId)
- func (v *Virtual) SetString(name string, val string)
- func (v *Virtual) SetTime(name string, val time.Time)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Document ¶
type Document struct { C *mgo.Collection Model *Model Found bool Virtual *Virtual // contains filtered or unexported fields }
func (*Document) Apply ¶
implement Apply function here it takes care of applying changes/merging to the document from another document
func (*Document) IsValid ¶
Use this method to check if this document is in fact populated with data from the database. Sleep suppresses mgo's ErrNotFound error and instead provides this interface for checking if results were returned.
func (*Document) OnCreate ¶
func (d *Document) OnCreate()
OnCreate is a stand-in method that can be implemented in the schema defination struct to be called when the document is created using Sleep.Model.CreateDoc method. Use `OnResult()` to be called then the document is queried from the database.
The method should have a reciever that is a pointer to the schema type
func (*Document) OnResult ¶
func (d *Document) OnResult()
OnResult is a stand-in method that can be implemented in the schema defination struct to be called when the document is created from the results out of the database. Use `OnCreate()` to be called then the document is created using Sleep.Model.CreateDoc method
The method should have a reciever that is a pointer to the schema type
func (*Document) Populate ¶
Same as Query.Populate() except it can be called on an existing document.
func (*Document) PopulateOne ¶
Same as populate but used to populate only a single field. Its last parameter is a pointer to the variable to hold the value of the result
func (*Document) PopulateQuery ¶
Same as Query.PopulateQuery() except the last parameter is a pointer to the variable to hold the value of the result.
func (*Document) Populated ¶
Populated gives access to the document's populated fields. This method does NOT make a database query. It returns only existing populated fields.
The path must be exactly the same as what was passed to Query.Populate() or Query.PopulateQuery() and is case sensitive.
The result parameter must be of the correct Type. For example, if the field was defined as such in the schema:
Foo: bson.ObjectId `model:"Bar"`
Then the argument must be of type *Bar Or, if the field was defined as:
Foo: []bson.ObjectId `model:"Bar"`
Then the argument must be of type: *[]*Bar
func (*Document) PostRemove ¶
func (d *Document) PostRemove()
PostRemove is a stand-in method that can be implemented in the schema defination struct to be called after the document is removed from the database.
The method should have a reciever that is a pointer to the schema type
func (*Document) PostSave ¶
func (d *Document) PostSave()
PostSave is a stand-in method that can be implemented in the schema defination struct to be called after the document is saved to the database.
The method should have a reciever that is a pointer to the schema type
func (*Document) PreRemove ¶
func (d *Document) PreRemove()
PreRemove is a stand-in method that can be implemented in the schema defination struct to be called before the document is removed from the database.
The method should have a reciever that is a pointer to the schema type
type Marango ¶
type Marango struct { Db *mgo.Database // contains filtered or unexported fields }
func (*Marango) C ¶
C gives access to the underlying *mgo.Collection value for a model. The model name is case sensitive.
func (*Marango) CreateDoc ¶
func (z *Marango) CreateDoc(doc interface{})
CreateDoc conditions an instance of the model to become a document. Will create an ObjectId for the document.
See Model.CreateDoc. They are the same
func (*Marango) Register ¶
Register registers a given schema and its corresponding collection name with Marango. All schemas MUST be registered using this function. Function will return a pointer to the Marango.Model value for this model
func (*Marango) SetModelTag ¶
SetModelTag changes the default tag key of `model` to an arbitrary key. This value is read to make relationships for populting based on ObjectIds
type Model ¶
type Model struct { *mgo.Collection //C is the underlying mgo.collection value for this model. //Refer to http://godoc.org/labix.org/v2/mgo#Collection for full usage information C *mgo.Collection // contains filtered or unexported fields }
Model struct represents a collection in MongoDB. It inherits from mgo.Collection and overwrides the functions below to provide more functionality and to create compatibility with Marango.
func (*Model) CreateDoc ¶
func (m *Model) CreateDoc(i interface{})
CreateDoc conditions an instance of the model to become a document.
What it means in pratical terms is that Create sets a value for the schema's *Marango.Document anonymous field. This will allow Marango to work with the value as a document. Calling this function is only necessary when wishing to create documents "manually". It is not necessary to call this function on a value that will be holding the result of a query; Marango will do that.
After a document is created with this function, the document will expose all of the public methods and fields of the Marango.Model struct as its own.
func (*Model) Find ¶
Find starts and returns a chainable *Query value This function passes the passed value to mgo.Collection.Find
To borrow from the mgo docs: "The document(argument) may be a map or a struct value capable of being marshalled with bson. The map may be a generic one using interface{} for its key and/or values, such as bson.M, or it may be a properly typed map. Providing nil as the document is equivalent to providing an empty document such as bson.M{}".
Further reading: http://godoc.org/labix.org/v2/mgo#Collection.Find
func (*Model) FindId ¶
FindId is a convenience function equivalent to:
query := myModel.Find(bson.M{"_id": id})
Unlike the Mgo.Collection.FindId function, this function will accept Id both in hex representation as a string or a bson.ObjectId.
FindId will return a chainable *Query value
func (*Model) RemoveId ¶
RemoveId removes a document from the collection based on its _id field. Same as mgo.Collection.RemoveId, except that it accepts the Id as a string or bson.ObjectId
func (*Model) UpdateId ¶
UpdateId updates a document in the collection based on its _id field. Same as mgo.Collection.UpdateId, except that it accepts the Id as a string or bson.ObjectId
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
func (*Query) Exec ¶
Exec executes the query.
What collection to query on is determined by the result parameter. Exec does the job of both mgo.Collection.One() and mgo.Collection.All().
Example 1 (Equivalent to mgo.Collection.One() ):
type Foo struct {...} foo := &Foo{} //foo is a pointer to the value for a single Foo struct marango.Find(bson.M{"location:": "Earth"}).Exec(foo)
Example 2 (Equivalent to mgo.Collection.All() ):
type Foo struct {...} foo := []*Foo{} //foo is the value for a slice of pointers to Foo structs marango.Find(bson.M{"location:": "Earth"}).Exec(&foo) //Another example showing further filtering marango.Find(bson.M{"location:": "Earth"}).Sort("name", "age").Limit(200).Exec(&foo)
func (*Query) Populate ¶
Populate sets the fields to be automatically populated based on the field's bson.ObjectId value.
This function takes a variable number of arguments. Each argument must be the full path to the field to be populated. The field to be populated can be either of type bson.ObjectId or []bson.ObjectId. The field must also have a tag with the key "model" and a case sensative value with the name of the model.
Example:
type Contact struct { BusinessPartner bson.ObjectId Competitors []bson.ObjectId } type Person struct { Marango.Document `bson:"-"` Name string PhoneNumber string Friend bson.ObjectId `model:"Person"` Acquaintances []bson.ObjectId `model:"Person"` Contacts []Contact } marango.FindId("...").Populate("Friend", "Acquaintances").Exec(personResult)
The path argument can also describe embeded structs. Every step into an embeded struct is seperated by a "."
Example:
marango.FindId("...").Populate("Contacts.BusinessPartner", "Contacts.Competitors").Exec(personResult)
func (*Query) PopulateQuery ¶
PopulateQuery does the same thing the Populate function does, except it only takes one field path at a time and the second parameter is a value of type *Marango.Query
Example (continuing with the Populate example):
//I want 10 acquantances that are older than 30 and sorted by their names popQuery := marango.Find(bson.M{"age": bson.M{"$gt": 30} }).Limit(10).Sort("name") marango.FindId("...").PopulateQuery("Acquaintances", popQuery).Exec(personResult)
You may also call Populate() and PopulateQuery() on the populate Query value to run auto-population on the populated results.
popQuery := marango.Find(bson.M{"age": bson.M{"$gt": 30} }).Limit(10).Sort("name").Populate("Friend")
func (*Query) Select ¶
Select enables selecting which fields should be retrieved for the results found. For example, the following query would only retrieve the name field:
err := marango.Find(bson.M{"age": 50}).Select(bson.M{"name": 1}).Exec(result)
Note 1: The _id field is always selected.. unless explicitly stated otherwise
Note 2**: If only some fields are selected for retrieval and then the Save() is called on the document, the fields not retrieved will be blank and will overwrite the database values with the default value for their respective types.
func (*Query) Skip ¶
Skip skips over the n initial documents from the query results. Using Skip only makes sense with ordered results and capped collections where documents are naturally ordered by insertion time.
func (*Query) Sort ¶
Sort sets the fields by which the database should sort the query results
Example: For example:
query1 := marango.Find(nil).Sort("firstname", "lastname") query2 := marango.Find(nil).Sort("-age") query3 := marango.Find(nil).Sort("$natural")
Further reading: http://godoc.org/labix.org/v2/mgo#Query.Sort
type Virtual ¶
type Virtual struct {
// contains filtered or unexported fields
}
Virtual holds temporary/computed values related to the document. As of right now Virtual implements getters and setters for types most commonly used in web developement. It also implements a generic getter and setter for storing and retrieving any type as type interface{} and must be asserted to its proper type upon retrieval.
These fields that are kept for the lifetime of the document in memory and are NOT persisted to the database.
func (*Virtual) Get ¶
Get returns the stored value with the given name as type interface{}. It also returns a boolean value indicating whether a value was found.
Get is a generic getter for any arbitrary type
func (*Virtual) GetBool ¶
Get returns the stored boolean value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) GetFloat ¶
GetFloat returns the stored float64 value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) GetInt ¶
GetInt returns the stored int value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) GetObjectId ¶
GetObjectId returns the stored bson.ObjectId value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) GetString ¶
GetString returns the stored string value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) GetTime ¶
GetTime returns the stored time.Time value with the given name. It also returns a boolean value indicating whether a value was found.
func (*Virtual) Set ¶
Set stores the value with the given name as type interface{}.
Set is a generic setter for any arbitrary type
func (*Virtual) SetObjectId ¶
SetObjectId stores the bson.ObjectId value with the given name.