Nhóm theo trong mongodb

MongoDB là một cơ sở dữ liệu hướng tài liệu (document), một dạng cơ sở dữ liệu NoSQL. Vì thế, MongoDB sẽ tránh cấu trúc dựa trên bảng của cơ sở dữ liệu quan hệ để thích ứng với các tài liệu như JSON có một lược đồ rất linh hoạt được gọi là BSON. MongoDB sử dụng lưu trữ dữ liệu dưới dạng JSON tài liệu nên mỗi bộ sưu tập sẽ có kích thước và các tài liệu khác nhau. Dữ liệu được lưu trữ trong tài liệu dạng JSON nên truy vấn sẽ rất nhanh

Định nghĩa thêm về  MongoDB

MongoDB lần đầu ra đời bởi MongoDB Inc. , tại thời điểm đó là thế hệ 10, vào tháng Mười năm 2007, nó là một phần của sản phẩm PaaS (Nền tảng dưới dạng Dịch vụ) tương tự như Windows Azure và Google App Engine. Sau đó nó đã được chuyển thành nguồn mở từ năm 2009

MongoDB đã trở thành một trong những cơ sở dữ liệu NoSQL nổi trội nhất nhì, được sử dụng làm phụ trợ cho rất nhiều trang web như eBay, SourceForge và The New York Times

Việc sử dụng Aggregation framework, giúp việc xử lý dữ liệu trở nên dễ dàng hơn. Và đã có nhiều xử lý mà MQL (Mongo Query Language) không thực hiện được

Nhóm theo trong mongodb

Để tạo đường dẫn có thể sử dụng nhiều cách

  • hàm uẩn
  • Use Compass
  • Sử dụng trình điều khiển

Nhóm theo trong mongodb

Hàm tổng hợp có thể tạo ra đường dẫn

Nhóm theo trong mongodb

Ví dụ

Thực hiện một truy vấn tổng hợp bao gồm 2 giai đoạn như sau

db.companies.aggregate([
   { $match: { founded_year: {$gt:2000} } },
   { $group: { _id: "$category_code", total: { $sum: "$number_of_employees" } } }
])
  • Giai đoạn 1. Sử dụng $match để lọc tất cả các công ty đã thành lập sau năm 2000
  • giai đoạn 2. sử dụng $group để nhóm các công ty theo category_code, với mỗi category_code thì tính tổng (tổng) của số nhân viên

Sử dụng la bàn dễ dàng tạo ra các giai đoạn. Ví dụ

Nhóm theo trong mongodb

To output pipe by way select the icon

Nhóm theo trong mongodb

Kết quả

Nhóm theo trong mongodb

Trong bước trên sau khi đã xuất ra đường dẫn, có thể chọn ngôn ngữ lập trình để có mã nguồn. Ví dụ với ngôn ngữ Java

Nhóm theo trong mongodb

Run try

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();

Kết quả

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...

View data sample_airbn testb. danh sáchAndReviews. Dữ liệu này bao gồm danh sách khách sạn, nhà trọ và đánh giá về nhà trọ đó

Một tài liệu sẽ có mẫu như sau

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}

Để truy vấn 3 nhà trọ giá rẻ có wifi, bạn có thể thực hiện truy vấn như sau

db.listingsAndReviews.find({"amenities": "Wifi"}, {"name": 1, "price": 1, "_id": 0}).sort({"price": 1}).limit(3)

Truy vấn trên có thể dễ dàng được thực hiện bằng cách tạo đường dẫn

  • Có rất nhiều giai đoạn có thể được đưa vào trong quy trình
  • Toàn bộ giai đoạn có thể tìm thấy tại đây
  • Các giai đoạn có thể lặp lại (trừ $out, $merge, và $geoNear)

$match stage được sử dụng để lọc (lọc) dữ liệu

cú pháp

{ $match: {  } }

Ví dụ 1

Ví dụ trong dữ liệu sample_training. các công ty

db.companies.aggregate(
   [
       {
           $match: {"category_code": "search","number_of_employees": {$gte:500}}
       }
   ]
)

The filter as on also can split to 2 Stages

db.companies.aggregate(
   [
       {
           $match: {"category_code": "search"}
       },
       {
           $match: {"number_of_employees": {$gte:500}}
       }
   ]
)

Ví dụ 2

Tạo 1 đường dẫn để tìm các bộ phim trong sample_mflix. phim trong that

  • imdb. xếp hạng thấp nhất là 7
  • thể loại không thuộc thể loại "Tội phạm" hoặc "Kinh dị"
  • được xếp hạng là "PG" hoặc "G"
  • ngôn ngữ có cả "tiếng Anh" và "tiếng Nhật"

Ở đây dữ liệu trong phim thì các thông tin về thể loại, xếp hạng, ngôn ngữ được lưu trữ trong mảng, vì vậy cần sử dụng các hàm tìm trong mảng tương ứng như $nin, $in, $all. Pipeline will like after

________số 8

Please find the house for

Căn nhà/phòng cho thuê có nhiều hơn 1 giường, giá thuê theo tháng thấp hơn 2000 và có tối thiểu 1 nhận xét có chữ Nice view có tên là gì?

Rustic Gaudí - Sagrada FamíliaBoqueria - Studio ấm cúng ở BornAppart. Zen au coeur de MontréalCăn hộ mới gần Sagrada Familia Chung cư đẹp và đầy nắng/Outremont

$project được sử dụng để lựa chọn những trường nào sẽ được chuyển sang giai đoạn tiếp theo

cú pháp

{ $project: {  } }

Ví dụ

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
0

Trong ví dụ về giai đoạn 2 của đường ống sử dụng $project. Chú ý trường nào muốn hiện thì là 1, trường nào không muốn hiện thì là 0. Ví dụ muốn hiện toàn bộ các trường ngoại lệ

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
1

Chú ý $project cũng có thể thay đổi dữ liệu, ví dụ chỉ muốn lấy 5 ký tự đầu tiên của tiêu đề

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
2

Hoặc trường hợp muốn lọc ra những bộ phim mà tiêu đề có 1 chữ cái thì có thể làm như sau

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
3

Kết quả

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
4

Ví dụ về đường ống bao gồm có 3 giai đoạn

  • Giai đoạn 1 để trích xuất những bộ phim có tiêu đề
  • Giai đoạn 2 để chuyển tiêu đề của bộ phim từ chuỗi thành mảng
  • Giai đoạn 3 để giữ lại các mảng tiêu đề có 1 phần tử

Trong ví dụ trên itcount() được sử dụng sau hàm tổng hợp để đếm

Một ví dụ khác

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
5

Kết quả

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
6

Hiển thị tên (tên) và mô tả (tóm tắt) của những nhà cho thuê có bếp (Kitchen), có mạng Wifi, được từ 8 người, ở Bồ Đào Nha (Bồ Đào Nha)

Mã số

$addFields được sử dụng để thêm trường vào tài liệu

cú pháp

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
7

Ví dụ

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
8

Kết quả

MongoClient mongoClient = new MongoClient(
       new MongoClientURI(
               "mongodb+srv://root:[email protected]/MyDB?authSource=admin&replicaSet=atlas-fc7dvz-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true"
       )
);
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection collection = database.getCollection("movies");

AggregateIterable result = collection.aggregate(Arrays.asList(new Document("$group", new Document("_id", "$year").append("numOfMovies", new Document("$sum", 1L))), new Document("$sort", new Document("numOfMovies", -1L))));
result.forEach((Consumer) d -> System.out.println(d.toJson()));
mongoClient.close();
9

Để ghép 2 chuỗi sử dụng $concat, để biến số thành chuỗi sử dụng $toString

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
0

$sample được sử dụng để lựa chọn ra một tập tin mẫu trong các tài liệu đang có

cú pháp

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
1

Ví dụ

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
2

Giai đoạn đầu tiên trong đường dẫn trên sẽ chọn ngẫu nhiên một mẫu bao gồm 100 tài liệu trong bộ sưu tập

Use sample_training. tuyến đường

Please show the 10 chuyến bay bất kỳ khởi động, or to the bay SGN

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
3

$group is used to partition data to the groups

Nhóm theo trong mongodb

cú pháp

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
4
"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
5

Quá trình tổng hợp sẽ thực hiện 3 giai đoạn

  • Giai đoạn 1. khớp dữ liệu trong bộ sưu tập Sản phẩm để lấy ra các sản phẩm có thương hiệu là Trek
  • giai đoạn 2. Nhóm các tài liệu theo Danh mục và tính giá trung bình
  • Giai đoạn 3. Sắp xếp theo giá trung bình vừa được tính

Kết quả

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
6

Như vậy sơ đồ xử lý dữ liệu sẽ như sau

Nhóm theo trong mongodb

Ví dụ

Thống kê số lượng phim trong mỗi năm và điểm trung bình imdb cho các phim trong năm đó

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
7

_id cũng có thể được tính toán rồi nhóm mới, ví dụ

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
8

Các loại phòng (room_type) nào xuất hiện trong sample_airbnb. danh sáchAndReviews

Mã số

Hãy biết các nhà (property_type) là Cabin có giá thuê trung bình bao nhiêu

Khoang 100Khoảng 95Khoảng 90Khoảng 85Khoảng 80

Please show the point of center of each student (dữ liệu sample_traning. lớp). Biết rằng điểm mỗi môn của SV là điểm trung bình các thành phần, điểm trung bình của sinh viên là điểm trung bình mỗi môn

Mã số

$sort là giai đoạn được sử dụng để sắp xếp

cú pháp

"_id": 2013, "numOfMovies": {"$numberLong": "1220"}}
{"_id": 2014, "numOfMovies": {"$numberLong": "1147"}}
{"_id": 2012, "numOfMovies": {"$numberLong": "1109"}}
{"_id": 2011, "numOfMovies": {"$numberLong": "1040"}}
{"_id": 2009, "numOfMovies": {"$numberLong": "990"}}
...
9

Ví dụ

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}
0

$limit được sử dụng để giới hạn số lượng bản ghi chuyển sang giai đoạn tiếp theo

cú pháp

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}
1

Ví dụ

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}
2

Để truy vấn và hiển thị tên, giá nhà/phòng giá rẻ có wifi, bạn có thể thực hiện truy vấn như sau

db.listingsAndReviews.find({"amenities": "Wifi"}, {"name": 1, "price": 1, "_id": 0}).sort({"price": 1}).limit(3)

Kết quả

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}
4

Truy vấn như trên có thể được thay thế bằng cách thực hiện Aggregation framework

Hãy tạo 1 đường dẫn bao gồm 4 giai đoạn phù hợp, dự án, sắp xếp, giới hạn, thực hiện truy vấn để có kết quả như trên

Mã số

$unwind được sử dụng để phân tách các tài liệu có 1 mảng thành nhiều tài liệu với mỗi phần tử của mảng

cú pháp

{
 "_id": "10006546",
 "listing_url": "https://www.airbnb.com/rooms/10006546",
 "name": "Ribeira Charming Duplex",
 "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
 "interaction": "Cot - 10 € / night Dog - € 7,5 / night",
 "house_rules": "Make the house your home...",
 "property_type": "House",
 "room_type": "Entire home/apt",
 "bed_type": "Real Bed",
 "minimum_nights": "2",
 "maximum_nights": "30",
 "cancellation_policy": "moderate",
 "last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "calendar_last_scraped": {
   "$date": {
     "$numberLong": "1550293200000"
   }
 },
 "first_review": {
   "$date": {
     "$numberLong": "1451797200000"
   }
 },
 "last_review": {
   "$date": {
     "$numberLong": "1547960400000"
   }
 },
 "accommodates": {
   "$numberInt": "8"
 },
 "bedrooms": {
   "$numberInt": "3"
 },
 "beds": {
   "$numberInt": "5"
 },
 "number_of_reviews": {
   "$numberInt": "51"
 },
 "bathrooms": {
   "$numberDecimal": "1.0"
 },
 "amenities": [
   "TV",
   "Cable TV",
   "Wifi",
   "Kitchen",
   "Paid parking off premises",
   "Smoking allowed",
   "Pets allowed",
   "Buzzer/wireless intercom",
   "Heating",
   "Family/kid friendly",
   "Washer",
   "First aid kit",
   "Fire extinguisher",
   "Essentials",
   "Hangers",
   "Hair dryer",
   "Iron",
   "Pack 'n Play/travel crib",
   "Room-darkening shades",
   "Hot water",
   "Bed linens",
   "Extra pillows and blankets",
   "Microwave",
   "Coffee maker",
   "Refrigerator",
   "Dishwasher",
   "Dishes and silverware",
   "Cooking basics",
   "Oven",
   "Stove",
   "Cleaning before checkout",
   "Waterfront"
 ],
 "price": {
   "$numberDecimal": "80.00"
 },
 "security_deposit": {
   "$numberDecimal": "200.00"
 },
 "cleaning_fee": {
   "$numberDecimal": "35.00"
 },
 "extra_people": {
   "$numberDecimal": "15.00"
 },
 "guests_included": {
   "$numberDecimal": "6"
 },
 "images": {
   "thumbnail_url": "",
   "medium_url": "",
   "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large",
   "xl_picture_url": ""
 },
 "host": {
   "host_id": "51399391",
   "host_url": "https://www.airbnb.com/users/show/51399391",
   "host_name": "Ana&Gonçalo",
   "host_location": "Porto, Porto District, Portugal",
   "host_about": "Gostamos de passear, de viajar, de conhecer pessoas e locais novos, gostamos de desporto e animais! Vivemos na cidade mais linda do mundo!!!",
   "host_response_time": "within an hour",
   "host_thumbnail_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_small",
   "host_picture_url": "https://a0.muscache.com/im/pictures/fab79f25-2e10-4f0f-9711-663cb69dc7d8.jpg?aki_policy=profile_x_medium",
   "host_neighbourhood": "",
   "host_response_rate": {
     "$numberInt": "100"
   },
   "host_is_superhost": false,
   "host_has_profile_pic": true,
   "host_identity_verified": true,
   "host_listings_count": {
     "$numberInt": "3"
   },
   "host_total_listings_count": {
     "$numberInt": "3"
   },
   "host_verifications": [
     "email",
     "phone",
     "reviews",
     "jumio",
     "offline_government_id",
     "government_id"
   ]
 },
 "address": {
   "street": "Porto, Porto, Portugal",
   "suburb": "",
   "government_area": "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
   "market": "Porto",
   "country": "Portugal",
   "country_code": "PT",
   "location": {
     "type": "Point",
     "coordinates": [
       {
         "$numberDouble": "-8.61308"
       },
       {
         "$numberDouble": "41.1413"
       }
     ],
     "is_location_exact": false
   }
 },
 "availability": {
   "availability_30": {
     "$numberInt": "28"
   },
   "availability_60": {
     "$numberInt": "47"
   },
   "availability_90": {
     "$numberInt": "74"
   },
   "availability_365": {
     "$numberInt": "239"
   }
 },
 "review_scores": {
   "review_scores_accuracy": {
     "$numberInt": "9"
   },
   "review_scores_cleanliness": {
     "$numberInt": "9"
   },
   "review_scores_checkin": {
     "$numberInt": "10"
   },
   "review_scores_communication": {
     "$numberInt": "10"
   },
   "review_scores_location": {
     "$numberInt": "10"
   },
   "review_scores_value": {
     "$numberInt": "9"
   },
   "review_scores_rating": {
     "$numberInt": "89"
   }
 },
 "reviews": [
   {
     "_id": "362865132",
     "date": {
       "$date": {
         "$numberLong": "1545886800000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "208880077",
     "reviewer_name": "Thomas",
     "comments": "Very helpful hosts. Cooked traditional..."
   },
   {
     "_id": "364728730",
     "date": {
       "$date": {
         "$numberLong": "1546232400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "91827533",
     "reviewer_name": "Mr",
     "comments": "Ana & Goncalo were great on communication..."
   },
   {
     "_id": "403055315",
     "date": {
       "$date": {
         "$numberLong": "1547960400000"
       }
     },
     "listing_id": "10006546",
     "reviewer_id": "15138940",
     "reviewer_name": "Milo",
     "comments": "The house was extremely well located..."
   }
 ]
}
5

Ví dụ

muốn tính điểm imdb trung bình của từng thể loại phim. Nhưng thể loại của phim thì lại được chứa trong mảng. Do đó cần phải Deconstructs it before