|
| 1 | +package com.hungcdev.mongodb.query; |
| 2 | + |
| 3 | +import com.hungcdev.mongodb.data.Comment; |
| 4 | +import com.hungcdev.mongodb.data.Post; |
| 5 | +import com.hungcdev.mongodb.insertion.InsertDocument; |
| 6 | +import com.mongodb.client.model.Filters; |
| 7 | +import org.bson.conversions.Bson; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.UUID; |
| 11 | + |
| 12 | +public class QueryArrayEmbeddedDocumentExample { |
| 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 | + queryArrayEmbeddedDocument1(IP, port, databaseName,collection); |
| 24 | + queryArrayEmbeddedDocument2(IP, port, databaseName,collection); |
| 25 | + queryArrayEmbeddedDocument3(IP, port, databaseName,collection); |
| 26 | + queryArrayEmbeddedDocument4(IP, port, databaseName,collection); |
| 27 | + queryArrayEmbeddedDocument5(IP, port, databaseName,collection); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + private static void queryArrayEmbeddedDocument1(String ip, int port, String databaseName, String collection) { |
| 32 | + QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 33 | + |
| 34 | + /* tim kiem document voi comments co chua document comment thoa man cac gia tri */ |
| 35 | + Bson query = Filters.eq("comments", new Comment("user1", "thankkkkk",15)); |
| 36 | + |
| 37 | + List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 38 | + System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 39 | + postList.forEach(post -> System.out.println(post.toString())); |
| 40 | + System.out.println("--- End ---"); |
| 41 | + } |
| 42 | + |
| 43 | + private static void queryArrayEmbeddedDocument2(String ip, int port, String databaseName, String collection) { |
| 44 | + QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 45 | + |
| 46 | + /* tim kiem document voi comments co chua document ma truong user co gia tri la user3 */ |
| 47 | + Bson query = Filters.eq("comments.user", "user3"); |
| 48 | + |
| 49 | + List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 50 | + System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 51 | + postList.forEach(post -> System.out.println(post.toString())); |
| 52 | + System.out.println("--- End ---"); |
| 53 | + } |
| 54 | + |
| 55 | + private static void queryArrayEmbeddedDocument3(String ip, int port, String databaseName, String collection) { |
| 56 | + QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 57 | + |
| 58 | + /* tim kiem document voi comments co document vi tri dau tien ma truong user co gia tri la user3 */ |
| 59 | + Bson query = Filters.eq("comments.0.user", "user3"); |
| 60 | + |
| 61 | + List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 62 | + System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 63 | + postList.forEach(post -> System.out.println(post.toString())); |
| 64 | + System.out.println("--- End ---"); |
| 65 | + } |
| 66 | + |
| 67 | + private static void queryArrayEmbeddedDocument4(String ip, int port, String databaseName, String collection) { |
| 68 | + QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 69 | + |
| 70 | + /* tim kiem document voi comments co it nhat mot document ma co truong user la user1 va truong likeNumber la 20 */ |
| 71 | + Bson query = Filters.elemMatch("comments", Filters.and(Filters.eq("user", "user1"), Filters.eq("likeNumber", 20))); |
| 72 | + |
| 73 | + List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 74 | + System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 75 | + postList.forEach(post -> System.out.println(post.toString())); |
| 76 | + System.out.println("--- End ---"); |
| 77 | + } |
| 78 | + |
| 79 | + private static void queryArrayEmbeddedDocument5(String ip, int port, String databaseName, String collection) { |
| 80 | + QueryDocument queryDocument = new QueryDocument(ip, port,databaseName); |
| 81 | + |
| 82 | + /* tim kiem document voi comments co document ma truong user la user1 va co document ma truong likeNumber bang 20 */ |
| 83 | + Bson query = Filters.and(Filters.eq("comments.user", "user1"), Filters.eq("comments.likeNumber", 20)); |
| 84 | + |
| 85 | + List<Post> postList = queryDocument.find(collection, Post.class, query); |
| 86 | + System.out.println("--- Document trong Collection Post voi "+ query.toString() + "---"); |
| 87 | + postList.forEach(post -> System.out.println(post.toString())); |
| 88 | + System.out.println("--- End ---"); |
| 89 | + } |
| 90 | + |
| 91 | + private static void initDataExample(String ip, int port , String databaseName, String collection) { |
| 92 | + InsertDocument insertDocument = new InsertDocument(ip, port, databaseName); |
| 93 | + insertDocument.insertMany(collection, Post.class, |
| 94 | + Post.builder().id(UUID.randomUUID().toString()).title("Mongodb").user("Hungcdev").comments(List.of(new Comment("user1", "good post",10), new Comment("user2", "great! Tks",20))).build(), |
| 95 | + Post.builder().id(UUID.randomUUID().toString()).title("Spring Java").user("Hungcdev").comments(List.of(new Comment("user2", "thank so much",15), new Comment("user1", "thankkkkk",20))).build(), |
| 96 | + Post.builder().id(UUID.randomUUID().toString()).title("Java").user("AtomPtit").comments(List.of(new Comment("user3", "good post",10), new Comment("user1", "thankkkkk",15))).build(), |
| 97 | + Post.builder().id(UUID.randomUUID().toString()).title("Spring Boot").user("AtomPtit").comments(List.of(new Comment("user2", "thank so much",20), new Comment("user3", "thankkkkk",30))).build(), |
| 98 | + Post.builder().id(UUID.randomUUID().toString()).title("Spring").user("HungHoi").comments(List.of(new Comment("user1", "great! Tks",25))).build() |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments