Tài liệu về nhà → MongoDB Atlas → MongoDB Atlas
Trên trang này
- Tạo chức năng dịch vụ ứng dụng để xác định chế độ xem được thực hiện theo yêu cầu
- Tạo một trình kích hoạt theo lịch trình
- Tạo một chỉ mục tìm kiếm Atlas trên chế độ xem được vật chất hóa
- Chạy truy vấn trên chế độ xem được vật chất hóa
Hướng dẫn này mô tả cách tạo một chỉ mục và chạy các truy vấn trên nhiều bộ sưu tập bằng cách sử dụng kết hợp các chế độ xem cụ thể theo yêu cầu và các dịch vụ ứng dụng ATLAS được lên lịch trình kích hoạt trên bộ sưu tập sample_supplies.sales
từ bộ dữ liệu mẫu.on-demand materialized views and Atlas App Services scheduled triggers on the sample_supplies.sales
collection from the sample dataset.
Chế độ xem cụ thể hóa theo yêu cầu là một bộ sưu tập mà bạn tạo và cập nhật bằng giai đoạn đường ống tổng hợp ____1010. Bạn có thể tạo một chỉ mục tìm kiếm ATLAS trên chế độ xem được vật chất hóa và sau đó chạy các truy vấn trên chế độ xem được vật chất hóa bằng giai đoạn đường ống tổng hợp
{ "_id" : "", "sales_quantity" : , "sales_amount" : }
1.Hướng dẫn này đưa bạn qua các bước sau:
Tạo một chức năng dịch vụ ứng dụng có tên
2 trong UI Dịch vụ ứng dụng để khởi tạo chế độ xem{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 được vật chất hóa bằng cách sử dụng dữ liệu từ bộ sưu tập mẫu{ "_id" : "", "sales_quantity" : , "sales_amount" : }
sample_supplies.sales
trên cụm Atlas của bạn.Lên lịch chức năng
2 để cập nhật{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 trên cơ sở định kỳ bằng cách sử dụng trình kích hoạt theo lịch trình dịch vụ ứng dụng.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập
3.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Chạy một truy vấn trên bộ sưu tập
3.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Trước khi bạn bắt đầu, hãy đảm bảo rằng cụm Atlas của bạn đáp ứng các yêu cầu được mô tả trong các điều kiện tiên quyết.
Tạo chức năng JavaScript trong UI Dịch vụ ứng dụng có tên
{ "_id" : "", "sales_quantity" : , "sales_amount" : }
2. Hàm xác định một quan điểm được vật chất hóa có chứa thông tin bán hàng hàng tháng tích lũy từ sample_supplies.sales
.Hàm
{ "_id" : "", "sales_quantity" : , "sales_amount" : }
2 xác định chế độ xem được vật chất hóa { "_id" : "", "sales_quantity" : , "sales_amount" : }
3 có chứa thông tin bán hàng hàng tháng tích lũy. Chức năng cập nhật thông tin bán hàng hàng tháng cho doanh số được thực hiện qua điện thoại. Ví dụ sau đây xác định chức năng:exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString: { format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} }}, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
Hàm sử dụng các giai đoạn đường ống tổng hợp sau để cập nhật
{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3:Giai đoạn
4 lọc dữ liệu để chỉ xử lý các doanh số được hoàn thành trongexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
5.exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
4 stage filters the data to process only those sales that were completed over theexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
5.exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; }; Giai đoạn
6 nhóm thông tin bán hàng vào năm tháng. Giai đoạn này xuất ra các tài liệu có biểu mẫu:exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
6 stage groups the sales information by the year-month. This stage outputs documents that have the form:exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; }; { "_id" : "", "sales_quantity" : , "sales_amount" : }
Giai đoạn
7 thay đổi loại dữ liệu của trườngexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
8 thànhexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
9. Các nhà khai thác tìm kiếm ATLASexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
1 không hỗ trợ kiểu dữ liệu{ "_id" : "", "sales_quantity" : , "sales_amount" : }
1. Thay đổi kiểu dữ liệu của trườngdb.sales.find[].sort[ {saleDate: -1} ]
8 cho phép bạn truy vấn trường này bằng các chỉ mục tìm kiếm ATLAS.exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
7 stage changes the data type of theexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
8 field toexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
9. Atlas Searchexports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; };
1 operators don't support the{ "_id" : "", "sales_quantity" : , "sales_amount" : }
1 data type. Changing thedb.sales.find[].sort[ {saleDate: -1} ]
8 field's data type allows you to query this field using Atlas Search indexes.exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; }; Giai đoạn
0 viết đầu ra cho bộ sưu tập{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
0 stage writes the output to the{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 collection.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Dựa trên trường
5, [mặc định cho các bộ sưu tập đầu ra không có đầu cơ], giai đoạn kiểm tra xem tài liệu trong kết quả tổng hợp có khớp với một tài liệu hiện có trong bộ sưu tập không: on thedb.sales.find[].sort[ {saleDate: -1} ]
5 field, [the default for unsharded output collections], the stage checks if the document in the aggregation results matches an existing document in the collection:db.sales.find[].sort[ {saleDate: -1} ]
Khi Atlas Search tìm thấy một trận đấu [nghĩa là một tài liệu có cùng một tháng đã tồn tại trong bộ sưu tập], Atlas Search thay thế tài liệu hiện có bằng tài liệu từ kết quả tổng hợp như được chỉ định trong giai đoạn.match [that is, a document with the same year-month already exists in the collection], Atlas Search replaces the existing document with the document from the aggregation results as specified in the stage.
Khi Atlas Search không tìm thấy một trận đấu, Atlas Search sẽ chèn tài liệu từ kết quả tổng hợp vào bộ sưu tập như được chỉ định trong giai đoạn. Đây là hành vi mặc định khi không có khớp cho trường.match, Atlas Search inserts the document from the aggregation results into the collection as specified in the stage. This is the default behavior when there is no match for the field.
Bây giờ bạn đã hiểu cách thức hoạt động của chức năng UpdatemonthlySales, hãy tạo chức năng trong UI Dịch vụ ứng dụng:updateMonthlySales function works, create the function in the App Services UI:
Để xác định chức năng phía máy chủ mới từ giao diện người dùng, trước tiên bạn phải tạo ứng dụng Dịch vụ ứng dụng: Nếu bạn chưa làm như vậy, hãy nhấp vào tab Dịch vụ ứng dụng.App Services tab. Tạo ứng dụng: Nếu bạn đang tạo ứng dụng Dịch vụ ứng dụng đầu tiên của mình trong dự án, bạn sẽ được hiển thị tùy chọn bắt đầu mà không cần mẫu [xây dựng ứng dụng của riêng bạn]. Chọn tùy chọn xây dựng ứng dụng của riêng bạn.Build your own App]. Select the Build your own App option. Nếu bạn đã tạo ít nhất một ứng dụng dịch vụ ứng dụng trong dự án, hãy nhấp vào Tạo một ứng dụng mới.Create a New App. Trong trường Tên, nhập db.sales.find[].sort[ {saleDate: -1} ] db.sales.find[].sort[ {saleDate: -1} ]
6 làm tên của hàm.Name field, enter
6 as the name of the function.
Trong trường liên kết của cơ sở dữ liệu của bạn, chọn sử dụng tùy chọn Nguồn dữ liệu MongoDB Atlas hiện có.Link your database field, select the Use an existing MongoDB Atlas Data Source option.
Từ thả xuống, chọn cụm Atlas bạn đã tạo trong các điều kiện tiên quyết.
Nhấp vào Tạo dịch vụ ứng dụng.Create App Service.
Để xác định chức năng phía máy chủ mới từ UI:
Nhấp vào các chức năng trong menu điều hướng bên trái.Functions in the left navigation menu.
Nhấp vào Tạo chức năng mới.Create New Function.
Nhập
2 làm tên của hàm.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Theo xác thực, chọn hệ thống.Authentication, select System.
Nhấp vào tab Trình chỉnh sửa chức năng.Function Editor tab.
Thêm mã JavaScript vào hàm
8. Tối thiểu, mã phải gán một hàm cho biến toàn cầudb.sales.find[].sort[ {saleDate: -1} ]
8:db.sales.find[].sort[ {saleDate: -1} ]
exports = function[]{ var pipeline = [ { $match: {purchaseMethod: "Phone"} }, { $unwind: {path: "$items"}}, { $group: { _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } }, sales_quantity: { $sum: "$items.quantity"}, sales_price: { $sum: "$items.price"} } }, { $set: { sales_price: { $toDouble: "$sales_price"}}}, { $merge: { into: "monthlysales", whenMatched: "replace" } } ] var monthlysales = context.services.get["mongodb-atlas"].db["sample_supplies"].collection["sales"]; return monthlysales.aggregate[pipeline]; }; Nhấp vào nút Run ở góc dưới bên phải của Trình chỉnh sửa chức năng để tạo chế độ xem
3 được vật chất hóa.Run button in the lower right-hand corner of the Function Editor to create the{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 materialized view.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Mở
1 trong cửa sổ thiết bị đầu cuối và kết nối với cụm của bạn. Để biết hướng dẫn chi tiết về kết nối, xem Connect quadb.monthlysales.find[].sort[ { _id: -1} ]
1db.monthlysales.find[].sort[ { _id: -1} ]
1 in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect viadb.monthlysales.find[].sort[ { _id: -1} ]
1db.monthlysales.find[].sort[ { _id: -1} ]
Sử dụng cơ sở dữ liệu
3:db.monthlysales.find[].sort[ { _id: -1} ]
Truy vấn Bộ sưu tập
4. Lưu ý rằng việc bán hàng cuối cùng trongdb.monthlysales.find[].sort[ { _id: -1} ]
4 xảy ra vào tháng 12 năm 2017:db.monthlysales.find[].sort[ { _id: -1} ]
db.sales.find[].sort[ {saleDate: -1} ]
Xác nhận rằng chế độ xem được vật chất hóa đã được tạo trong cơ sở dữ liệu
3 của bạn:db.monthlysales.find[].sort[ { _id: -1} ]
Lệnh liệt kê các bộ sưu tập của bạn, bao gồm chế độ xem
3 mới được tạo ra.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Truy vấn quan điểm cụ thể hóa
3:{ "_id" : "", "sales_quantity" : , "sales_amount" : }
db.monthlysales.find[].sort[ { _id: -1} ]
Cập nhật bộ sưu tập
4 với dữ liệu bán hàng điện thoại mới từ tháng 1 năm 2018.db.monthlysales.find[].sort[ { _id: -1} ]
db.sales.insertMany[ [ { saleDate: ISODate["2018-01-23T21:06:49.506Z"], items: [ { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["40.01"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["35.29"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["56.12"], quantity: 5 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["77.71"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["18.47"], quantity: 2 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["19.95"], quantity: 8 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.08"], quantity: 3 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["14.16"], quantity: 3 } ], storeLocation: 'Denver', customer: { gender: 'M', age: 42, email: '', satisfaction: 4 }, couponUsed: true, purchaseMethod: 'Phone' }, { saleDate: ISODate["2018-01-25T10:01:02.918Z"], items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.05"], quantity: 10 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["28.31"], quantity: 9 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["20.95"], quantity: 3 }, { name: 'laptop', tags: [ 'electronics', 'school', 'office' ], price: Decimal128["866.5"], quantity: 4 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["33.09"], quantity: 4 }, { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["37.55"], quantity: 1 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["83.28"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["42.9"], quantity: 4 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["16.68"], quantity: 2 } ], storeLocation: 'Seattle', customer: { gender: 'M', age: 50, email: '', satisfaction: 5 }, couponUsed: false, purchaseMethod: 'Phone' }, ] ] Truy vấn Bộ sưu tập
4 một lần nữa để xác nhận các mục bán hàng mới. 2 kết quả truy vấn hàng đầu phản ánh rằng dữ liệu bán hàng hiện kết thúc vào tháng 1 năm 2018:db.monthlysales.find[].sort[ { _id: -1} ]
db.sales.find[].sort[ {saleDate: -1} ]
Quay trở lại dự thảo chức năng của bạn trong giao diện người dùng Dịch vụ ứng dụng và nhấp vào nút Run ở góc dưới bên phải của Trình chỉnh sửa chức năng. Hàm
2 làm mới chế độ xem{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 được thực hiện bằng cách sử dụng dữ liệu bán hàng tháng 1 năm 2018.Run button in the lower right-hand corner of the Function Editor. The{ "_id" : "", "sales_quantity" : , "sales_amount" : }
2 function refreshes the{ "_id" : "", "sales_quantity" : , "sales_amount" : }
3 materialized view using the January 2018 sales data.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Nhấp vào Lưu bản nháp từ tab Trình chỉnh sửa chức năng hoặc Cài đặt.Save Draft from either the Function Editor or Settings tab.
Quay trở lại
db.monthlysales.find[].sort[ { _id: -1} ]
1 và truy vấn chế độ xem { "_id" : "", "sales_quantity" : , "sales_amount" : }
3 được vật chất hóa để xác nhận bản cập nhật. Kết quả hàng đầu được trả về bởi truy vấn phản ánh dữ liệu db.monthlysales.find[].sort[ { _id: -1} ]
4 được cập nhật cho tháng 1 năm 2018:db.monthlysales.find[].sort[ { _id: -1} ]
1 and query
the { "_id" : "", "sales_quantity" : , "sales_amount" : }
3 materialized view to confirm the update. The top result returned by the query reflects the updated db.monthlysales.find[].sort[ { _id: -1} ]
4 data for January 2018:db.monthlysales.find[].sort[ { _id: -1} ]
Lên lịch chức năng dịch vụ ứng dụng được tạo trong bước trước để chạy mỗi ngày một lần để giữ cho chế độ xem được vật chất hóa cập nhật.
Đặt trường Loại kích hoạt thành lịch trình.Trigger Type field to Scheduled.
Trong trường Tên, nhập
2.Name field, enter{ "_id" : "", "sales_quantity" : , "sales_amount" : }
2.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Đặt trường Loại lịch trình thành BASIC.Schedule Type field to Basic.
Trong lần lặp lại một lần bằng cách thả xuống, chọn
7 và đặt giá trị thành ngày ưa thích của bạn.Repeat once by dropdown, selectdb.sales.insertMany[ [ { saleDate: ISODate["2018-01-23T21:06:49.506Z"], items: [ { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["40.01"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["35.29"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["56.12"], quantity: 5 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["77.71"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["18.47"], quantity: 2 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["19.95"], quantity: 8 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.08"], quantity: 3 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["14.16"], quantity: 3 } ], storeLocation: 'Denver', customer: { gender: 'M', age: 42, email: '', satisfaction: 4 }, couponUsed: true, purchaseMethod: 'Phone' }, { saleDate: ISODate["2018-01-25T10:01:02.918Z"], items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.05"], quantity: 10 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["28.31"], quantity: 9 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["20.95"], quantity: 3 }, { name: 'laptop', tags: [ 'electronics', 'school', 'office' ], price: Decimal128["866.5"], quantity: 4 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["33.09"], quantity: 4 }, { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["37.55"], quantity: 1 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["83.28"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["42.9"], quantity: 4 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["16.68"], quantity: 2 } ], storeLocation: 'Seattle', customer: { gender: 'M', age: 50, email: '', satisfaction: 5 }, couponUsed: false, purchaseMethod: 'Phone' }, ] ]
7 and set the value to your preferred date.db.sales.insertMany[ [ { saleDate: ISODate["2018-01-23T21:06:49.506Z"], items: [ { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["40.01"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["35.29"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["56.12"], quantity: 5 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["77.71"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["18.47"], quantity: 2 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["19.95"], quantity: 8 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.08"], quantity: 3 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["14.16"], quantity: 3 } ], storeLocation: 'Denver', customer: { gender: 'M', age: 42, email: '', satisfaction: 4 }, couponUsed: true, purchaseMethod: 'Phone' }, { saleDate: ISODate["2018-01-25T10:01:02.918Z"], items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.05"], quantity: 10 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["28.31"], quantity: 9 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["20.95"], quantity: 3 }, { name: 'laptop', tags: [ 'electronics', 'school', 'office' ], price: Decimal128["866.5"], quantity: 4 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["33.09"], quantity: 4 }, { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["37.55"], quantity: 1 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["83.28"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["42.9"], quantity: 4 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["16.68"], quantity: 2 } ], storeLocation: 'Seattle', customer: { gender: 'M', age: 50, email: '', satisfaction: 5 }, couponUsed: false, purchaseMethod: 'Phone' }, ] ] Ghi chú
Ngoài ra, cho mục đích thử nghiệm, hãy đặt lặp lại một lần bằng cách thả xuống đến xảy ra thường xuyên hơn, chẳng hạn như phút hoặc giờRepeat once by dropdown to a more frequent occurrence, such as Minute or Hour
Đặt trường Chọn loại sự kiện thành hàm.Select An Event Type field to Function.
Trong chức năng thả xuống, chọn
2.Function dropdown, select{ "_id" : "", "sales_quantity" : , "sales_amount" : }
2.{ "_id" : "", "sales_quantity" : , "sales_amount" : }
Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập
db.sales.insertMany[ [ { saleDate: ISODate["2018-01-23T21:06:49.506Z"], items: [ { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["40.01"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["35.29"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["56.12"], quantity: 5 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["77.71"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["18.47"], quantity: 2 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["19.95"], quantity: 8 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.08"], quantity: 3 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["14.16"], quantity: 3 } ], storeLocation: 'Denver', customer: { gender: 'M', age: 42, email: '', satisfaction: 4 }, couponUsed: true, purchaseMethod: 'Phone' }, { saleDate: ISODate["2018-01-25T10:01:02.918Z"], items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.05"], quantity: 10 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["28.31"], quantity: 9 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["20.95"], quantity: 3 }, { name: 'laptop', tags: [ 'electronics', 'school', 'office' ], price: Decimal128["866.5"], quantity: 4 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["33.09"], quantity: 4 }, { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["37.55"], quantity: 1 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["83.28"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["42.9"], quantity: 4 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["16.68"], quantity: 2 } ], storeLocation: 'Seattle', customer: { gender: 'M', age: 50, email: '', satisfaction: 5 }, couponUsed: false, purchaseMethod: 'Phone' }, ] ]
9.Nhấp vào cơ sở dữ liệu ở góc trên cùng bên trái của Atlas để điều hướng đến trang triển khai cơ sở dữ liệu cho dự án của bạn.Databases in the top-left corner of Atlas to navigate to the Database Deployments page for your project. Trong trường Tên chỉ mục, nhập db.sales.find[].sort[ {saleDate: -1} ] db.sales.find[].sort[ {saleDate: -1} ]
0.Index Name field, enter
0.
Trong phần cơ sở dữ liệu và bộ sưu tập, tìm cơ sở dữ liệu
db.monthlysales.find[].sort[ { _id: -1} ]
3 và chọn bộ sưu tập { "_id" : "", "sales_quantity" : , "sales_amount" : }
3.Database and
Collection section, find the db.monthlysales.find[].sort[ { _id: -1} ]
3 database, and select the { "_id" : "", "sales_quantity" : , "sales_amount" : }
3 collection.Nhấn tiếp.Next.
Một cửa sổ phương thức dường như cho bạn biết chỉ mục của bạn đang xây dựng. Nhấp vào nút Đóng.Close button.
Chỉ mục mới được tạo xuất hiện trên tab Tìm kiếm. Trong khi chỉ số đang xây dựng, trường trạng thái đọc được xây dựng trong tiến trình. Khi chỉ mục được xây dựng xong, trường trạng thái đọc hoạt động.Search tab. While the index is building, the Status field reads Build in Progress. When the index is finished building, the Status field reads Active.
Ghi chú
Ngoài ra, cho mục đích thử nghiệm, hãy đặt lặp lại một lần bằng cách thả xuống đến xảy ra thường xuyên hơn, chẳng hạn như phút hoặc giờ
Đặt trường Chọn loại sự kiện thành hàm.
Trong chức năng thả xuống, chọn { "_id" : "", "sales_quantity" : , "sales_amount" : } db.monthlysales.find[].sort[ { _id: -1} ]
2.
1 prompt:
Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập
db.sales.insertMany[ [ { saleDate: ISODate["2018-01-23T21:06:49.506Z"], items: [ { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["40.01"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["35.29"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["56.12"], quantity: 5 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["77.71"], quantity: 2 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["18.47"], quantity: 2 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["19.95"], quantity: 8 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.08"], quantity: 3 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["14.16"], quantity: 3 } ], storeLocation: 'Denver', customer: { gender: 'M', age: 42, email: '', satisfaction: 4 }, couponUsed: true, purchaseMethod: 'Phone' }, { saleDate: ISODate["2018-01-25T10:01:02.918Z"], items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["8.05"], quantity: 10 }, { name: 'binder', tags: [ 'school', 'general', 'organization' ], price: Decimal128["28.31"], quantity: 9 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["20.95"], quantity: 3 }, { name: 'laptop', tags: [ 'electronics', 'school', 'office' ], price: Decimal128["866.5"], quantity: 4 }, { name: 'notepad', tags: [ 'office', 'writing', 'school' ], price: Decimal128["33.09"], quantity: 4 }, { name: 'printer paper', tags: [ 'office', 'stationary' ], price: Decimal128["37.55"], quantity: 1 }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ], price: Decimal128["83.28"], quantity: 2 }, { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ], price: Decimal128["42.9"], quantity: 4 }, { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ], price: Decimal128["16.68"], quantity: 2 } ], storeLocation: 'Seattle', customer: { gender: 'M', age: 50, email: '', satisfaction: 5 }, couponUsed: false, purchaseMethod: 'Phone' }, ] ]
9.db.monthlysales.aggregate[[ { $search: { "index": "monthlySalesIndex", "range": { "gt": 10000, "path": ["sales_price"] } } }, { $count: 'months_w_over_10000' }, ]]
Nhấp vào cơ sở dữ liệu ở góc trên cùng bên trái của Atlas để điều hướng đến trang triển khai cơ sở dữ liệu cho dự án của bạn.
Trong trường Tên chỉ mục, nhập
db.sales.find[].sort[ {saleDate: -1} ]
0.MongoDB Server Manual.