Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. package mongodb
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "log"
  7. "sync"
  8. "time"
  9. "git.x2erp.com/qdy/go-base/config"
  10. "git.x2erp.com/qdy/go-base/config/subconfigs"
  11. "git.x2erp.com/qdy/go-base/logger"
  12. "git.x2erp.com/qdy/go-base/model/response"
  13. "go.mongodb.org/mongo-driver/bson"
  14. "go.mongodb.org/mongo-driver/mongo"
  15. "go.mongodb.org/mongo-driver/mongo/options"
  16. "go.mongodb.org/mongo-driver/mongo/readpref"
  17. )
  18. // MongoDBFactory MongoDB工厂(全局单例模式)
  19. type MongoDBFactory struct {
  20. client *mongo.Client
  21. db *mongo.Database
  22. config *subconfigs.MongoDBConfig
  23. mu sync.RWMutex // 添加读写锁保护
  24. }
  25. var (
  26. instanceMongodb *MongoDBFactory
  27. instanceMongodbOnce sync.Once
  28. initErr error
  29. )
  30. // CreateFactory 获取MongoDB工厂单例
  31. func CreateFactory(cfg config.IConfig) *MongoDBFactory {
  32. config := cfg.GetMongoDBConfig()
  33. instanceMongodbOnce.Do(func() {
  34. if config == nil {
  35. log.Fatal("配置未初始化,请先在yaml进行配置")
  36. }
  37. // 设置默认值
  38. if config.Timeout == 0 {
  39. config.Timeout = 30
  40. }
  41. if config.MaxPoolSize == 0 {
  42. config.MaxPoolSize = 100
  43. }
  44. if config.MinPoolSize == 0 {
  45. config.MinPoolSize = 10
  46. }
  47. if config.AuthSource == "" {
  48. config.AuthSource = "admin"
  49. }
  50. // 验证配置
  51. if config.URI == "" {
  52. initErr = fmt.Errorf("mongodb URI must be configured")
  53. return
  54. }
  55. if config.Database == "" {
  56. initErr = fmt.Errorf("mongodb database name must be configured")
  57. return
  58. }
  59. log.Printf("Creating MongoDB connection...")
  60. // 创建客户端选项
  61. clientOptions := options.Client().
  62. ApplyURI(config.URI)
  63. //SetConnectTimeout(config.Timeout).
  64. //SetSocketTimeout(config.Timeout).
  65. //SetServerSelectionTimeout(config.Timeout).
  66. //SetMaxPoolSize(config.MaxPoolSize).
  67. //SetMinPoolSize(config.MinPoolSize).
  68. //SetMaxConnIdleTime(5 * time.Minute).
  69. //SetHeartbeatInterval(10 * time.Second)
  70. // 设置认证
  71. if config.Username != "" && config.Password != "" {
  72. clientOptions.SetAuth(options.Credential{
  73. Username: config.Username,
  74. Password: config.Password,
  75. AuthSource: config.AuthSource,
  76. })
  77. }
  78. // 设置SSL
  79. if config.SSL {
  80. clientOptions.SetTLSConfig(&tls.Config{
  81. InsecureSkipVerify: false,
  82. })
  83. }
  84. // 创建上下文
  85. ctx, cancel := context.WithTimeout(context.Background(), config.GetTimeout())
  86. defer cancel()
  87. log.Printf(" context.WithTimeout ... successfully.")
  88. // 连接MongoDB
  89. client, err := mongo.Connect(ctx, clientOptions)
  90. if err != nil {
  91. initErr = fmt.Errorf("failed to connect to MongoDB: %v", err)
  92. return
  93. }
  94. log.Printf(" mongo.connect ... successfully.")
  95. // // 测试连接
  96. // if err := client.Ping(ctx, nil); err != nil {
  97. // initErr = fmt.Errorf("failed to ping MongoDB: %v", err)
  98. // return
  99. // }
  100. // 获取数据库
  101. database := client.Database(config.Database)
  102. log.Printf("MongoDBFactory is successfully created.\n")
  103. instanceMongodb = &MongoDBFactory{
  104. client: client,
  105. db: database,
  106. config: config,
  107. }
  108. })
  109. if initErr != nil {
  110. //logger.Errorf("MongoDBFactory is error:'%v'", initErr)
  111. log.Fatalf("MongoDBFactory is error:'%v'", initErr)
  112. //return nil
  113. }
  114. return instanceMongodb
  115. }
  116. func (c *MongoDBFactory) GetTimeout() time.Duration {
  117. return c.config.GetTimeout()
  118. }
  119. // TestConnection 测试连接
  120. func (f *MongoDBFactory) TestConnection() {
  121. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  122. defer cancel()
  123. if err := f.client.Ping(ctx, readpref.Primary()); err != nil {
  124. log.Printf("MongoDB连接测试失败: %v", err)
  125. } else {
  126. log.Printf("MongoDB连接测试通过.")
  127. }
  128. }
  129. // ========== MongoDBFactory 实例方法 ==========
  130. // GetClient 获取MongoDB客户端
  131. func (f *MongoDBFactory) GetClient() *mongo.Client {
  132. return f.client
  133. }
  134. // GetDatabase 获取数据库
  135. func (f *MongoDBFactory) GetDatabase() *mongo.Database {
  136. return f.db
  137. }
  138. // GetCollection 获取集合(类似获取数据库连接)
  139. func (f *MongoDBFactory) GetCollection(collectionName string) *mongo.Collection {
  140. return f.db.Collection(collectionName)
  141. }
  142. func (f *MongoDBFactory) GetName() string {
  143. return "MongoDBFactory"
  144. }
  145. // Close 关闭MongoDB连接
  146. func (f *MongoDBFactory) Close() {
  147. if f.client != nil {
  148. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  149. defer cancel()
  150. err := f.client.Disconnect(ctx)
  151. if err != nil {
  152. logger.Errorf("failed to disconnect MongoDB: %v", err)
  153. //return fmt.Errorf("failed to disconnect MongoDB: %v", err)
  154. }
  155. log.Printf("MongoDB connection closed gracefully\n")
  156. f.client = nil
  157. f.db = nil
  158. }
  159. }
  160. // GetConfig 获取配置信息
  161. func (f *MongoDBFactory) GetConfig() subconfigs.MongoDBConfig {
  162. return *f.config
  163. }
  164. // ========== 快捷操作方法(增强版) ==========
  165. // InsertOne 插入单个文档,返回是否成功
  166. func (f *MongoDBFactory) InsertOne(collectionName string, document interface{}) (*response.QueryResult[interface{}], error) {
  167. collection := f.GetCollection(collectionName)
  168. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  169. defer cancel()
  170. result, err := collection.InsertOne(ctx, document)
  171. if err != nil {
  172. logger.Errorf("插入文档失败,集合:%s,错误:%v", collectionName, err)
  173. return &response.QueryResult[interface{}]{
  174. Success: false,
  175. Error: err.Error(),
  176. }, err
  177. }
  178. return &response.QueryResult[interface{}]{
  179. Success: true,
  180. Data: result.InsertedID,
  181. }, nil
  182. }
  183. // InsertOneWithResult 插入单个文档并返回结果
  184. func (f *MongoDBFactory) InsertOneWithResult(collectionName string, document interface{}) (*mongo.InsertOneResult, bool) {
  185. collection := f.GetCollection(collectionName)
  186. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  187. defer cancel()
  188. result, err := collection.InsertOne(ctx, document)
  189. if err != nil {
  190. logger.Errorf("插入文档失败,集合:%s,错误:%v", collectionName, err)
  191. return nil, false
  192. }
  193. return result, true
  194. }
  195. // InsertMany 插入多个文档,返回是否成功
  196. func (f *MongoDBFactory) InsertMany(collectionName string, documents []interface{}) bool {
  197. collection := f.GetCollection(collectionName)
  198. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  199. defer cancel()
  200. _, err := collection.InsertMany(ctx, documents)
  201. if err != nil {
  202. logger.Errorf("批量插入文档失败,集合:%s,错误:%v", collectionName, err)
  203. return false
  204. }
  205. return true
  206. }
  207. // InsertManyWithResult 插入多个文档并返回结果
  208. func (f *MongoDBFactory) InsertManyWithResult(collectionName string, documents []interface{}) (*mongo.InsertManyResult, bool) {
  209. collection := f.GetCollection(collectionName)
  210. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  211. defer cancel()
  212. result, err := collection.InsertMany(ctx, documents)
  213. if err != nil {
  214. logger.Errorf("批量插入文档失败,集合:%s,错误:%v", collectionName, err)
  215. return nil, false
  216. }
  217. return result, true
  218. }
  219. // FindOne 查询单个文档,返回解码后的对象
  220. func (f *MongoDBFactory) FindOne(collectionName string, filter interface{}, result interface{}) error {
  221. collection := f.GetCollection(collectionName)
  222. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  223. defer cancel()
  224. err := collection.FindOne(ctx, filter).Decode(result)
  225. if err != nil {
  226. if err != mongo.ErrNoDocuments {
  227. logger.Errorf("查询文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  228. }
  229. return err
  230. }
  231. return nil
  232. }
  233. // FindOneAndDecode 查询单个文档并自动解码(简化版)
  234. func (f *MongoDBFactory) FindOneAndDecode(collectionName string, filter interface{}) (interface{}, error) {
  235. collection := f.GetCollection(collectionName)
  236. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  237. defer cancel()
  238. var result map[string]interface{}
  239. err := collection.FindOne(ctx, filter).Decode(&result)
  240. if err != nil {
  241. if err != mongo.ErrNoDocuments {
  242. logger.Errorf("查询文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  243. }
  244. return nil, err
  245. }
  246. return result, nil
  247. }
  248. // Find 查询多个文档,返回对象数组
  249. func (f *MongoDBFactory) Find(collectionName string, filter interface{}, results interface{}, opts ...*options.FindOptions) error {
  250. collection := f.GetCollection(collectionName)
  251. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  252. defer cancel()
  253. cursor, err := collection.Find(ctx, filter, opts...)
  254. if err != nil {
  255. logger.Errorf("查询文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  256. return err
  257. }
  258. defer cursor.Close(ctx)
  259. // 解码到传入的切片指针中
  260. if err = cursor.All(ctx, results); err != nil {
  261. logger.Errorf("解码查询结果失败,集合:%s,错误:%v", collectionName, err)
  262. return err
  263. }
  264. return nil
  265. }
  266. // FindAll 查询所有文档,返回对象数组
  267. func (f *MongoDBFactory) FindAll(collectionName string, results interface{}) error {
  268. return f.Find(collectionName, bson.M{}, results)
  269. }
  270. // UpdateOneWithResult 更新单个文档并返回结果
  271. func (f *MongoDBFactory) UpdateOneWithResult(collectionName string, filter interface{}, update interface{}) (*mongo.UpdateResult, bool) {
  272. collection := f.GetCollection(collectionName)
  273. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  274. defer cancel()
  275. result, err := collection.UpdateOne(ctx, filter, update)
  276. if err != nil {
  277. logger.Errorf("更新文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  278. return nil, false
  279. }
  280. return result, true
  281. }
  282. // CountDocuments 统计文档数量,返回数量
  283. func (f *MongoDBFactory) CountDocuments(collectionName string, filter interface{}) (int64, error) {
  284. collection := f.GetCollection(collectionName)
  285. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  286. defer cancel()
  287. count, err := collection.CountDocuments(ctx, filter)
  288. if err != nil {
  289. logger.Errorf("统计文档数量失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  290. return 0, err
  291. }
  292. return count, nil
  293. }
  294. // Aggregate 聚合查询,返回对象数组
  295. func (f *MongoDBFactory) Aggregate(collectionName string, pipeline interface{}, results interface{}) error {
  296. collection := f.GetCollection(collectionName)
  297. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  298. defer cancel()
  299. cursor, err := collection.Aggregate(ctx, pipeline)
  300. if err != nil {
  301. logger.Errorf("聚合查询失败,集合:%s,管道:%v,错误:%v", collectionName, pipeline, err)
  302. return err
  303. }
  304. defer cursor.Close(ctx)
  305. if err = cursor.All(ctx, results); err != nil {
  306. logger.Errorf("解码聚合结果失败,集合:%s,错误:%v", collectionName, err)
  307. return err
  308. }
  309. return nil
  310. }
  311. // FindOneAndUpdate 查找并更新,返回更新后的文档
  312. func (f *MongoDBFactory) FindOneAndUpdate(collectionName string, filter interface{}, update interface{}, result interface{}) error {
  313. collection := f.GetCollection(collectionName)
  314. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  315. defer cancel()
  316. err := collection.FindOneAndUpdate(ctx, filter, update).Decode(result)
  317. if err != nil {
  318. if err != mongo.ErrNoDocuments {
  319. logger.Errorf("查找并更新失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  320. }
  321. return err
  322. }
  323. return nil
  324. }
  325. // FindOneAndDelete 查找并删除,返回删除的文档
  326. func (f *MongoDBFactory) FindOneAndDelete(collectionName string, filter interface{}, result interface{}) error {
  327. collection := f.GetCollection(collectionName)
  328. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  329. defer cancel()
  330. err := collection.FindOneAndDelete(ctx, filter).Decode(result)
  331. if err != nil {
  332. if err != mongo.ErrNoDocuments {
  333. logger.Errorf("查找并删除失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  334. }
  335. return err
  336. }
  337. return nil
  338. }
  339. // BulkWrite 批量写入操作,返回是否成功
  340. func (f *MongoDBFactory) BulkWrite(collectionName string, operations []mongo.WriteModel) bool {
  341. collection := f.GetCollection(collectionName)
  342. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  343. defer cancel()
  344. _, err := collection.BulkWrite(ctx, operations)
  345. if err != nil {
  346. logger.Errorf("批量写入操作失败,集合:%s,错误:%v", collectionName, err)
  347. return false
  348. }
  349. return true
  350. }
  351. // CreateIndex 创建索引,返回是否成功
  352. func (f *MongoDBFactory) CreateIndex(collectionName string, keys interface{}, opts ...*options.IndexOptions) bool {
  353. collection := f.GetCollection(collectionName)
  354. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  355. defer cancel()
  356. indexModel := mongo.IndexModel{
  357. Keys: keys,
  358. }
  359. if len(opts) > 0 && opts[0] != nil {
  360. indexModel.Options = opts[0]
  361. }
  362. _, err := collection.Indexes().CreateOne(ctx, indexModel)
  363. if err != nil {
  364. logger.Errorf("创建索引失败,集合:%s,键:%v,错误:%v", collectionName, keys, err)
  365. return false
  366. }
  367. return true
  368. }
  369. // FindWithPagination 分页查询,返回对象数组
  370. func (f *MongoDBFactory) FindWithPagination(
  371. collectionName string,
  372. filter interface{},
  373. skip, limit int64,
  374. sort interface{},
  375. results interface{},
  376. ) error {
  377. //collection := f.GetCollection(collectionName)
  378. //ctx, cancel := context.WithTimeout(context.Background(), f.config.Timeout)
  379. //defer cancel()
  380. findOptions := options.Find().
  381. SetSkip(skip).
  382. SetLimit(limit)
  383. if sort != nil {
  384. findOptions.SetSort(sort)
  385. }
  386. return f.Find(collectionName, filter, results, findOptions)
  387. }
  388. // FindOneByID 根据ID查询文档
  389. func (f *MongoDBFactory) FindOneByID(collectionName string, id interface{}, result interface{}) error {
  390. return f.FindOne(collectionName, bson.M{"_id": id}, result)
  391. }
  392. // Exists 检查文档是否存在
  393. func (f *MongoDBFactory) Exists(collectionName string, filter interface{}) (bool, error) {
  394. count, err := f.CountDocuments(collectionName, filter)
  395. if err != nil {
  396. return false, err
  397. }
  398. return count > 0, nil
  399. }
  400. // FindAndCount 查询文档并返回总数(用于分页场景)
  401. func (f *MongoDBFactory) FindAndCount(
  402. collectionName string,
  403. filter interface{},
  404. skip, limit int64,
  405. sort interface{},
  406. results interface{},
  407. ) (int64, error) {
  408. // 查询数据
  409. err := f.FindWithPagination(collectionName, filter, skip, limit, sort, results)
  410. if err != nil {
  411. return 0, err
  412. }
  413. // 查询总数
  414. total, err := f.CountDocuments(collectionName, filter)
  415. if err != nil {
  416. return 0, err
  417. }
  418. return total, nil
  419. }
  420. // UpdateOne 更新单个文档,返回是否执行成功和影响记录数
  421. func (f *MongoDBFactory) UpdateOne(collectionName string, filter interface{}, update interface{}) (bool, int64) {
  422. collection := f.GetCollection(collectionName)
  423. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  424. defer cancel()
  425. result, err := collection.UpdateOne(ctx, filter, update)
  426. if err != nil {
  427. logger.Errorf("更新文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  428. return false, 0
  429. }
  430. // 返回执行成功 和 实际更新的文档数
  431. return true, result.ModifiedCount
  432. }
  433. // UpdateOneWithMatch 更新单个文档,只有匹配到文档才返回成功
  434. func (f *MongoDBFactory) UpdateOneWithMatch(collectionName string, filter interface{}, update interface{}) bool {
  435. success, modifiedCount := f.UpdateOne(collectionName, filter, update)
  436. return success && modifiedCount > 0
  437. }
  438. // UpdateMany 更新多个文档,返回是否执行成功和影响记录数
  439. func (f *MongoDBFactory) UpdateMany(collectionName string, filter interface{}, update interface{}) (bool, int64) {
  440. collection := f.GetCollection(collectionName)
  441. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  442. defer cancel()
  443. result, err := collection.UpdateMany(ctx, filter, update)
  444. if err != nil {
  445. logger.Errorf("批量更新文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  446. return false, 0
  447. }
  448. return true, result.ModifiedCount
  449. }
  450. // DeleteOne 删除单个文档,返回是否执行成功和删除的记录数
  451. func (f *MongoDBFactory) DeleteOne(collectionName string, filter interface{}) (bool, int64) {
  452. collection := f.GetCollection(collectionName)
  453. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  454. defer cancel()
  455. result, err := collection.DeleteOne(ctx, filter)
  456. if err != nil {
  457. logger.Errorf("删除文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  458. return false, 0
  459. }
  460. // 返回执行成功 和 实际删除的文档数
  461. return true, result.DeletedCount
  462. }
  463. // DeleteOneWithMatch 删除单个文档,只有删除了文档才返回成功
  464. func (f *MongoDBFactory) DeleteOneWithMatch(collectionName string, filter interface{}) bool {
  465. success, deletedCount := f.DeleteOne(collectionName, filter)
  466. return success && deletedCount > 0
  467. }
  468. // DeleteMany 删除多个文档,返回是否执行成功和删除的记录数
  469. func (f *MongoDBFactory) DeleteMany(collectionName string, filter interface{}) (bool, int64) {
  470. collection := f.GetCollection(collectionName)
  471. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  472. defer cancel()
  473. result, err := collection.DeleteMany(ctx, filter)
  474. if err != nil {
  475. logger.Errorf("批量删除文档失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  476. return false, 0
  477. }
  478. return true, result.DeletedCount
  479. }
  480. // UpsertOne 更新或插入文档(upsert操作)
  481. func (f *MongoDBFactory) UpsertOne(collectionName string, filter interface{}, update interface{}) (bool, interface{}) {
  482. collection := f.GetCollection(collectionName)
  483. ctx, cancel := context.WithTimeout(context.Background(), f.config.GetTimeout())
  484. defer cancel()
  485. // 设置 upsert 选项
  486. opts := options.Update().SetUpsert(true)
  487. result, err := collection.UpdateOne(ctx, filter, update, opts)
  488. if err != nil {
  489. logger.Errorf("Upsert操作失败,集合:%s,过滤条件:%v,错误:%v", collectionName, filter, err)
  490. return false, result
  491. }
  492. // 返回: 成功状态, 匹配数, 修改数, 插入数
  493. return true, result
  494. }
  495. // UpdateOneByID 根据ID更新文档
  496. func (f *MongoDBFactory) UpdateOneByID(collectionName string, id interface{}, update interface{}) (bool, int64) {
  497. return f.UpdateOne(collectionName, bson.M{"_id": id}, update)
  498. }
  499. // UpdateOneByIDWithMatch 根据ID更新文档,只有匹配到才返回成功
  500. func (f *MongoDBFactory) UpdateOneByIDWithMatch(collectionName string, id interface{}, update interface{}) bool {
  501. success, modifiedCount := f.UpdateOneByID(collectionName, id, update)
  502. return success && modifiedCount > 0
  503. }
  504. // DeleteOneByID 根据ID删除文档
  505. func (f *MongoDBFactory) DeleteOneByID(collectionName string, id interface{}) (bool, int64) {
  506. return f.DeleteOne(collectionName, bson.M{"_id": id})
  507. }
  508. // DeleteOneByIDWithMatch 根据ID删除文档,只有删除了才返回成功
  509. func (f *MongoDBFactory) DeleteOneByIDWithMatch(collectionName string, id interface{}) bool {
  510. success, deletedCount := f.DeleteOneByID(collectionName, id)
  511. return success && deletedCount > 0
  512. }