mongodb 官方的go驱动包 go.mongodb.org/mongo-driver 使用起来比较繁琐,最近对其进行了二次封装
github地址:https://github.com/w3liu/go-common/tree/master/store/mongo
测试用例如下:
func TestFindOne(t *testing.T) {
col := &demoCollection{}
filter := bson.D{
{"title", "什么是Lambda架构"},
}
opt := options.FindOne().SetSort(bson.D{{"_id", -1}})
finder := NewOneFinder(col).Where(filter).Options(opt)
b, err := store.FindOne(context.TODO(), finder)
if err != nil {
t.Fatal(err)
}
t.Log("b", b)
t.Logf("%#v", col)
}
func TestFindMany(t *testing.T) {
col := &demoCollection{}
filter := bson.D{
//{"title", "什么是Lambda架构"},
}
opt := options.Find().SetSort(bson.D{{"_id", -1}})
records := make([]*demoCollection, 0)
finder := NewFinder(col).Where(filter).Options(opt).Records(&records)
err := store.FindMany(context.TODO(), finder)
if err != nil {
t.Fatal(err)
}
for \_, item := range records {
t.Log(item)
}
}
func TestInsertOne(t *testing.T) {
err := store.InsertOne(context.TODO(), defaultCollection)
if err != nil {
t.Fatal(err)
}
t.Log("id", defaultCollection.Id.Hex())
}
func TestDeleteOne(t *testing.T) {
id, _ := primitive.ObjectIDFromHex("5f6183a9ed076ced7eacec3a")
col := &demoCollection{
Id: id,
}
cnt, err := store.DeleteOne(context.TODO(), col)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
func TestUpdateOne(t *testing.T) {
id, _ := primitive.ObjectIDFromHex("5f618414c978e349ced0c81f")
col := &demoCollection{
Id: id,
}
filter := bson.D{
{"_id", id},
}
update := bson.D{
{"title", "什么是Lambda架构?"},
}
updater := NewUpdater(col).Where(filter).Update(update)
cnt, err := store.UpdateOne(context.TODO(), updater)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
func TestInsertMany(t *testing.T) {
cols := make([]*demoCollection, 0)
title := defaultCollection.Title
for i := 0; i < 10; i++ {
item := &demoCollection{
Title: fmt.Sprintf("%s_%d", title, i),
Author: defaultCollection.Author,
Content: defaultCollection.Content,
Status: 1,
CreatedAt: time.Now(),
}
cols = append(cols, item)
}
docs := make(\[\]interface{}, 0)
for i := 0; i < len(cols); i++ {
docs = append(docs, cols\[i\])
}
err := store.InsertMany(context.TODO(), docs)
if err != nil {
t.Fatal(err)
}
for i := 0; i < len(cols); i++ {
t.Log("id", cols\[i\].GetId())
t.Log("title", cols\[i\].Title)
}
}
func TestDeleteMany(t *testing.T) {
//filter := bson.D{
// {"title", "什么是Lambda架构"},
//}
deleter := NewDeleter(defaultCollection).Where(nil)
cnt, err := store.DeleteMany(context.TODO(), deleter)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
func TestUpdateMany(t *testing.T) {
filter := bson.D{
{"author", "数据社"},
}
update := bson.D{
{"title", "什么是Lambda架构"},
}
updater := NewUpdater(defaultCollection).Where(filter).Update(update)
cnt, err := store.UpdateMany(context.TODO(), updater)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
func TestAggregate(t *testing.T) {
var records \[\]\*struct {
Total int \`bson:"total"\`
Count int \`bson:"count"\`
}
match := bson.D{
{"$match", bson.D{
{"author", "数据社1"},
}},
}
group := bson.D{
{"$group", bson.D{
{"\_id", nil},
{"total", bson.M{"$sum": "$status"}},
{"count", bson.M{"$sum": 1}},
}},
}
aggregator := NewAggregator(defaultCollection).Stage(match).Stage(group).Records(&records)
err := store.Aggregate(context.TODO(), aggregator)
if err != nil {
t.Fatal(err)
}
if len(records) > 0 {
t.Log(records\[0\])
}
}
func TestCountDocuments(t *testing.T) {
//filter := bson.D{
// {"author", "数据社1"},
//}
counter := NewCounter(defaultCollection).Where(nil)
cnt, err := store.CountDocuments(context.TODO(), counter)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
func TestCountEstimateDocuments(t *testing.T) {
counter := NewEstimateCounter(defaultCollection)
cnt, err := store.CountEstimateDocuments(context.TODO(), counter)
if err != nil {
t.Fatal(err)
}
t.Log("cnt", cnt)
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章