database
v0.0.0-20230630043320-433fd72654f0
github.com/ioVN/database
Documentation
pkg.go.dev ↗Functions
MongoConnect
func MongoConnect(uri, dbname string, timeout time.Duration) (*mongo.Database, error)
MongoConnect : create a new connection to mongodb
Example :
db, err := MongoConnect("mongodb://localhost:27017/testDB", "testDB", 30*time.Second)
if err != nil {
log.Fatal(err)
}
if _, err := db.ListCollectionSpecifications(ctx, bson.M{}); err != nil {
log.Fatal(err)
}
MongoInit
func MongoInit(db *mongo.Database, collectionName string, index ...MongoIndex) *mongo.Collection
MongoInit : make collection with indexes
ParseMongoURI
func ParseMongoURI(uri string) (dbURI, dbname string)
Types
MongoIndex
type MongoIndex struct {
Keys interface{}
Unique bool
}
MakeMongoIndex
func MakeMongoIndex(keys bson.D, unique bool) MongoIndex
MakeMongoIndex - make MongoIndex by keys