|
|
|
@ -9,59 +9,59 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Database 设置库名
|
|
|
|
|
func (app *app) Database(database string) *app {
|
|
|
|
|
app.databaseName = database
|
|
|
|
|
return app
|
|
|
|
|
func (c *Client) Database(database string) *Client {
|
|
|
|
|
c.DatabaseName = database
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collection 设置表名
|
|
|
|
|
func (app *app) Collection(collection string) *app {
|
|
|
|
|
app.collectionName = collection
|
|
|
|
|
return app
|
|
|
|
|
func (c *Client) Collection(collection string) *Client {
|
|
|
|
|
c.collectionName = collection
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Model 传入模型自动获取库名和表名
|
|
|
|
|
func (app *app) Model(value interface{}) *app {
|
|
|
|
|
func (c *Client) Model(value interface{}) *Client {
|
|
|
|
|
// https://studygolang.com/articles/896
|
|
|
|
|
val := reflect.ValueOf(value)
|
|
|
|
|
if methodValue := val.MethodByName("Database"); methodValue.IsValid() {
|
|
|
|
|
app.databaseName = methodValue.Call(nil)[0].String()
|
|
|
|
|
c.DatabaseName = methodValue.Call(nil)[0].String()
|
|
|
|
|
}
|
|
|
|
|
if methodValue := val.MethodByName("TableName"); methodValue.IsValid() {
|
|
|
|
|
app.collectionName = methodValue.Call(nil)[0].String()
|
|
|
|
|
c.collectionName = methodValue.Call(nil)[0].String()
|
|
|
|
|
}
|
|
|
|
|
return app
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (app *app) Session() (session mongo.Session, err error) {
|
|
|
|
|
session, err = app.db.StartSession()
|
|
|
|
|
func (c *Client) Session() (session mongo.Session, err error) {
|
|
|
|
|
session, err = c.Db.StartSession()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertOne 插入单个文档
|
|
|
|
|
func (app *app) InsertOne(value interface{}) (result *mongo.InsertOneResult, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) InsertOne(value interface{}) (result *mongo.InsertOneResult, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err = collection.InsertOne(context.TODO(), value)
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertMany 插入多个文档
|
|
|
|
|
func (app *app) InsertMany(values []interface{}) (result *mongo.InsertManyResult, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) InsertMany(values []interface{}) (result *mongo.InsertManyResult, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err = collection.InsertMany(context.TODO(), values)
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete 删除文档
|
|
|
|
|
func (app *app) Delete(filter interface{}) (int64, error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) Delete(filter interface{}) (int64, error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
count, err := collection.DeleteOne(context.TODO(), filter, nil)
|
|
|
|
|
return count.DeletedCount, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteMany 删除多个文档
|
|
|
|
|
func (app *app) DeleteMany(key string, value interface{}) (int64, error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) DeleteMany(key string, value interface{}) (int64, error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
filter := bson.D{{key, value}}
|
|
|
|
|
count, err := collection.DeleteMany(context.TODO(), filter)
|
|
|
|
|
return count.DeletedCount, err
|
|
|
|
@ -72,8 +72,8 @@ func (app *app) DeleteMany(key string, value interface{}) (int64, error) {
|
|
|
|
|
// 字段增加值 inc($inc)
|
|
|
|
|
// 从数组中增加一个元素 push($push)
|
|
|
|
|
// 从数组中删除一个元素 pull($pull)
|
|
|
|
|
func (app *app) UpdateOne(filter, update interface{}) (int64, error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) UpdateOne(filter, update interface{}) (int64, error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err := collection.UpdateOne(context.TODO(), filter, update)
|
|
|
|
|
return result.UpsertedCount, err
|
|
|
|
|
}
|
|
|
|
@ -83,43 +83,43 @@ func (app *app) UpdateOne(filter, update interface{}) (int64, error) {
|
|
|
|
|
// 字段增加值 inc($inc)
|
|
|
|
|
// 从数组中增加一个元素 push($push)
|
|
|
|
|
// 从数组中删除一个元素 pull($pull)
|
|
|
|
|
func (app *app) UpdateMany(filter, update interface{}) (int64, error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) UpdateMany(filter, update interface{}) (int64, error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err := collection.UpdateMany(context.TODO(), filter, update)
|
|
|
|
|
return result.UpsertedCount, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find 查询
|
|
|
|
|
func (app *app) Find(filter interface{}, opts ...*options.FindOptions) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) Find(filter interface{}, opts ...*options.FindOptions) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err = collection.Find(context.TODO(), filter, opts...)
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindOne 查询单个文档
|
|
|
|
|
func (app *app) FindOne(filter interface{}) (result *mongo.SingleResult) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) FindOne(filter interface{}) (result *mongo.SingleResult) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result = collection.FindOne(context.TODO(), filter)
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindMany 查询多个文档
|
|
|
|
|
func (app *app) FindMany(filter interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) FindMany(filter interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err = collection.Find(context.TODO(), filter)
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindManyByFilters 多条件查询
|
|
|
|
|
func (app *app) FindManyByFilters(filter interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection, err := app.db.Database(app.databaseName).Collection(app.collectionName).Clone()
|
|
|
|
|
func (c *Client) FindManyByFilters(filter interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection, err := c.Db.Database(c.DatabaseName).Collection(c.collectionName).Clone()
|
|
|
|
|
result, err = collection.Find(context.TODO(), bson.M{"$and": filter})
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindManyByFiltersSort 多条件查询支持排序
|
|
|
|
|
func (app *app) FindManyByFiltersSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection, err := app.db.Database(app.databaseName).Collection(app.collectionName).Clone()
|
|
|
|
|
func (c *Client) FindManyByFiltersSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection, err := c.Db.Database(c.DatabaseName).Collection(c.collectionName).Clone()
|
|
|
|
|
findOptions := options.Find()
|
|
|
|
|
findOptions.SetSort(Sort)
|
|
|
|
|
result, err = collection.Find(context.TODO(), filter, findOptions)
|
|
|
|
@ -127,8 +127,8 @@ func (app *app) FindManyByFiltersSort(filter interface{}, Sort interface{}) (res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindCollection 查询集合文档
|
|
|
|
|
func (app *app) FindCollection(Limit int64) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) FindCollection(Limit int64) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
findOptions := options.Find()
|
|
|
|
|
findOptions.SetLimit(Limit)
|
|
|
|
|
result, err = collection.Find(context.TODO(), bson.D{{}}, findOptions)
|
|
|
|
@ -136,8 +136,8 @@ func (app *app) FindCollection(Limit int64) (result *mongo.Cursor, err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindCollectionSort 查询集合文档支持排序
|
|
|
|
|
func (app *app) FindCollectionSort(Sort interface{}, Limit int64) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) FindCollectionSort(Sort interface{}, Limit int64) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
findOptions := options.Find()
|
|
|
|
|
findOptions.SetSort(Sort)
|
|
|
|
|
findOptions.SetLimit(Limit)
|
|
|
|
@ -146,8 +146,8 @@ func (app *app) FindCollectionSort(Sort interface{}, Limit int64) (result *mongo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindManyCollectionSort 查询集合文档支持排序支持条件
|
|
|
|
|
func (app *app) FindManyCollectionSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) FindManyCollectionSort(filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
findOptions := options.Find()
|
|
|
|
|
findOptions.SetSort(Sort)
|
|
|
|
|
result, err = collection.Find(context.TODO(), filter, findOptions)
|
|
|
|
@ -155,8 +155,8 @@ func (app *app) FindManyCollectionSort(filter interface{}, Sort interface{}) (re
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CollectionCount 查询集合里有多少数据
|
|
|
|
|
func (app *app) CollectionCount() (name string, size int64) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) CollectionCount() (name string, size int64) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
name = collection.Name()
|
|
|
|
|
size, _ = collection.EstimatedDocumentCount(context.TODO())
|
|
|
|
|
return name, size
|
|
|
|
@ -166,8 +166,8 @@ func (app *app) CollectionCount() (name string, size int64) {
|
|
|
|
|
// Skip 跳过
|
|
|
|
|
// Limit 读取数量
|
|
|
|
|
// sort 1 ,-1 . 1 为升序 , -1 为降序
|
|
|
|
|
func (app *app) CollectionDocuments(Skip, Limit int64, sort int, key string, value interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) CollectionDocuments(Skip, Limit int64, sort int, key string, value interface{}) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
SORT := bson.D{{"_id", sort}}
|
|
|
|
|
filter := bson.D{{key, value}}
|
|
|
|
|
findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
|
|
|
|
@ -176,15 +176,15 @@ func (app *app) CollectionDocuments(Skip, Limit int64, sort int, key string, val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AggregateByFiltersSort 统计分析
|
|
|
|
|
func (app *app) AggregateByFiltersSort(pipeline interface{}, opts ...*options.AggregateOptions) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) AggregateByFiltersSort(pipeline interface{}, opts ...*options.AggregateOptions) (result *mongo.Cursor, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
result, err = collection.Aggregate(context.TODO(), pipeline, opts...)
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountDocumentsByFilters 统计数量
|
|
|
|
|
func (app *app) CountDocumentsByFilters(filter interface{}) (count int64, err error) {
|
|
|
|
|
collection := app.db.Database(app.databaseName).Collection(app.collectionName)
|
|
|
|
|
func (c *Client) CountDocumentsByFilters(filter interface{}) (count int64, err error) {
|
|
|
|
|
collection := c.Db.Database(c.DatabaseName).Collection(c.collectionName)
|
|
|
|
|
count, err = collection.CountDocuments(context.TODO(), filter)
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|