|
|
package dorm
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
)
|
|
|
|
|
|
// InsertOne 插入单个文档
|
|
|
func (c *MongoClient) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (result *mongo.InsertOneResult, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err = collection.InsertOne(ctx, document, opts...)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// InsertMany 插入多个文档
|
|
|
func (c *MongoClient) InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (result *mongo.InsertManyResult, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err = collection.InsertMany(ctx, documents, opts...)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// Delete 删除文档
|
|
|
func (c *MongoClient) Delete(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (int64, error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
count, err := collection.DeleteOne(ctx, filter, opts...)
|
|
|
return count.DeletedCount, err
|
|
|
}
|
|
|
|
|
|
// DeleteMany 删除多个文档
|
|
|
func (c *MongoClient) DeleteMany(ctx context.Context, key string, value interface{}) (int64, error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
filter := bson.D{{key, value}}
|
|
|
count, err := collection.DeleteMany(ctx, filter)
|
|
|
return count.DeletedCount, err
|
|
|
}
|
|
|
|
|
|
// UpdateOne 更新单个文档
|
|
|
// 修改字段的值($set)
|
|
|
// 字段增加值 inc($inc)
|
|
|
// 从数组中增加一个元素 push($push)
|
|
|
// 从数组中删除一个元素 pull($pull)
|
|
|
func (c *MongoClient) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (int64, error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err := collection.UpdateOne(ctx, filter, update, opts...)
|
|
|
return result.UpsertedCount, err
|
|
|
}
|
|
|
|
|
|
// UpdateMany 更新多个文档
|
|
|
// 修改字段的值($set)
|
|
|
// 字段增加值 inc($inc)
|
|
|
// 从数组中增加一个元素 push($push)
|
|
|
// 从数组中删除一个元素 pull($pull)
|
|
|
func (c *MongoClient) UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (int64, error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err := collection.UpdateMany(ctx, filter, update, opts...)
|
|
|
return result.UpsertedCount, err
|
|
|
}
|
|
|
|
|
|
// Find 查询
|
|
|
//func (c *MongoClient) 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 (c *MongoClient) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (result *mongo.SingleResult) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result = collection.FindOne(ctx, filter, opts...)
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
// FindMany 查询多个文档
|
|
|
func (c *MongoClient) FindMany(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err = collection.Find(ctx, filter, opts...)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// FindManyByFilters 多条件查询
|
|
|
func (c *MongoClient) FindManyByFilters(ctx context.Context, filter interface{}) (result *mongo.Cursor, err error) {
|
|
|
collection, err := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName).Clone()
|
|
|
result, err = collection.Find(ctx, bson.M{"$and": filter})
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// FindManyByFiltersSort 多条件查询支持排序
|
|
|
func (c *MongoClient) FindManyByFiltersSort(ctx context.Context, filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
collection, err := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName).Clone()
|
|
|
findOptions := options.Find()
|
|
|
findOptions.SetSort(Sort)
|
|
|
result, err = collection.Find(ctx, filter, findOptions)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// FindCollection 查询集合文档
|
|
|
func (c *MongoClient) FindCollection(ctx context.Context, Limit int64) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
findOptions := options.Find()
|
|
|
findOptions.SetLimit(Limit)
|
|
|
result, err = collection.Find(ctx, bson.D{{}}, findOptions)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// FindCollectionSort 查询集合文档支持排序
|
|
|
func (c *MongoClient) FindCollectionSort(ctx context.Context, Sort interface{}, Limit int64) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
findOptions := options.Find()
|
|
|
findOptions.SetSort(Sort)
|
|
|
findOptions.SetLimit(Limit)
|
|
|
result, err = collection.Find(ctx, bson.D{{}}, findOptions)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// FindManyCollectionSort 查询集合文档支持排序支持条件
|
|
|
func (c *MongoClient) FindManyCollectionSort(ctx context.Context, filter interface{}, Sort interface{}) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
findOptions := options.Find()
|
|
|
findOptions.SetSort(Sort)
|
|
|
result, err = collection.Find(ctx, filter, findOptions)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// CollectionCount 查询集合里有多少数据
|
|
|
func (c *MongoClient) CollectionCount(ctx context.Context) (name string, size int64) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
name = collection.Name()
|
|
|
size, _ = collection.EstimatedDocumentCount(ctx)
|
|
|
return name, size
|
|
|
}
|
|
|
|
|
|
// CollectionDocuments 按选项查询集合
|
|
|
// Skip 跳过
|
|
|
// Limit 读取数量
|
|
|
// sort 1 ,-1 . 1 为升序 , -1 为降序
|
|
|
func (c *MongoClient) CollectionDocuments(ctx context.Context, Skip, Limit int64, sort int, key string, value interface{}) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
SORT := bson.D{{"_id", sort}}
|
|
|
filter := bson.D{{key, value}}
|
|
|
findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
|
|
|
result, err = collection.Find(ctx, filter, findOptions)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// AggregateByFiltersSort 统计分析
|
|
|
func (c *MongoClient) AggregateByFiltersSort(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (result *mongo.Cursor, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
result, err = collection.Aggregate(ctx, pipeline, opts...)
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
// CountDocumentsByFilters 统计数量
|
|
|
func (c *MongoClient) CountDocumentsByFilters(ctx context.Context, filter interface{}) (count int64, err error) {
|
|
|
collection := c.Db.Database(c.getDatabaseName()).Collection(c.collectionName)
|
|
|
count, err = collection.CountDocuments(ctx, filter)
|
|
|
return count, err
|
|
|
}
|