Aggregation trong MongoDB | 41 bài học miễn phí MongoDB hay nhất

3 262 1
Aggregation trong MongoDB | 41 bài học miễn phí MongoDB hay nhất

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/mongodb/index.jsp                                                                                                              Copyright  ©  vietjack.com     Aggregation MongoDB Aggregation hiểu tập hợp Các Aggregation operation xử lý ghi liệu trả kết tính tốn Các phép tốn tập hợp nhóm giá trị từ nhiều Document lại với nhau, thực nhiều phép toán đa dạng liệu nhóm để trả kết Trong SQL, count(*) GROUP BY tương đương với Aggregation MongoDB Phương thức aggregate() MongoDB Với Aggregation MongoDB, bạn nên sử dụng phương thức aggregate() Cú pháp Cú pháp phương thức aggregate() sau: >db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) Ví dụ Trong Collection, bạn có liệu sau: { _id: ObjectId(7df78ad8902c) title: 'MongoDB Overview', description: 'MongoDB is no sql database', by_user: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { _id: ObjectId(7df78ad8902d) title: 'NoSQL Overview', description: 'No sql database is very fast', by_user: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 10 }, { _id: ObjectId(7df78ad8902e) title: 'Neo4j Overview', description: 'Neo4j is no sql database', by_user: 'Neo4j', url: 'http://www.neo4j.com', tags: ['neo4j', 'database', 'NoSQL'], likes: 750 }, Từ Collection trên, bạn muốn hiển thị danh sách có hướng dẫn viết người dùng, bạn sử dụng phương thức aggregate() sau: > db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) { "result" : [ { "_id" : "tutorials point", "num_tutorial" : }, { "_id" : "Neo4j", "num_tutorial" : } ], "ok" : } > Truy vấn SQL tương đương cho trường hợp select by_user, count(*) from mycol group by by_user http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/mongodb/index.jsp                                                                                                              Copyright  ©  vietjack.com     Trong ví dụ trên, nhóm Document trường by_user lần xuất by_user, giá trị trước tổng tăng lên Có danh sách biểu thức Aggregation có sẵn, liệt kê đây: Biểu thức Miêu tả Ví dụ $sum Tổng giá trị xác định từ tất Document Collection db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) $avg Tính trung bình tất giá trị cho từ tất Document Collection db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) $min Lấy giá trị nhỏ giá trị từ tất Document Collection db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) $max Lấy giá trị lớn giá trị từ tất Document Collection db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) $push Chèn giá trị vào mảng Document kết db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) $addToSet Chèn giá trị tới mảng Document kết quả, không tạo db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) $first Lấy Document từ Source Document theo nhóm db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) $last Lấy Document cuối từ Source Document db.mycol.aggregate([{$group : {_id : "$by_user", last_url : http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/mongodb/index.jsp                                                                                                              Copyright  ©  vietjack.com     theo nhóm {$last : "$url"}}}]) Khái niệm pipeline MongoDB Trong UNIX, lệnh shell pipeline nghĩa khả để thực thi hoạt động số input sử dụng output input cho lệnh tiếp theo, … MongoDB hỗ trợ khái niệm pipeline Aggregation Framework Có tập hợp giai đoạn có giai đoạn lấy tập hợp Document input tạo tập Document kết (hoặc kết cuối JSON Document phần cuối pipeline) Kết này, sau đó, lại sử dụng cho giai đoạn … Các giai đoạn có Aggregation Framework là: • $project: Được sử dụng để chọn số trường cụ thể từ Collection • $match: Đây hoạt động lọc giảm số Document mà cung cấp input cho giai đoạn • $group: Thực Aggregation thực sự, trình bày • $sort: Sắp xếp Document • $skip: Nhảy qua số Document cung cấp • $limit: Giới hạn số Document • $unwind: Được sử dụng để chia Document sử dụng mảng thành nhiều Document Sử dụng hoạt động tạo số lượng Document cho bước   http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí     ...                                                Trang  chia  sẻ  các bài học  online miễn phí     http://vietjack.com /mongodb/ index.jsp                                                    ... Khái niệm pipeline MongoDB Trong UNIX, lệnh shell pipeline nghĩa khả để thực thi hoạt động số input sử dụng output input cho lệnh tiếp theo, … MongoDB hỗ trợ khái niệm pipeline Aggregation Framework...                                                              Trang  chia  sẻ  các bài học  online miễn phí    

Ngày đăng: 02/12/2017, 11:44

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan