MongoCollectionExtension.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package org.ssssssss.magicapi.modules;
  2. import com.mongodb.client.FindIterable;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.model.UpdateOptions;
  5. import org.bson.Document;
  6. import org.ssssssss.script.annotation.Comment;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.stream.Collectors;
  11. /**
  12. * MongoCollection方法扩展
  13. */
  14. public class MongoCollectionExtension {
  15. @Comment("执行批量插入操作")
  16. public void insert(MongoCollection<Document> collection, @Comment("要插入的集合") List<Map<String, Object>> maps) {
  17. collection.insertMany(maps.stream().map(Document::new).collect(Collectors.toList()));
  18. }
  19. @Comment("执行单条插入操作")
  20. public void insert(MongoCollection<Document> collection, @Comment("执行插入数据") Map<String, Object> map) {
  21. insert(collection, Collections.singletonList(map));
  22. }
  23. @Comment("执行查询操作")
  24. public FindIterable<Document> find(MongoCollection<Document> collection, @Comment("查询条件") Map<String, Object> query) {
  25. return collection.find(new Document(query));
  26. }
  27. @Comment("修改操作,返回修改数量")
  28. public long update(MongoCollection<Document> collection, @Comment("查询条件") Map<String, Object> query, @Comment("修改值") Map<String, Object> update) {
  29. return collection.updateOne(new Document(query), new Document(update)).getModifiedCount();
  30. }
  31. @Comment("批量修改,返回修改数量")
  32. public long updateMany(MongoCollection<Document> collection, @Comment("修改条件") Map<String, Object> query, @Comment("修改值") Map<String, Object> update) {
  33. return collection.updateMany(new Document(query), new Document(update)).getModifiedCount();
  34. }
  35. @Comment("批量修改,返回修改数量")
  36. public long updateMany(MongoCollection<Document> collection, @Comment("查询条件") Map<String, Object> query, @Comment("修改值") Map<String, Object> update, Map<String, Object> filters) {
  37. UpdateOptions updateOptions = new UpdateOptions();
  38. if (filters != null && !filters.isEmpty()) {
  39. Object upsert = filters.get("upsert");
  40. if (upsert != null) {
  41. filters.remove("upsert");
  42. updateOptions.upsert(Boolean.parseBoolean(upsert.toString()));
  43. }
  44. Object bypassDocumentValidation = filters.get("bypassDocumentValidation");
  45. if (bypassDocumentValidation != null) {
  46. filters.remove("bypassDocumentValidation");
  47. updateOptions.bypassDocumentValidation(Boolean.parseBoolean(bypassDocumentValidation.toString()));
  48. }
  49. List<Document> arrayFilters = filters.entrySet().stream().map(entry -> new Document(entry.getKey(), entry.getValue())).collect(Collectors.toList());
  50. updateOptions.arrayFilters(arrayFilters);
  51. }
  52. return collection.updateMany(new Document(query), new Document(update), updateOptions).getModifiedCount();
  53. }
  54. @Comment("查询数量")
  55. public long count(MongoCollection<Document> collection, @Comment("查询") Map<String, Object> query) {
  56. return collection.countDocuments(new Document(query));
  57. }
  58. @Comment("批量删除,返回删除条数")
  59. public long remove(MongoCollection<Document> collection, @Comment("删除条件") Map<String, Object> query) {
  60. return collection.deleteMany(new Document(query)).getDeletedCount();
  61. }
  62. @Comment("删除一条,返回删除条数")
  63. public long removeOne(MongoCollection<Document> collection, @Comment("删除条件") Map<String, Object> query) {
  64. return collection.deleteOne(new Document(query)).getDeletedCount();
  65. }
  66. }