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?
Kết quả trong: Show
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 MongoDB → MongoDB Manual 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 9 có dạng nguyên mẫu sau: 9 stage has the following prototype form:
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 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. 9 takes a document that can specify the inclusion of fields, the suppression of the 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 9 có các biểu mẫu sau: 9 specifications have the following forms:
Xem thêm giai đoạn 6 để loại trừ các trường. 9Trường 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 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, 9 bỏ qua việc bao gồm trường đó và không thêm trường vào tài liệu. 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
8 variable.Nếu biểu thức đánh giá thành 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: { " |
{ |
"_id" : 1, |
title: "abc123", |
isbn: "0001122223334", |
author: { last: "zzz", first: "aaa" }, |
copies: 5 |
} |
{ $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: { "
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" }
0Trườ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" }
2Kế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" }
3Xem 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" }
4Giai đ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" }
5Kế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" }
4Giai đ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" }
7Kế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" }
8Trườ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" }
9Kế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: { } }
0Giai đ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: { } }
1Kết quả hoạt động trong tài liệu sau:
{ $project: { } }
2Trườ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: { } }
3Giai đ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: { } }
4Giai đ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: { } }
5Xem thêm giai đoạn
contact: { address: { country: <1 or 0 or expression> } }
6 để loại trừ các trường.{ $project: { } }
6Xem 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: { } }
8Kết quả hoạt động trong tài liệu sau:
{ $project: { } }
9Trườ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
0Giai đ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
1Giai đ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
2Xem 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
3Giai đ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
4Xem 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
5Ngoà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
6Cả 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
8Hoạt động tổng hợp dẫn đến đầu ra sau: