Hướng dẫn how do you match an array field in mongodb aggregate? - làm thế nào để bạn khớp một trường mảng trong tổng hợp mongodb?

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0return một boolean cho biết liệu một giá trị được chỉ định có ở trong một mảng hay không.

Returns a boolean indicating whether a specified value is in an array.

Ghi chú

Tài liệu này mô tả toán tử tổng hợp

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0. Đối với toán tử truy vấn
{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0, xem $ in.
{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0
aggregation operator. For the
{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0 query operator, see $in.

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0 có cú pháp biểu thức toán tử sau: has the following operator expression syntax:

{ $in: [ , <array expression> ] }

Thoại

Sự mô tả

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
4

Bất kỳ biểu thức hợp lệ.

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
5

Bất kỳ biểu thức hợp lệ nào giải quyết thành một mảng.

Không giống như toán tử truy vấn

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0, toán tử tổng hợp
{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0 không hỗ trợ khớp với các biểu thức thông thường.
{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0
operator does not support matching by regular expressions.

Thí dụ

Kết quả

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
8

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
9

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
0

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
9

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
2

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
3

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
4

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
3

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
6

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
9

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
8

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])
3

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
0

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
9

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }
0 Không có lỗi trong một trong các trường hợp sau: nếu biểu thức $ trong không được đưa ra chính xác hai đối số hoặc nếu đối số thứ hai không giải quyết được cho một mảng. fails with an error in either of the following cases: if the $in expression is not given exactly two arguments, or if the second argument does not resolve to an array.

Một bộ sưu tập có tên

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
3 có các tài liệu sau:

{ "_id" : 1, "location" : "24th Street",
"in_stock" : [ "apples", "oranges", "bananas" ] }
{ "_id" : 2, "location" : "36th Street",
"in_stock" : [ "bananas", "pears", "grapes" ] }
{ "_id" : 3, "location" : "82nd Street",
"in_stock" : [ "cantaloupes", "watermelons", "apples" ] }

Hoạt động tổng hợp sau đây xem xét mảng

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
4 trong mỗi tài liệu và xác định xem chuỗi
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
5 có mặt hay không.

db.fruit.aggregate([
{
$project: {
"store location" : "$location",
"has bananas" : {
$in: [ "bananas", "$in_stock" ]
}
}
}
])

Hoạt động trả về các kết quả sau:

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6Filters Các tài liệu chỉ truyền các tài liệu phù hợp với (các) điều kiện được chỉ định cho giai đoạn đường ống tiếp theo.

Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

Giai đoạn

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 có dạng nguyên mẫu sau:
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
stage has the following prototype form:

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 lấy một tài liệu chỉ định các điều kiện truy vấn. Cú pháp truy vấn giống hệt với cú pháp truy vấn hoạt động đọc; tức là
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 không chấp nhận các biểu thức tổng hợp thô. Thay vào đó, hãy sử dụng biểu thức truy vấn

{ $match: { $expr: { <aggregation expression> } } }

0 để bao gồm biểu thức tập hợp trong
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 takes a document that specifies the query conditions. The query syntax is identical to the read operation query syntax; i.e.
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
does not accept raw aggregation expressions. Instead, use a

{ $match: { $expr: { <aggregation expression> } } }

0 query expression to include aggregation expression in
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6

  • Đặt

    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 càng sớm trong đường ống tổng hợp càng tốt. Bởi vì
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 giới hạn tổng số tài liệu trong đường ống tổng hợp, trước đó, các hoạt động
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 giảm thiểu số lượng xử lý xuống đường ống.
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    as early in the aggregation pipeline as possible. Because
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    limits the total number of documents in the aggregation pipeline, earlier
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    operations minimize the amount of processing down the pipe.

  • Nếu bạn đặt

    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 vào đầu đường ống, truy vấn có thể tận dụng các chỉ mục như bất kỳ

    { $match: { $expr: { <aggregation expression> } } }

    6 hoặc

    { $match: { $expr: { <aggregation expression> } } }

    7 nào khác
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    at the very beginning of a pipeline, the query can take advantage of indexes like any other

    { $match: { $expr: { <aggregation expression> } } }

    6 or

    { $match: { $expr: { <aggregation expression> } } }

    7

  • Cú pháp truy vấn

    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 giống hệt với cú pháp truy vấn hoạt động đọc; tức là
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 không chấp nhận các biểu thức tổng hợp thô. Để bao gồm biểu thức tập hợp trong
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6, hãy sử dụng biểu thức truy vấn

    { $match: { $expr: { <aggregation expression> } } }

    0:
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    query syntax is identical to the read operation query syntax; i.e.
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    does not accept raw aggregation expressions. To include aggregation expression in
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    , use a

    { $match: { $expr: { <aggregation expression> } } }

    0 query expression:

    { $match: { $expr: { <aggregation expression> } } }

  • Bạn không thể sử dụng

    { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
    { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
    2 trong các truy vấn
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 như một phần của đường ống tổng hợp.
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    queries as part of the aggregation pipeline.

  • Bạn không thể sử dụng

    { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
    { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
    4 hoặc
    { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
    { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
    { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
    { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
    5 trong các truy vấn
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 như một phần của đường ống tổng hợp. Thay vào đó, bạn có thể:
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    queries as part of the aggregation pipeline. As an alternative, you can either:

    • Sử dụng giai đoạn

      { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
      { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
      { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
      { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
      7 thay vì giai đoạn
      { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
      { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
      { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
      6.
      { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
      { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
      { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
      6
      stage.

    • Sử dụng toán tử truy vấn

      { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
      { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
      { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
      { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
      { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
      9 với
      db.articles.aggregate(
      [ { $match : { author : "dave" } } ]
      );
      0 hoặc
      db.articles.aggregate(
      [ { $match : { author : "dave" } } ]
      );
      1 trong giai đoạn
      { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
      { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
      { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
      6.
      { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
      { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
      { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
      6
      stage.

  • Để sử dụng

    db.articles.aggregate(
    [ { $match : { author : "dave" } } ]
    );
    3 trong giai đoạn
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6, giai đoạn
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6 phải là giai đoạn đầu tiên của đường ống.
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    stage, the
    { "_id" : 1, "store location" : "24th Street", "has bananas" : true }
    { "_id" : 2, "store location" : "36th Street", "has bananas" : true }
    { "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
    6
    stage has to be the first stage of the pipeline.

    Lượt xem không hỗ trợ tìm kiếm văn bản.

Các ví dụ sử dụng một bộ sưu tập có tên

db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);
6 với các tài liệu sau:

{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }

Hoạt động sau sử dụng

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 để thực hiện phù hợp với bình đẳng đơn giản:
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
to perform a simple equality match:

db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 chọn các tài liệu trong đó trường
db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);
9 bằng
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
0 và tổng hợp trả về như sau:
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
selects the documents where the
db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);
9 field equals
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
0, and the aggregation returns the following:

{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }

Ví dụ sau đây chọn các tài liệu để xử lý bằng toán tử đường ống

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 và sau đó đưa kết quả vào toán tử đường ống
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
2 để tính toán số lượng tài liệu:
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
pipeline operator and then pipes the results to the
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
2 pipeline operator to compute a count of the documents:

db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );

Trong đường ống tổng hợp,

{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 chọn các tài liệu trong đó
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
4 lớn hơn
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
5 và nhỏ hơn
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
6 hoặc
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
7 lớn hơn hoặc bằng
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
8. Các tài liệu này sau đó được đưa đến
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
2 để thực hiện đếm. Tổng hợp trả về những điều sau:
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
selects the documents where either the
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
4 is greater than
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
5 and less than
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
6 or the
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
7 is greater than or equal to
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
8. These documents are then piped to the
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
2 to perform a count. The aggregation returns the following:

{ "_id" : null, "count" : 5 }

Tham khảo các trang sau để biết thêm thông tin và sử dụng các trường hợp về tổng hợp.

Đối với các truy vấn

db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0 của bạn đối với dữ liệu trên cụm Atlas của bạn, bạn có thể sử dụng tùy chọn Toán tử Atlas SearchCompound
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1 để khớp hoặc lọc tài liệu. Chạy
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6 sau
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0 ít hoạt động hơn so với chạy
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0 với tùy chọn toán tử tổng hợp
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1. Để tìm hiểu thêm về tùy chọn
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1, xem Hợp chất.
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0
queries against data on your Atlas cluster, you can use the Atlas Search compound operator
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1 option to match or filter documents. Running
{ "_id" : 1, "store location" : "24th Street", "has bananas" : true }
{ "_id" : 2, "store location" : "36th Street", "has bananas" : true }
{ "_id" : 3, "store location" : "82nd Street", "has bananas" : false }
6
after
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0
is less performant than running
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
0
with the compound operator
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1 option. To learn more about the
db.articles.aggregate( [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
1 option, see compound.

  • Tập hợp với tập dữ liệu mã zip

  • Tập hợp với dữ liệu ưu tiên người dùng