Hướng dẫn mongodb index multiple collections - mongodb lập chỉ mục nhiều bộ sưu tập

Tài liệu về nhà → MongoDB AtlasMongoDB 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" : <NumberDecimal> }

1.

Hướng dẫn này đưa bạn qua các bước sau:

  1. Tạo một chức năng dịch vụ ứng dụng có tên

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2 trong UI Dịch vụ ứng dụng để khởi tạo chế độ xem

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    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 sample_supplies.sales trên cụm Atlas của bạn.

  2. Lên lịch chức năng

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2 để cập nhật

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    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.

  3. Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3.

  4. Chạy một truy vấn trên bộ sưu tập

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3.

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" : <NumberDecimal> }

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" : <NumberDecimal> }

2 xác định chế độ xem được vật chất hóa

{ "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

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" : <NumberDecimal> }

3:

  • Giai đoạn

    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 lọc dữ liệu để chỉ xử lý các doanh số được hoàn thành trong
    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);
    };
    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 the
    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);
    };
    5.

  • Giai đoạn

    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 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:

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

  • Giai đoạn

    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 thay đổi loại dữ liệu của trườ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);
    };
    8 thành
    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);
    };
    9. Các nhà khai thác tìm kiếm ATLAS

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    1 không hỗ trợ kiểu dữ liệu

    db.sales.find().sort( {saleDate: -1} )

    1. Thay đổi kiểu dữ liệu của trườ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);
    };
    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 the
    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);
    };
    8 field to
    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);
    };
    9. Atlas Search

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    1 operators don't support the

    db.sales.find().sort( {saleDate: -1} )

    1 data type. Changing the
    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);
    };
    8 field's data type allows you to query this field using Atlas Search indexes.

  • Giai đoạn

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    0 viết đầu ra cho bộ sưu tập

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3.

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    0
    stage writes the output to the

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3 collection.

    Dựa trên trường

    db.sales.find().sort( {saleDate: -1} )

    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 the

    db.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:

    • 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:

  1. 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.

  2. 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.

  3. Trong trường Tên, nhập

    db.sales.find().sort( {saleDate: -1} )

    6 làm tên của hàm.Name field, enter

    db.sales.find().sort( {saleDate: -1} )

    6 as the name of the function.

  4. 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.

  5. Từ thả xuống, chọn cụm Atlas bạn đã tạo trong các điều kiện tiên quyết.

  6. 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:

  1. 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.

  2. Nhấp vào Tạo chức năng mới.Create New Function.

  3. Nhập

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2 làm tên của hàm.

  4. Theo xác thực, chọn hệ thống.Authentication, select System.

  1. Nhấp vào tab Trình chỉnh sửa chức năng.Function Editor tab.

  2. Thêm mã JavaScript vào hàm

    db.sales.find().sort( {saleDate: -1} )

    8. Tối thiểu, mã phải gán một hàm cho biến toàn cầu

    db.sales.find().sort( {saleDate: -1} )

    8:

    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);
    };

  3. 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

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    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" : <NumberDecimal> }

    3 materialized view.

  1. Mở

    db.monthlysales.find().sort( { _id: -1} )

    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 qua

    db.monthlysales.find().sort( { _id: -1} )

    1

    db.monthlysales.find().sort( { _id: -1} )

    1
    in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect via

    db.monthlysales.find().sort( { _id: -1} )

    1

  2. Sử dụng cơ sở dữ liệu

    db.monthlysales.find().sort( { _id: -1} )

    3:

  3. Truy vấn Bộ sưu tập

    db.monthlysales.find().sort( { _id: -1} )

    4. Lưu ý rằng việc bán hàng cuối cùng trong

    db.monthlysales.find().sort( { _id: -1} )

    4 xảy ra vào tháng 12 năm 2017:

    db.sales.find().sort( {saleDate: -1} )

  4. Xác nhận rằng chế độ xem được vật chất hóa đã được tạo trong cơ sở dữ liệu

    db.monthlysales.find().sort( { _id: -1} )

    3 của bạn:

    Lệnh liệt kê các bộ sưu tập của bạn, bao gồm chế độ xem

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3 mới được tạo ra.

  5. Truy vấn quan điểm cụ thể hóa

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3:

    db.monthlysales.find().sort( { _id: -1} )

  6. Cập nhật bộ sưu tập

    db.monthlysales.find().sort( { _id: -1} )

    4 với dữ liệu bán hàng điện thoại mới từ tháng 1 năm 2018.

    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'
    },
    ] )

  7. Truy vấn Bộ sưu tập

    db.monthlysales.find().sort( { _id: -1} )

    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.sales.find().sort( {saleDate: -1} )

  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

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2 làm mới chế độ xem

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    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" : <NumberDecimal> }

    2 function refreshes the

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3 materialized view using the January 2018 sales data.

  2. 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" : <NumberDecimal> }

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" : <NumberDecimal> }

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.

  1. Đặt trường Loại kích hoạt thành lịch trình.Trigger Type field to Scheduled.

  2. Trong trường Tên, nhập

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2.Name field, enter

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2.

  3. Đặt trường Loại lịch trình thành BASIC.Schedule Type field to Basic.

  4. Trong lần lặp lại một lần bằng cách thả xuống, chọn

    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'
    },
    ] )
    7 và đặt giá trị thành ngày ưa thích của bạn.Repeat once by dropdown, select
    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'
    },
    ] )
    7 and set the value to your preferred date.

    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

  5. Đặt trường Chọn loại sự kiện thành hàm.Select An Event Type field to Function.

  6. Trong chức năng thả xuống, chọn

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2.Function dropdown, select

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    2.

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.

  1. Trong trường Tên chỉ mục, nhập

    db.sales.find().sort( {saleDate: -1} )

    0.Index Name field, enter

    db.sales.find().sort( {saleDate: -1} )

    0.

  2. 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" : <NumberDecimal> }

    3.Database and Collection section, find the

    db.monthlysales.find().sort( { _id: -1} )

    3 database, and select the

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

    3 collection.

  3. 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" : <NumberDecimal> }

2.

db.monthlysales.find().sort( { _id: -1} )

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.