Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
type Record struct {
Hello string `bson:"hello"`
}
record := new(Record)
err = mgoConnector.GetOne(TEST_COLLECTION, bson.M{
"hello": "test",
}, record)
fmt.Println((*record).Hello)type MongoConnector struct {
Connect func(mongoUrl string, databaseName *string) error
InsertOne func(collectionName string, entity interface{}) error
UpdateOne func(collectionName string, findPredicate bson.M, updatePredicate bson.M) error
UpdateAll func(collectionName string, findPredicate bson.M, updatePredicate bson.M) (*mgo.ChangeInfo, error)
GetOne func(collectionName string, findPredicate bson.M, structToDeserialize interface{}) error
GetAll func(collectionName string, findPredicate bson.M, structToDeserialize interface{}) error
DropCollection func(collectionName string) error
Disconnect func()
}.UpdateOne(nil,
bson.NewDocument(bson.EC.String("_id", access.UserID)),
bson.NewDocument(
bson.EC.SubDocumentFromElements("$addToSet",
bson.EC.SubDocumentFromElements("documents",
bson.EC.Array("$each", documents),
),
),
bson.EC.SubDocumentFromElements("$setOnInsert",
bson.EC.String("_id", access.UserID),
),
),
updateopt.Upsert(true),
)
partialUpdate := MyCustomStruct { Note: "New Note" }
update:= bson.D { {"$set" , &partialUpdate}}
filter := MyCustomStruct{ ID: "some-id" }
collection.UpdateOne(context.TODO(), filter, update)
MongoDB Go Driver туториал