Documentation ¶
Index ¶
- Variables
- func Hello(name string) string
- func PrintBSON(parm interface{})
- func PrintStruct(s interface{})
- type Coll
- func (c *Coll) Aggregate(pipeline interface{}, parms ...interface{}) *Cursor
- func (c *Coll) DeleteMany(filter interface{}, opts ...interface{}) *mongo.DeleteResult
- func (c *Coll) DeleteOne(filter interface{}, opts ...interface{}) *mongo.DeleteResult
- func (c *Coll) Err() error
- func (c *Coll) Find(parms ...interface{}) *Cursor
- func (c *Coll) FindOne(parms ...interface{}) *bson.D
- func (c *Coll) InsertMany(documents interface{}, opts ...interface{}) *mongo.InsertManyResult
- func (c *Coll) InsertOne(document interface{}, opts ...interface{}) *mongo.InsertOneResult
- func (c *Coll) NewCursor() *Cursor
- type Cursor
- func (c *Cursor) Close() error
- func (c *Cursor) Count() int
- func (c *Cursor) Err() error
- func (c *Cursor) ForEach(f func(*bson.D))
- func (c *Cursor) HasNext() bool
- func (c *Cursor) Limit(limitCount int64) *Cursor
- func (c *Cursor) Next() *bson.D
- func (c *Cursor) Pretty() string
- func (c *Cursor) Skip(skipCount int64) *Cursor
- func (c *Cursor) Sort(sortSequence interface{}) *Cursor
- func (c *Cursor) String() string
- func (c *Cursor) ToArray(parm ...interface{}) []bson.D
- type DB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosedCursor = errors.New("call made to closed cursor for a method that requires an open cursor")
var ErrInvalidColl = errors.New("collection not linked to a properly established db")
var ErrInvalidCursor = errors.New("cursor not linked to a properly established collection")
var ErrNotConnected = errors.New("not connected to a MongoDB")
var ErrNotConnectedDB = errors.New("not connected to a MongoDB Database")
var ErrNotFindCursor = errors.New("method call requires a Find() cursor")
Functions ¶
func Hello ¶
Hello returns a greeting for the named person.
Example ¶
fmt.Println(Hello("Doug"))
Output:
func PrintBSON ¶
func PrintBSON(parm interface{})
PrintBSON prints formatted BSON structures, optionally with the value type.
Example ¶
pipeline := `[ { "$match" : {"_id" : {"$oid":"5bf36072a5820f6e28a4736c"} }}, { "$test" : "field that will be truncated because of length" }, { "$test" : ["an array", "of text", "values"]}, { "$test" : [{"array": "of"}, {"objects": 2}]}, { "$limit": 3 } ]` var doc interface{} err := bson.UnmarshalExtJSON([]byte(pipeline), true, &doc) if err != nil { panic(err) } PrintBSON(doc)
Output: [{ $match (D): { _id (ObjectID): ObjectID("5bf36072a5820f6e28a4736c") } }, { $test (string): field that will be truncated b... }, { $test (A): [an array, of text, values] }, { $test (A): [{ array (string): of }, { objects (int32): 2 } ] }, { $limit (int32): 3 } ]
func PrintStruct ¶
func PrintStruct(s interface{})
PrintStruct prints an interface object as "pretty" json
Example ¶
// bsonD := bson.D{{Key: "bsonDKey", Value: "bsonDValue"}} bsonD := bson.D{{Key: "bsonDKey", Value: "bsonDValue"}} PrintStruct(bsonD)
Output: [ { "Key": "bsonDKey", "Value": "bsonDValue" } ]
Types ¶
type Coll ¶
type Coll struct { DB *DB MongoColl *mongo.Collection CollName string }
Coll represents a collection
func (*Coll) Aggregate ¶
Aggregate returns a cursor for an aggregation pipeline operation. The pipeline passed can be one of: []bson.D, bson.A, string If bson.A, each entry must be a bson.D If string, must be a valid JSON doc that parses to a valid bson.A
func (*Coll) DeleteMany ¶
func (c *Coll) DeleteMany(filter interface{}, opts ...interface{}) *mongo.DeleteResult
DeleteMany can delete many documents with one call as specified by the filter TODO: implement delete options.
func (*Coll) DeleteOne ¶
func (c *Coll) DeleteOne(filter interface{}, opts ...interface{}) *mongo.DeleteResult
DeleteOne deletes a single document. Note that the filter need not specify a single document but only one document will be deleted. TODO: implement delete options.
func (*Coll) Find ¶
Find returns a Cursor Parms are optional. If present, the following parms are recognized:
parms[0] - query - bson.M defines of which documents to select parms[1] - projection - bson.D defines which fields to retrieve
func (*Coll) FindOne ¶
FindOne returns a single MongoDB Document All parms are optional. If present, the following parms are recognized:
parms[0] - query - bson.M or bson.D defines of which documents to select parms[1] - projection - bson.M or bson.D defines which fields to retrieve
func (*Coll) InsertMany ¶
func (c *Coll) InsertMany(documents interface{}, opts ...interface{}) *mongo.InsertManyResult
InsertMany inserts a slice of documents into a Collection. Documents must be a slice or bson.A of bson.D documents TODO: implement insert one options
func (*Coll) InsertOne ¶
func (c *Coll) InsertOne(document interface{}, opts ...interface{}) *mongo.InsertOneResult
InsertOne inserts one document into the Collection. Document must be a bson.D or bson.M. TODO: implement insert one options
type Cursor ¶
type Cursor struct { Collection *Coll MongoCursor *mongo.Cursor IsClosed bool IsFindCursor bool NextDoc *bson.D Filter interface{} FindOptions options.FindOptions AggrPipeline interface{} AggrOptions options.AggregateOptions }
Cursor represents a cursor for a Collection
func (*Cursor) Close ¶
Close closes a cursor Note that it's possible to reuse the cursor, though not recommended?
func (*Cursor) ForEach ¶
ForEach calls the specified function once for each remaining cursor document passing the function a bson.D document.
func (*Cursor) Next ¶
Next returns the next document for the cursor as a bson.D struct. TODO: allow a struct to be passed similar to cursor.ToArray(...)
func (*Cursor) Pretty ¶
Pretty returns a pretty string version of the remaining documents for a cursor. Unlike String() it shows the bson.D as key:value instead of {Key:key Value:value}
func (*Cursor) Skip ¶
Skip specifies the number of documents to skip before returning the first document
func (*Cursor) String ¶
String fulfills the Stringer interface. Calling this will return a string containing the "pretty" print contents of the ToArray() function. ToArray() returns an array with all of the documents remaining for the cursor and closes the cursor.
func (*Cursor) ToArray ¶
ToArray returns all of the remaining documents for a cursor in a bson.D slice. NOTE: currently seems to return all docs even those already read via a cursor.Next() call. Optional parm is a pointer to a slice which typically would contain a custom struct or bson.A struct. In this case, ToArray returns an empty []bson.D slice. This may change at some future date but currently it is difficult to deal with a slice of any type. For consistency with Mongo Shell ToArray should return a slice. However we also need the ability to return all of the documents to a slice which is a custom struct or a bson.A struct.
type DB ¶
DB struct defines the fields necessary to track the state of a connection to a MongoDB server
func (*DB) Coll ¶
Coll returns a collection for a given name If there was a previous error don't set coll.MongoColl
func (*DB) Disconnect ¶
func (mg *DB) Disconnect()
Disconnect disconnects the MongoDB and cleans up any other resources, resetting the MonGolang structure
func (*DB) InitMonGolang ¶
InitMonGolang initializes the connection to the MongoDB Database
func (*DB) ShowCollections ¶
ShowCollections returns a list of collections for current Database