Hướng dẫn how do i get only certain fields in mongodb aggregate? - làm cách nào để tôi chỉ nhận được một số trường nhất định trong tổng hợp mongodb?

db.coll.aggregate([
    { "$match": { "year": 2013 } },
    { "$group": {"_id": "$Name", "Link": {$push: "$Link"}, "count": { "$sum": 1 }}},
    { "$project": {"Name": "$_id", _id: 0, "Link": 1, "count": 1}},
    { $sort: {count: 1} }
])

Kết quả trong:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }

Ok, vì vậy, trận đấu $ là chính xác ngoại trừ một lỗi đánh máy có 'năm' -> 'năm'

Nhóm $ có thể được đơn giản hóa một chút. Tôi đã xóa thêm một bộ dấu ngoặc để bạn nhận được ID: 'FirstName' thay vì id: {'name': 'FirstName'} Vì chúng tôi có thể định hình lại _id thành 'tên' trong giai đoạn dự án $.

Bạn cần thêm $ Push hoặc $ Addtoset để duy trì giá trị liên kết $ trong nhóm của bạn. $ addtoset sẽ chỉ cho phép các giá trị duy nhất trong mảng, trong khi $ Push sẽ thêm tất cả các giá trị, vì vậy hãy sử dụng bất kỳ tùy ý nào của bạn.

$ Project và $ Sort là đơn giản, đổi tên và bao gồm/loại trừ bất kỳ trường nào bạn muốn.

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

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9Passes dọc theo các tài liệu với các trường được yêu cầu đến giai đoạn tiếp theo trong đường ống. Các trường được chỉ định có thể là các trường hiện có từ các tài liệu đầu vào hoặc các trường mới được tính toán.

Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 có dạng nguyên mẫu sau:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage has the following prototype form:

{ $project: { } }

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 lấy một tài liệu có thể chỉ định bao gồm các trường, việc triệt tiêu trường

"contact.address.country": <1 or 0 or expression>

2, bổ sung các trường mới và đặt lại các giá trị của các trường hiện có. Ngoài ra, bạn có thể chỉ định loại trừ các trường.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
takes a document that can specify the inclusion of fields, the suppression of the

"contact.address.country": <1 or 0 or expression>

2 field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields.

Thông số kỹ thuật

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 có các biểu mẫu sau:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
specifications have the following forms:

Hình thức

Sự mô tả

"contact.address.country": <1 or 0 or expression>

4

Chỉ định bao gồm một trường. Số nguyên khác không cũng được coi là

"contact.address.country": <1 or 0 or expression>

5.

"contact.address.country": <1 or 0 or expression>

6

Chỉ định sự triệt tiêu của trường

"contact.address.country": <1 or 0 or expression>

2.

Để loại trừ một trường có điều kiện, hãy sử dụng biến

"contact.address.country": <1 or 0 or expression>

8 thay thế. Để biết chi tiết, xem loại trừ các trường một cách có điều kiện.Exclude Fields Conditionally.

"contact.address.country": <1 or 0 or expression>

9

Thêm một trường mới hoặc đặt lại giá trị của một trường hiện có.

Nếu biểu thức đánh giá thành

contact: { address: { country: <1 or 0 or expression> } }

0, trường sẽ được loại trừ trong đầu ra. Để biết chi tiết, xem loại trừ các trường một cách có điều kiện.Exclude Fields Conditionally.

contact: { address: { country: <1 or 0 or expression> } }

1

Chỉ định loại trừ một trường.

Để loại trừ một trường có điều kiện, hãy sử dụng biến

"contact.address.country": <1 or 0 or expression>

8 thay thế. Để biết chi tiết, xem loại trừ các trường một cách có điều kiện.Exclude Fields Conditionally.

Thêm một trường mới hoặc đặt lại giá trị của một trường hiện có.cannot employ any other

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 specification forms. This restriction does not apply to conditionally exclusion of a field using the

"contact.address.country": <1 or 0 or expression>

8 variable.

Nếu biểu thức đánh giá thành

contact: { address: { country: <1 or 0 or expression> } }

0, trường sẽ được loại trừ trong đầu ra. Để biết chi tiết, xem loại trừ các trường một cách có điều kiện.

  • Chỉ định loại trừ một trường.

    { $project: { "": 0, "": 0, ... } } // Return all but the specified fields

    9

  • Nếu bạn chỉ định loại trừ một trường khác ngoài

    "contact.address.country": <1 or 0 or expression>

    2, bạn không thể sử dụng bất kỳ biểu mẫu đặc tả

    { $project: { "": 0, "": 0, ... } } // Return all but the specified fields

    9 nào khác. Hạn chế này không áp dụng để loại trừ một trường có điều kiện bằng cách sử dụng biến

    "contact.address.country": <1 or 0 or expression>

    8.

    { $project: { "": 0, "": 0, ... } } // Return all but the specified fields

    9
    ignores that field inclusion and does not add the field to the document.

Xem thêm giai đoạn

contact: { address: { country: <1 or 0 or expression> } }

6 để loại trừ các trường.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9

Trường

"contact.address.country": <1 or 0 or expression>

2, theo mặc định, được bao gồm trong các tài liệu đầu ra. Để bao gồm bất kỳ trường nào khác từ các tài liệu đầu vào trong các tài liệu đầu ra, bạn phải chỉ định rõ ràng việc đưa vào

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

Nếu bạn chỉ định bao gồm một trường không tồn tại trong tài liệu,

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 bỏ qua việc bao gồm trường đó và không thêm trường vào tài liệu.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
specification forms: i.e. if you exclude fields, you cannot also specify the inclusion of fields, reset the value of existing fields, or add new fields. This restriction does not apply to conditional exclusion of a field using the

"contact.address.country": <1 or 0 or expression>

8 variable.

Nếu biểu thức đánh giá thành

contact: { address: { country: <1 or 0 or expression> } }

0, trường sẽ được loại trừ trong đầu ra. Để biết chi tiết, xem loại trừ các trường một cách có điều kiện.

Chỉ định loại trừ một trường.Conditionally Exclude Fields.

Nếu bạn chỉ định loại trừ một trường khác ngoài "contact.address.country": <1 or 0 or expression>2, bạn không thể sử dụng bất kỳ biểu mẫu đặc tả { $project: { "": 0, "": 0, ... } } // Return all but the specified fields9 nào khác. Hạn chế này không áp dụng để loại trừ một trường có điều kiện bằng cách sử dụng biến "contact.address.country": <1 or 0 or expression>8.

Xem thêm giai đoạn

contact: { address: { country: <1 or 0 or expression> } }

6 để loại trừ các trường.

Trường

"contact.address.country": <1 or 0 or expression>

2, theo mặc định, được bao gồm trong các tài liệu đầu ra. Để bao gồm bất kỳ trường nào khác từ các tài liệu đầu vào trong các tài liệu đầu ra, bạn phải chỉ định rõ ràng việc đưa vào

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9

Nếu bạn chỉ định bao gồm một trường không tồn tại trong tài liệu,

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 bỏ qua việc bao gồm trường đó và không thêm trường vào tài liệu.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
treats the numeric or boolean literal as a flag for including or excluding the field.

Theo mặc định, trường

"contact.address.country": <1 or 0 or expression>

2 được bao gồm trong các tài liệu đầu ra. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra, bạn phải chỉ định rõ ràng sự triệt tiêu của trường

"contact.address.country": <1 or 0 or expression>

2 trong

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9

Nếu bạn chỉ định loại trừ trường hoặc trường, tất cả các trường khác được trả về trong các tài liệu đầu ra.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 stage supports using the square brackets

{ $project: { "contact.address.country": 1, contact: 1 } }

3 to directly create new array fields. If you specify array fields that do not exist in a document, the operation substitutes

{ $project: { "contact.address.country": 1, contact: 1 } }

4 as the value for that field. For an example, see Project New Array Fields.

Nếu bạn chỉ định loại trừ một trường khác với

"contact.address.country": <1 or 0 or expression>

2, bạn không thể sử dụng bất kỳ biểu mẫu đặc tả

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 nào khác: tức là nếu bạn loại trừ các trường, bạn cũng không thể chỉ định bao gồm các trường, hãy đặt lại giá trị của các trường hiện có hoặc thêm các trường mới. Hạn chế này không áp dụng cho việc loại trừ có điều kiện của một trường bằng biến

"contact.address.country": <1 or 0 or expression>

8.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage. See Array Indexes are Unsupported.

Bạn có thể sử dụng biến

"contact.address.country": <1 or 0 or expression>

8 trong các biểu thức tập hợp để triệt tiêu một trường. Ví dụ, xem các trường loại trừ có điều kiện.

"contact.address.country": <1 or 0 or expression>

Ghi chú

contact: { address: { country: <1 or 0 or expression> } }

MongoDB cũng cung cấp

{ $project: { contact: 1, "contact.address.country": 1 } }

9 để thêm các trường mới vào các tài liệu.

Để thêm một trường mới hoặc để đặt lại giá trị của một trường hiện có, chỉ định tên trường và đặt giá trị của nó thành một số biểu thức. Để biết thêm thông tin về biểu thức, xem biểu thức.

Để đặt giá trị trường trực tiếp thành một chữ số hoặc boolean, trái ngược với việc đặt trường thành một biểu thức giải quyết thành một nghĩa đen, hãy sử dụng toán tử

{ $project: { "contact.address.country": 1, contact: 1 } }

0. Mặt khác,

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 coi chữ số hoặc boolean là một lá cờ để bao gồm hoặc loại trừ trường.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage fails with a

{ $project: { "contact.address.country": 1, contact: 1 } }

8 error because it attempts to project both the embedded

{ $project: { "contact.address.country": 1, contact: 1 } }

9 document and the
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
0 field:

{ $project: { contact: 1, "contact.address.country": 1 } }

Bằng cách chỉ định một trường mới và đặt giá trị của nó thành đường dẫn trường của một trường hiện có, bạn có thể đổi tên một cách hiệu quả một trường.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 fails with the same error:

{ $project: { "contact.address.country": 1, contact: 1 } }

Một lỗi được trả về nếu đặc tả

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 là một tài liệu trống.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
specification is an empty document.

Bạn không thể sử dụng một chỉ mục mảng với giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9. Xem các chỉ mục mảng không được hỗ trợ.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage. See Array Indexes are Unsupported.

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây chỉ bao gồm các trường

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage includes only the

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7, and the
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 fields in its output documents:

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

Kết quả hoạt động trong tài liệu sau:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
0

Trường

"contact.address.country": <1 or 0 or expression>

2 luôn được bao gồm theo mặc định. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra của giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9, chỉ định loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 bằng cách đặt nó thành

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 trong tài liệu chiếu.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage, specify the exclusion of the

"contact.address.country": <1 or 0 or expression>

2 field by setting it to

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 in the projection document.

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây chỉ bao gồm các trường

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage excludes the

"contact.address.country": <1 or 0 or expression>

2 field but includes the
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7, and the
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 fields in its output documents:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
2

Kết quả hoạt động trong tài liệu sau:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
3

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
4

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây chỉ bao gồm các trường

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage excludes the
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 field from the output:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
5

Kết quả hoạt động trong tài liệu sau:

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
4

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây chỉ bao gồm các trường

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage excludes the
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
05 and
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 fields from the output:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
7

Kết quả hoạt động trong tài liệu sau:

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
8

Trường

"contact.address.country": <1 or 0 or expression>

2 luôn được bao gồm theo mặc định. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra của giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9, chỉ định loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 bằng cách đặt nó thành

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 trong tài liệu chiếu.

{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
9

Kết quả hoạt động trong tài liệu sau:

Trường

"contact.address.country": <1 or 0 or expression>

2 luôn được bao gồm theo mặc định. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra của giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9, chỉ định loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 bằng cách đặt nó thành

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 trong tài liệu chiếu.

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{ $project: { } }

0

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây chỉ bao gồm các trường

"contact.address.country": <1 or 0 or expression>

2,
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage uses the

"contact.address.country": <1 or 0 or expression>

8 variable to excludes the
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
12 field only if it equals
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
13:

{ $project: { } }

1

Kết quả hoạt động trong tài liệu sau:

{ $project: { } }

2

Trường

"contact.address.country": <1 or 0 or expression>

2 luôn được bao gồm theo mặc định. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra của giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9, chỉ định loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 bằng cách đặt nó thành

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 trong tài liệu chiếu.

{ $project: { } }

3

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 nhưng bao gồm các trường
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { } }

4

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 khỏi đầu ra:

{ $project: { } }

5

Xem thêm giai đoạn

contact: { address: { country: <1 or 0 or expression> } }

6 để loại trừ các trường.

{ $project: { } }

6

Xem xét bộ sưu tập

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
4 với tài liệu sau:

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm các trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
05 và
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 từ đầu ra:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage adds the new fields
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
19,
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
20, and
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
21:

{ $project: { } }

8

Kết quả hoạt động trong tài liệu sau:

{ $project: { } }

9

Trường

"contact.address.country": <1 or 0 or expression>

2 luôn được bao gồm theo mặc định. Để loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 khỏi các tài liệu đầu ra của giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9, chỉ định loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 bằng cách đặt nó thành

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

3 trong tài liệu chiếu.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

0

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây loại trừ trường

"contact.address.country": <1 or 0 or expression>

2 nhưng bao gồm các trường
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
7 và
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
8 trong các tài liệu đầu ra của nó:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

1

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 khỏi đầu ra:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

2

Xem thêm giai đoạn

contact: { address: { country: <1 or 0 or expression> } }

6 để loại trừ các trường.

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm các trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
05 và
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 từ đầu ra:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

3

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 khỏi đầu ra:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

4

Xem thêm giai đoạn

contact: { address: { country: <1 or 0 or expression> } }

6 để loại trừ các trường.

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9
stage. This section shows an example.

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây không bao gồm các trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
05 và
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
01 từ đầu ra:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

5

Ngoài ra, bạn có thể tổ chức đặc tả loại trừ trong một tài liệu:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

6

Cả hai thông số kỹ thuật đều dẫn đến cùng một đầu ra:

Bạn có thể sử dụng biến

"contact.address.country": <1 or 0 or expression>

8 trong các biểu thức tập hợp để triệt tiêu một trường.

Giai đoạn

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

9 sau đây sử dụng biến

"contact.address.country": <1 or 0 or expression>

8 để loại trừ trường
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
12 chỉ khi nó bằng
{ "Link" : [ "www.mylink.com/125" ], "count" : 1, "Name" : "SecondName" }

{ "Link" : [ "www.mylink.com/123", "www.mylink.com/124" ], "count" : 2, "Name" : "FirstName" }
13:

{ $project: { "": 0, "": 0, ... } } // Return all but the specified fields

8

Hoạt động tổng hợp dẫn đến đầu ra sau:

Tra cứu trong tập hợp cho MongoDB là gì?

$ Tra cứu thực hiện một trận đấu bình đẳng trên Localfield với Foreign Field từ các tài liệu của bộ sưu tập. Nếu một tài liệu đầu vào không chứa LocalField, thì $ Tra cứu sẽ coi trường là có giá trị null cho mục đích phù hợp.performs an equality match on the localField to the foreignField from the documents of the from collection. If an input document does not contain the localField , the $lookup treats the field as having a value of null for matching purposes.

Chúng ta có thể sử dụng đếm với chức năng tổng hợp trong MongoDB không?

Tập hợp Count MongoDB $ Các nhà điều hành Count MongoDB $ cho phép chúng tôi chuyển một tài liệu cho giai đoạn tiếp theo của đường ống tổng hợp có chứa số lượng tài liệu.Có một vài điều quan trọng cần lưu ý về cú pháp này: Đầu tiên, chúng tôi gọi toán tử đếm $ và sau đó chỉ định chuỗi.The MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.

Sự khác biệt giữa việc sử dụng tổng hợp () và find () trong MongoDB là gì?

Với tổng hợp + $ khớp, bạn có được một BSON nguyên khối lớn chứa tất cả các tài liệu phù hợp. Với tìm, bạn có một con trỏ cho tất cả các tài liệu phù hợp. Sau đó bạn có thể nhận từng tài liệu một. With find, you get a cursor to all matching documents. Then you can get each document one by one.

Chúng ta có thể chỉ định đồng thời nhiều hơn một hàm tổng hợp trong MongoDB không?

Db.collection.aggregate () có thể sử dụng một số kênh cùng một lúc để xử lý dữ liệu. collection. aggregate () can use several channels at the same time for data processing.