Mongodb đếm theo ngày

Cùng với sự phát triễn của các hệ thống lớn, NOSQL đang dần trở thành xu thế tất yếu đối với các hệ thống yêu cầu xử lý số lượng dữ liệu lớn, yêu cầu truy vấn nhanh và có thể mở rộng dễ dàng. MongoDB đang dần trở thành một trong những hệ thống cơ sở dữ liệu tốt nhất cho công việc này. Có rất nhiều tính năng nổi bật trong quá trình xử lý dữ liệu trong mongoDb, tuy nhiên nổi bật nhất vẫn là Aggregation. Đây là một tính năng rất hữu ích được sử dụng để tính toán số lượng dữ liệu lớn trên các tài liệu tập hợp (tương tự như bảng trong SQL). Bài viết này sẽ giới thiệu cơ bản về các toán tử và cách thức hoạt động của Aggregation

Nội dung chính Hiển thị

  • open start
  • tổng quan
  • Nguyên tắc hoạt động
  • Một số ví dụ về các toán tử trong Aggregation
  • toán tử $match
  • nhà điều hành dự án $
  • nhà điều hành dự án $
  • nhà điều hành dự án $
  • nhà điều hành dự án $
  • Kết hợp các toán tử
  • Toán tử $Match + Toán tử $Project
  • Kết hợp các toán tử
  • Kết hợp các toán tử
  • Toán tử $Match + Toán tử $Group + Toán tử $Sort + Toán tử $limit
  • Toán tử $Match + Toán tử $Group + Toán tử $Sort + Toán tử $Out

tổng quan

Khung tổng hợp là một truy vấn nâng cao của MongoDb, cho phép thực hiện tính toán, xử lý và kết hợp từ nhiều tài liệu (tương tự như các bảng trong SQL) để lấy thông tin cần thiết. Ví dụ. Chúng ta có các tài liệu. bán hàng, sản phẩm và người dùng, chúng ta có thể sử dụng Aggregation framework để tính toán thông tin từ 3 bảng này như danh số bán trong tháng này, danh số theo sản phẩm hoặc theo người dùng. Có thể cấu hình Aggregation tương tự như lệnh GROUP BY trong SQL

Nguyên tắc hoạt động

Khi thực hiện theo tác động với Aggregation framework, về nguyên tắc Aggregation sẽ thực hiện xử lý dựa trên các aggregation pipeline. Mỗi bước thực hiện một tính toán duy nhất trong các dữ liệu đầu vào và tạo dữ liệu đầu ra. Để hiểu rõ hơn chúng ta có thể nhìn luồng xử lý phía dưới

Mongodb đếm theo ngày

Nhìn vào chúng ta có thể thấy , khi một tài liệu được đưa vào đường dẫn Tổng hợp để xử lý. Việc thực hiện sẽ được thực hiện theo tuần tự. Thao tác 1 -> Thao tác 2. -> Hoạt động N. Đầu ra của Thao tác 1 sẽ là đầu vào của Thao tác 2 và cứ thế. Cuối cùng sau khi xử lý xong dữ liệu sẽ được xuất ra

Some Operation basic in Aggregation

dự án $. only the field mong muốn truy vấn. . only the field mong muốn truy vấn

$match. select document mong muốn truy vấn. . select document mong muốn truy vấn

giới hạn $. giới hạn số lượng tài liệu. giới hạn số lượng tài liệu

bỏ qua $. bỏ qua tài liệu nhất định. bỏ qua tài liệu nhất định

nhóm $. nhóm các tài liệu theo điều kiện nhất định. nhóm các tài liệu theo điều kiện nhất định

$sắp xếp. sắp xếp tài liệu. sắp xếp tài liệu

thư giãn $. thực hiện thao tác mở rộng trên một mảng , tạo một tài liệu đầu ra cho mỗi giá trị trong mảng đó. thực hiện thao tác mở rộng trên một mảng , tạo một tài liệu đầu ra cho mỗi giá trị trong mảng đó

$ ra. ghi kết quả sau khi thực hiện trên đường dẫn vào một bộ sưu tập. (chỉ áp dụng cho phiên bản 2. 6 trở đi). ghi kết quả sau khi thực hiện trên đường dẫn vào một bộ sưu tập. (chỉ áp dụng cho phiên bản 2. 6 trở đi)

Dưới đây là một đường dẫn khuôn khổ tổng hợp ví dụ khi thực hiện theo dựa trên các thuật toán so khớp , nhóm và sắp xếp

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
7

Các toán tử sẽ được thực hiện như mô tả dưới đây

Hình trên khi thực hiện tính toán trên sản phẩm bộ sưu tập

1. Hoạt động đầu tiên $match sẽ thực thi. $match will select a number of document on input

2. Sau khi thực hiện thao tác $match, các giá trị từ kết quả đầu ra của $match sẽ được sử dụng để bắt đầu cho hoạt động $group. Toán tử này có nhiệm vụ nhóm các kết quả đầu ra theo điều kiện cụ thể ví dụ như tính tổng hoặc trung bình cộng. $match will be used to start for operation $group. Toán tử này có nhiệm vụ nhóm các kết quả đầu ra theo điều kiện cụ thể ví dụ như tính tổng hoặc trung bình cộng

3. Đầu ra của hoạt động $group sẽ được sử dụng làm đầu vào cho hoạt động $sort. Operation $Sort có nhiệm vụ sắp xếp các kết quả (theo điều kiện cho trước) và trả về kết quả cuối $sort. Operation $Sort có nhiệm vụ sắp xếp các kết quả (theo điều kiện trước đó) và trả về kết quả cuối cùng

Bảng so sánh giữa SQL và aggregation framework

Lệnh SQLAggregation framework operatorSELECT$project $group functions. $sum, $min, $avg, v.v. TỪdb. tên bộ sưu tập. tổng hợp (. ) THAM GIA$unwindGROUP BY$groupHAVING$match

Một số ví dụ về các toán tử trong Aggregation

Tập hợp sử dụng rất nhiều toán tử để tính toán trên các tài liệu xác định. Bài viết này sẽ chỉ đề cập đến nhà điều hành mới nhất thường xuyên được sử dụng nhất

Dữ liệu được sử dụng cho các toán tử tính toán dưới bài viết

bộ sưu tập tên. Dữ liệu khách hàng. Dữ liệu khách hàng

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}

toán tử $match

Đây là một nhà điều hành rất phổ biến và được sử dụng nhiều nhất. Mục đích chính của nhà điều hành này là xác định những tài liệu phù hợp với điều kiện cho trước

cú pháp

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
8

Câu lệnh dưới đây sẽ thực hiện việc chọn tài liệu với khách hàng ở thành phố. SalemSalem

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);

Kết quả

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

nhà điều hành dự án $

Chỉ định các trường cần thiết khi thực hiện truy vấn dựa trên đầu vào

cú pháp

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
9

Ví dụ. Câu lệnh bên dưới sẽ chỉ lấy các trường địa chỉ , thành phố , tiểu bang từ khách hàng thu thập

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}
0

Kết quả

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "state" : "MA"
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "state" : "MA"
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "state" : "MA"
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "state" : "MA"
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "state" : "NH"
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "state" : "MA"
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "state" : "MA"
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "state" : "NH"
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "state" : "MA"
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "state" : "NH"
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "state" : "MA"
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "state" : "NH"
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "state" : "MA"
}

nhà điều hành dự án $

Chỉ định các trường cần thiết khi thực hiện truy vấn dựa trên đầu vào

cú pháp

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
9
{
  $group:
    {
      _id: , // Group By Expression
      : {  :  },
      ...
    }
 }

Ví dụ. Câu lệnh bên dưới sẽ chỉ lấy các trường địa chỉ , thành phố , tiểu bang từ khách hàng thu thập

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}
0

Kết quả

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}
1

nhà điều hành dự án $

Chỉ định các trường cần thiết khi thực hiện truy vấn dựa trên đầu vào

cú pháp

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
9
/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}
3

Kết quả

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}
4

nhà điều hành dự án $

Chỉ định các trường cần thiết khi thực hiện truy vấn dựa trên đầu vào

Ví dụ. Lệnh bên dưới sẽ thực hiện sắp xếp các kết quả theo thứ tự tăng dần của mã bưu điện và giảm dần của fed_id và thực hiện lấy 2 bảng ghi đầu tiên

/* 1 */
{
    "_id" : ObjectId("549988b74c71737b38c32991"),
    "address" : "47 Mockingbird Ln",
    "city" : "Lynnfield",
    "cust_type_cd" : "I",
    "fed_id" : "111-11-1111",
    "postal_code" : "01940",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989904c71737b38c329a2"),
        "first_name" : "John",
        "last_name" : "Chilton",
        "start_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 10
    },
    "individual" : {
        "birth_date" : ISODate("1972-04-21T17:00:00.000Z"),
        "first_name" : "James",
        "last_name" : "Hadley",
        "_id" : ObjectId("549989924c71737b38c329a6")
    }
}

/* 2 */
{
    "_id" : ObjectId("549988b74c71737b38c32992"),
    "address" : "372 Clearwater Blvd",
    "city" : "Woburn",
    "cust_type_cd" : "I",
    "fed_id" : "222-22-2222",
    "postal_code" : "01801",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989944c71737b38c329b4"),
        "first_name" : "Paul",
        "last_name" : "Hardy",
        "start_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 11
    },
    "individual" : {
        "birth_date" : ISODate("1968-08-14T17:00:00.000Z"),
        "first_name" : "Susan",
        "last_name" : "Tingley",
        "_id" : ObjectId("549989954c71737b38c329b8")
    }
}

/* 3 */
{
    "_id" : ObjectId("549988b74c71737b38c32993"),
    "address" : "18 Jessup Rd",
    "city" : "Quincy",
    "cust_type_cd" : "I",
    "fed_id" : "333-33-3333",
    "postal_code" : "02169",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("549989964c71737b38c329c6"),
        "first_name" : "Carl",
        "last_name" : "Lutz",
        "start_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 12
    },
    "individual" : {
        "birth_date" : ISODate("1958-02-05T17:00:00.000Z"),
        "first_name" : "Frank",
        "last_name" : "Tucker",
        "_id" : ObjectId("549989994c71737b38c329ca")
    }
}

/* 4 */
{
    "_id" : ObjectId("549988b74c71737b38c32994"),
    "address" : "12 Buchanan Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "444-44-4444",
    "postal_code" : "02451",
    "state" : "MA",
    "officer" : {
        "_id" : ObjectId("5499899a4c71737b38c329d8"),
        "first_name" : "Stanley",
        "last_name" : "Cheswick",
        "start_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "title" : "President",
        "cust_id" : 13
    },
    "individual" : {
        "birth_date" : ISODate("1966-12-21T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Hayward",
        "_id" : ObjectId("5499899b4c71737b38c329dc")
    }
}

/* 5 */
{
    "_id" : ObjectId("549988b74c71737b38c32995"),
    "address" : "2341 Main St",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "555-55-5555",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1971-08-24T17:00:00.000Z"),
        "first_name" : "Charles",
        "last_name" : "Frasier",
        "_id" : ObjectId("5499899c4c71737b38c329ee")
    }
}

/* 6 */
{
    "_id" : ObjectId("549988b74c71737b38c32996"),
    "address" : "12 Blaylock Ln",
    "city" : "Waltham",
    "cust_type_cd" : "I",
    "fed_id" : "666-66-6666",
    "postal_code" : "02451",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1962-09-13T17:00:00.000Z"),
        "first_name" : "John",
        "last_name" : "Spencer",
        "_id" : ObjectId("5499899e4c71737b38c32a00")
    }
}

/* 7 */
{
    "_id" : ObjectId("549988b74c71737b38c32997"),
    "address" : "29 Admiral Ln",
    "city" : "Wilmington",
    "cust_type_cd" : "I",
    "fed_id" : "777-77-7777",
    "postal_code" : "01887",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1947-03-18T17:00:00.000Z"),
        "first_name" : "Margaret",
        "last_name" : "Young",
        "_id" : ObjectId("5499899f4c71737b38c32a12")
    }
}

/* 8 */
{
    "_id" : ObjectId("549988b74c71737b38c32998"),
    "address" : "472 Freedom Rd",
    "city" : "Salem",
    "cust_type_cd" : "I",
    "fed_id" : "888-88-8888",
    "postal_code" : "03079",
    "state" : "NH",
    "individual" : {
        "birth_date" : ISODate("1977-06-30T17:00:00.000Z"),
        "first_name" : "Louis",
        "last_name" : "Blake",
        "_id" : ObjectId("549989a14c71737b38c32a24")
    }
}

/* 9 */
{
    "_id" : ObjectId("549988b74c71737b38c32999"),
    "address" : "29 Maple St",
    "city" : "Newton",
    "cust_type_cd" : "I",
    "fed_id" : "999-99-9999",
    "postal_code" : "02458",
    "state" : "MA",
    "individual" : {
        "birth_date" : ISODate("1968-06-15T17:00:00.000Z"),
        "first_name" : "Richard",
        "last_name" : "Farley",
        "_id" : ObjectId("549989a24c71737b38c32a36")
    }
}

/* 10 */
{
    "_id" : ObjectId("549988b74c71737b38c3299a"),
    "address" : "7 Industrial Way",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-1111111",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("1995-04-30T17:00:00.000Z"),
        "name" : "Chilton Engineering",
        "state_id" : "12-345-678",
        "_id" : ObjectId("549989a34c71737b38c32a37")
    }
}

/* 11 */
{
    "_id" : ObjectId("549988b74c71737b38c3299b"),
    "address" : "287A Corporate Ave",
    "city" : "Wilmington",
    "cust_type_cd" : "B",
    "fed_id" : "04-2222222",
    "postal_code" : "01887",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("2000-12-31T17:00:00.000Z"),
        "name" : "Northeast Cooling Inc.",
        "state_id" : "23-456-789",
        "_id" : ObjectId("549989a54c71737b38c32a49")
    }
}

/* 12 */
{
    "_id" : ObjectId("549988b74c71737b38c3299c"),
    "address" : "789 Main St",
    "city" : "Salem",
    "cust_type_cd" : "B",
    "fed_id" : "04-3333333",
    "postal_code" : "03079",
    "state" : "NH",
    "business" : {
        "incorp_date" : ISODate("2002-06-29T17:00:00.000Z"),
        "name" : "Superior Auto Body",
        "state_id" : "34-567-890",
        "_id" : ObjectId("549989a64c71737b38c32a5b")
    }
}

/* 13 */
{
    "_id" : ObjectId("549988b74c71737b38c3299d"),
    "address" : "4772 Presidential Way",
    "city" : "Quincy",
    "cust_type_cd" : "B",
    "fed_id" : "04-4444444",
    "postal_code" : "02169",
    "state" : "MA",
    "business" : {
        "incorp_date" : ISODate("1999-04-30T17:00:00.000Z"),
        "name" : "AAA Insurance Inc.",
        "state_id" : "45-678-901",
        "_id" : ObjectId("549989a84c71737b38c32a6d")
    }
}
5

Lưu ý. 1 tương ứng với tăng dần và -1 giảm dần

Kết quả

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
0

Kết hợp các toán tử

Trên thực tế công việc thao tác và làm việc với Aggregation là sự kết hợp của nhiều toán tử khác nhau để có thể đưa ra kết quả mong muốn. Dưới đây là một số ví dụ

Toán tử $Match + Toán tử $Project

Lệnh bên dưới sẽ thực hiện lọc ra các khách hàng ở thành phố là Woburn và bang là MA. Kết quả sẽ được hiển thị chỉ với 2 địa chỉ trường và sĩ quan

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
1

Kết quả

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
2

Kết hợp các toán tử

Trên thực tế công việc thao tác và làm việc với Aggregation là sự kết hợp của nhiều toán tử khác nhau để có thể đưa ra kết quả mong muốn. Dưới đây là một số ví dụ

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
3

Kết quả

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
4

Kết hợp các toán tử

Trên thực tế công việc thao tác và làm việc với Aggregation là sự kết hợp của nhiều toán tử khác nhau để có thể đưa ra kết quả mong muốn. Dưới đây là một số ví dụ

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
5

Toán tử $Match + Toán tử $Project

Lệnh bên dưới sẽ thực hiện lọc ra các khách hàng ở thành phố là Woburn và bang là MA. Kết quả sẽ được hiển thị chỉ với 2 địa chỉ trường và sĩ quan

db.Customer.aggregate(
    [ { $match : { city : "Salem" } } ]
);
6

Toán tử $Match + Toán tử $Group + Toán tử $Sort + Toán tử $limit

Câu lệnh phía dưới là sự kết hợp của 4 toán tử. Khớp, Nhóm, Sắp xếp và Giới hạn. Câu lệnh này sẽ tìm kiếm các khách hàng với trạng thái là. MA , sau đó sẽ nhóm các khách hàng vừa tìm được theo thành phố và đếm xem có bao nhiêu khách hàng ở mỗi thành phố. Tiếp theo thực hiện sắp xếp các kết quả vừa tìm được theo Post_code theo thứ tự tăng dần và lấy 5 kết quả đầu tiên được tìm thấy.