Documentation
¶
Overview ¶
Example (CosmosDBOperations) ¶
var groupName = config.GenerateGroupName("CosmosDB")
config.SetGroupName(groupName)
ctx := context.Background()
defer resources.Cleanup(ctx)
_, err := resources.CreateGroup(ctx, config.GroupName())
if err != nil {
util.LogAndPanic(err)
}
_, err = CreateDatabaseAccount(ctx, accountName)
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot create database account: %+v", err))
}
util.PrintAndLog("database account created")
keys, err := ListKeys(ctx, accountName)
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot list keys: %+v", err))
}
util.PrintAndLog("keys listed")
host := fmt.Sprintf("%s.documents.azure.com", accountName)
collection := "Packages"
session, err := mongodb.NewMongoDBClientWithCredentials(accountName, *keys.PrimaryMasterKey, host)
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot get mongoDB session: %+v", err))
}
util.PrintAndLog("got mongoDB session")
GetCollection(session, accountName, collection)
util.PrintAndLog("got collection")
err = InsertDocument(
session,
accountName,
collection,
map[string]interface{}{
"fullname": "react",
"description": "A framework for building native apps with React.",
"forksCount": 11392,
"StarsCount": 48794,
"LastUpdatedBy": "shergin",
})
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot insert document: %v", err))
}
util.PrintAndLog("inserted document")
doc, err := GetDocument(
session,
accountName,
collection,
bson.M{"fullname": "react"})
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot get document: %v", err))
}
util.PrintAndLog("got document")
util.PrintAndLog(fmt.Sprintf("document description: %s", doc["description"]))
err = UpdateDocument(
session,
accountName,
collection,
doc["_id"].(bson.ObjectId),
bson.M{
"$set": bson.M{
"fullname": "react-native",
},
})
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot update document: %v", err))
}
util.PrintAndLog("update document")
err = DeleteDocument(session, accountName, collection, doc["_id"].(bson.ObjectId))
if err != nil {
util.LogAndPanic(fmt.Errorf("cannot delete document: %v", err))
}
util.PrintAndLog("delete document")
Output: database account created keys listed got mongoDB session got collection inserted document got document document description: A framework for building native apps with React. update document delete document
Index ¶
- func CreateDatabaseAccount(ctx context.Context, accountName string) (dba documentdb.DatabaseAccount, err error)
- func DeleteDocument(session *mgo.Session, database, collectionName string, id bson.ObjectId) error
- func GetCollection(session *mgo.Session, database, collectionName string) *mgo.Collection
- func GetDocument(session *mgo.Session, database, collectionName string, query bson.M) (result map[string]interface{}, err error)
- func InsertDocument(session *mgo.Session, database, collectionName string, ...) error
- func ListKeys(ctx context.Context, accountName string) (documentdb.DatabaseAccountListKeysResult, error)
- func UpdateDocument(session *mgo.Session, database, collectionName string, id bson.ObjectId, ...) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateDatabaseAccount ¶
func CreateDatabaseAccount(ctx context.Context, accountName string) (dba documentdb.DatabaseAccount, err error)
CreateDatabaseAccount creates or updates an Azure Cosmos DB database account.
func DeleteDocument ¶
DeleteDocument deletes the mongoDB document with the specified ID
func GetCollection ¶
func GetCollection(session *mgo.Session, database, collectionName string) *mgo.Collection
GetCollection gets a mongoDB collection
func GetDocument ¶
func GetDocument(session *mgo.Session, database, collectionName string, query bson.M) (result map[string]interface{}, err error)
GetDocument gets a mongoDB document in the specified collection
func InsertDocument ¶
func InsertDocument(session *mgo.Session, database, collectionName string, document map[string]interface{}) error
InsertDocument inserts a mongoDB document in the specified collection
func ListKeys ¶
func ListKeys(ctx context.Context, accountName string) (documentdb.DatabaseAccountListKeysResult, error)
ListKeys gets the keys for a Azure Cosmos DB database account.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.