|
| 1 | +package com.hungcdev.mongodb.query; |
| 2 | + |
| 3 | +import com.hungcdev.mongodb.data.Post; |
| 4 | +import com.hungcdev.mongodb.insertion.InsertDocument; |
| 5 | +import com.mongodb.client.model.Filters; |
| 6 | +import org.bson.conversions.Bson; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +public class QueryDocumentExample { |
| 12 | + |
| 13 | +public static void main(String[] args) { |
| 14 | +// Thong tin cau hinh de ket noi MongoDB |
| 15 | +String IP = "localhost"; |
| 16 | +int port = 27017; |
| 17 | +String databaseName = "HungcDev"; |
| 18 | +String collection = "Post"; |
| 19 | + |
| 20 | +/* Khoi tao du lieu */ |
| 21 | +initDataExample(IP, port, databaseName, collection); |
| 22 | + |
| 23 | +/* Tim kiem tat ca document Post trong Collection */ |
| 24 | +findAllExample(IP, port, databaseName, collection); |
| 25 | + |
| 26 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Equals */ |
| 27 | +findEqualsExample(IP, port, databaseName, collection); |
| 28 | + |
| 29 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Not Equals */ |
| 30 | +findNotEqualsExample(IP, port, databaseName, collection); |
| 31 | + |
| 32 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh In */ |
| 33 | +findInExample(IP, port, databaseName, collection); |
| 34 | + |
| 35 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Not In */ |
| 36 | +findNotInExample(IP, port, databaseName, collection); |
| 37 | + |
| 38 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Greater Than */ |
| 39 | +findGreaterThanExample(IP, port, databaseName, collection); |
| 40 | + |
| 41 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Greater Than Or Equals */ |
| 42 | +findGreaterThanOrEqualsExample(IP, port, databaseName, collection); |
| 43 | + |
| 44 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Less than */ |
| 45 | +findLessThanExample(IP, port, databaseName, collection); |
| 46 | + |
| 47 | +/* Tim kiem document Post trong Collection voi dieu kien so sanh Less than or Equals */ |
| 48 | +findLessThanOrEqualsExample(IP, port, databaseName, collection); |
| 49 | + |
| 50 | +/* Tim kiem document Post trong Collection thoa man tat ca cac dieu kien */ |
| 51 | +findANDLogicExample(IP, port, databaseName, collection); |
| 52 | + |
| 53 | +/* Tim kiem document Post trong Collection bat ky mot trong cac dieu kien */ |
| 54 | +findORLogicExample(IP, port, databaseName, collection); |
| 55 | +} |
| 56 | + |
| 57 | +private static void findORLogicExample(String ip, int port, String databaseName, String collection) { |
| 58 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 59 | + |
| 60 | +/* tim kiem document voi view >= 100 && view < 300 */ |
| 61 | +Bson query = Filters.or(Filters.eq("user", "Hungcdev"), Filters.in("title", List.of("Spring", "Java"))); |
| 62 | + |
| 63 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 64 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 65 | +postList.forEach(post -> System.out.println(post.toString())); |
| 66 | +System.out.println("--- End ---"); |
| 67 | +} |
| 68 | + |
| 69 | +private static void findANDLogicExample(String ip, int port, String databaseName, String collection) { |
| 70 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 71 | + |
| 72 | +/* tim kiem document voi view >= 100 && view < 300 */ |
| 73 | +Bson query = Filters.and(Filters.gte("view", 100), Filters.lt("view", 300)); |
| 74 | + |
| 75 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 76 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 77 | +postList.forEach(post -> System.out.println(post.toString())); |
| 78 | +System.out.println("--- End ---"); |
| 79 | +} |
| 80 | + |
| 81 | +private static void findLessThanOrEqualsExample(String ip, int port, String databaseName, String collection) { |
| 82 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 83 | + |
| 84 | +/* tim kiem document voi view <= 300 */ |
| 85 | +Bson query = Filters.lte("view", 300); |
| 86 | + |
| 87 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 88 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 89 | +postList.forEach(post -> System.out.println(post.toString())); |
| 90 | +System.out.println("--- End ---"); |
| 91 | +} |
| 92 | + |
| 93 | +private static void findLessThanExample(String ip, int port, String databaseName, String collection) { |
| 94 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 95 | + |
| 96 | +/* tim kiem document voi view < 300 */ |
| 97 | +Bson query = Filters.lt("view", 300); |
| 98 | + |
| 99 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 100 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 101 | +postList.forEach(post -> System.out.println(post.toString())); |
| 102 | +System.out.println("--- End ---"); |
| 103 | +} |
| 104 | + |
| 105 | +private static void findGreaterThanOrEqualsExample(String ip, int port, String databaseName, String collection) { |
| 106 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 107 | + |
| 108 | +/* tim kiem document voi view >= 300 */ |
| 109 | +Bson query = Filters.gte("view", 300); |
| 110 | + |
| 111 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 112 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 113 | +postList.forEach(post -> System.out.println(post.toString())); |
| 114 | +System.out.println("--- End ---"); |
| 115 | +} |
| 116 | + |
| 117 | +private static void findGreaterThanExample(String ip, int port, String databaseName, String collection) { |
| 118 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 119 | + |
| 120 | +/* tim kiem document voi view > 300 */ |
| 121 | +Bson query = Filters.gt("view", 300); |
| 122 | + |
| 123 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 124 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 125 | +postList.forEach(post -> System.out.println(post.toString())); |
| 126 | +System.out.println("--- End ---"); |
| 127 | +} |
| 128 | + |
| 129 | +private static void findNotInExample(String ip, int port, String databaseName, String collection) { |
| 130 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 131 | + |
| 132 | +/* tim kiem document voi title khong nam trong danh sach {Mongodb, Spring, Java}*/ |
| 133 | +Bson query = Filters.nin("title",List.of("Mongodb", "Spring", "Java")); |
| 134 | + |
| 135 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 136 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 137 | +postList.forEach(post -> System.out.println(post.toString())); |
| 138 | +System.out.println("--- End ---"); |
| 139 | +} |
| 140 | + |
| 141 | +private static void findInExample(String ip, int port, String databaseName, String collection) { |
| 142 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 143 | + |
| 144 | +/* tim kiem document voi title nam trong danh sach {Mongodb, Spring, Java}*/ |
| 145 | +Bson query = Filters.in("title",List.of("Mongodb", "Spring", "Java")); |
| 146 | + |
| 147 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 148 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 149 | +postList.forEach(post -> System.out.println(post.toString())); |
| 150 | +System.out.println("--- End ---"); |
| 151 | +} |
| 152 | + |
| 153 | +private static void findNotEqualsExample(String ip, int port, String databaseName, String collection) { |
| 154 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 155 | + |
| 156 | +/* tim kiem document voi user khac gia tri Hungcdev */ |
| 157 | +Bson query = Filters.ne("user","Hungcdev"); |
| 158 | + |
| 159 | +/* tim kiem document voi enable khac gia tri true */ |
| 160 | +// Bson query = Filters.ne("enable",true); |
| 161 | + |
| 162 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 163 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 164 | +postList.forEach(post -> System.out.println(post.toString())); |
| 165 | +System.out.println("--- End ---"); |
| 166 | +} |
| 167 | + |
| 168 | +private static void findEqualsExample(String ip, int port, String databaseName, String collection) { |
| 169 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 170 | + |
| 171 | +/* tim kiem document voi id = 8cf90a70-d7cd-4b7b-995b-167daab3c059 */ |
| 172 | +// Bson query = Filters.eq("_id","8cf90a70-d7cd-4b7b-995b-167daab3c059"); |
| 173 | + |
| 174 | +/* tim kiem document voi user la Hungcdev */ |
| 175 | +// Bson query = Filters.eq("user","Hungcdev"); |
| 176 | + |
| 177 | +/* tim kiem document voi enable la true */ |
| 178 | +Bson query = Filters.eq("enable",true); |
| 179 | + |
| 180 | +List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 181 | +System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 182 | +postList.forEach(post -> System.out.println(post.toString())); |
| 183 | +System.out.println("--- End ---"); |
| 184 | +} |
| 185 | + |
| 186 | +private static void initDataExample(String ip, int port , String databaseName, String collection) { |
| 187 | +InsertDocument insertDocument = new InsertDocument(ip, port, databaseName); |
| 188 | + |
| 189 | +insertDocument.insertMany(collection, Post.class, |
| 190 | +Post.builder().id(UUID.randomUUID().toString()).title("Mongodb").user("Hungcdev").content("MongoDB Tutorial").view(100).enable(true).build(), |
| 191 | +Post.builder().id(UUID.randomUUID().toString()).title("Spring").user("Hungcdev").content("Spring Tutorial").view(200).enable(false).build(), |
| 192 | +Post.builder().id(UUID.randomUUID().toString()).title("Java").user("AtomPtit").content("Java Tutorial").view(300).enable(true).build(), |
| 193 | +Post.builder().id(UUID.randomUUID().toString()).title("Spring Boot").user("AtomPtit").content("Spring Boot Tutorial").view(400).enable(true).build(), |
| 194 | +Post.builder().id(UUID.randomUUID().toString()).title("PHP").user("HungHoi").content("PHP Tutorial").view(500).enable(true).build() |
| 195 | +); |
| 196 | +} |
| 197 | + |
| 198 | +private static void findAllExample(String ip, int port, String databaseName, String collection) { |
| 199 | +QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 200 | +List<Post> postList = queryDocument.findAll(collection, Post.class); |
| 201 | +System.out.println("--- Document trong Collection Post ---"); |
| 202 | +postList.forEach(post -> System.out.println(post.toString())); |
| 203 | +System.out.println("--- End ---"); |
| 204 | +} |
| 205 | + |
| 206 | +} |
0 commit comments