Hướng dẫn mongodb unwind object - đối tượng thư giãn mongodb

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

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4Deconstructs Một trường mảng từ các tài liệu đầu vào để xuất một tài liệu cho từng phần tử. Mỗi tài liệu đầu ra là tài liệu đầu vào với giá trị của trường mảng được thay thế bằng phần tử.
Hướng dẫn mongodb unwind object - đối tượng thư giãn mongodb

Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.

Bạn có thể vượt qua toán hạng đường dẫn trường hoặc toán hạng tài liệu để thư giãn trường mảng.

Bạn có thể chuyển đường dẫn trường mảng đến

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4. Khi sử dụng cú pháp này,

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 không xuất tài liệu nếu giá trị trường là null, thiếu hoặc một mảng trống.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
. When using this syntax,

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
does not output a document if the field value is null, missing, or an empty array.

{ $unwind: <field path> }

Khi bạn chỉ định đường dẫn trường, tiền tố tên trường có dấu hiệu đô la

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

7 và gửi kèm theo trích dẫn.

Bạn có thể chuyển một tài liệu cho

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 để chỉ định các tùy chọn hành vi khác nhau.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
to specify various behavior options.

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}

Đồng ruộng

Loại hình

Sự mô tả

đường dẫn

sợi dây

Đường dẫn trường đến một trường mảng. Để chỉ định đường dẫn trường, tiền tố tên trường có dấu hiệu đô la

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

7 và đặt trong các trích dẫn.

bao gồm cả hai

sợi dây

Đường dẫn trường đến một trường mảng. Để chỉ định đường dẫn trường, tiền tố tên trường có dấu hiệu đô la

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

7 và đặt trong các trích dẫn.

bao gồm cả hai

boolean

Optional.

  • Không bắt buộc. Tên của một trường mới để giữ chỉ số mảng của phần tử. Tên không thể bắt đầu với một dấu hiệu đô la

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    7.

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4
    outputs the document.

  • bảo quản

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4 does not output a document.

Nếu

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

1, nếu

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

2 là null, thiếu hoặc một mảng trống,

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 sẽ xuất ra tài liệu.

  • Nếu

    db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

    4, nếu

    db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

    2 là null, thiếu hoặc một mảng trống,

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4 không xuất ra một tài liệu.

  • Giá trị mặc định là

    db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

    4.preserveNullAndEmptyArrays option.

Khi toán hạng không giải quyết được một mảng, nhưng không bị thiếu,

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

8 hoặc một mảng trống,

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 coi toán hạng là một mảng phần tử duy nhất.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
, by default, ignores the input document and will not output documents for that input document.

Khi toán hạng là

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

8, bị thiếu hoặc một mảng trống

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 tuân theo hành vi được đặt cho tùy chọn PreservenAllandEmptyArrays.preserveNullAndEmptyArrays option.

Nếu bạn chỉ định một đường dẫn cho một trường không tồn tại trong tài liệu đầu vào hoặc trường là một mảng trống,

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4, theo mặc định, bỏ qua tài liệu đầu vào và sẽ không xuất tài liệu cho tài liệu đầu vào đó.
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3
, create a sample collection named
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
4 with the following document:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

Để xuất các tài liệu nơi trường mảng bị thiếu, null hoặc một mảng trống, hãy sử dụng tùy chọn PreservenAllandEmptyArrays.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 stage to output a document for each element in the
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 array:

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

Trong

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3, hãy tạo một bộ sưu tập mẫu có tên
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
4 với tài liệu sau:

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }

Tập hợp sau đây sử dụng giai đoạn

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 để xuất tài liệu cho từng phần tử trong mảng
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6:

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

db.clothing.insertMany([
{ "_id" : 1, "item" : "Shirt", "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "Shorts", "sizes" : [ ] },
{ "_id" : 3, "item" : "Hat", "sizes": "M" },
{ "_id" : 4, "item" : "Gloves" },
{ "_id" : 5, "item" : "Scarf", "sizes" : null }
])

Mỗi tài liệu giống hệt với tài liệu đầu vào ngoại trừ giá trị của trường

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 hiện có giá trị từ mảng
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 gốc. treats the
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 field as a single element array if:

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

    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    9:

  • db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4 coi trường
    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    6 là một mảng phần tử duy nhất nếu:

  • lĩnh vực này có mặt,

giá trị không phải là null, và

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

Giá trị không phải là một mảng trống.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 operation returns:

{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }

  • Mở rộng các mảng

    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    6 với

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4
    returns a document for each element in the
    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    6 field.

  • Hoạt động

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4 trả về:

  • Trong tài liệu

    db.clothing.insertMany([
    { "_id" : 1, "item" : "Shirt", "sizes": [ "S", "M", "L"] },
    { "_id" : 2, "item" : "Shorts", "sizes" : [ ] },
    { "_id" : 3, "item" : "Hat", "sizes": "M" },
    { "_id" : 4, "item" : "Gloves" },
    { "_id" : 5, "item" : "Scarf", "sizes" : null }
    ])
    5,
    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    6 là một mảng đông dân.

    db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

    4 Trả về một tài liệu cho mỗi phần tử trong trường
    { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
    { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
    6.

Trong tài liệu db.clothing.insertMany([ { "_id" : 1, "item" : "Shirt", "sizes": [ "S", "M", "L"] }, { "_id" : 2, "item" : "Shorts", "sizes" : [ ] }, { "_id" : 3, "item" : "Hat", "sizes": "M" }, { "_id" : 4, "item" : "Gloves" }, { "_id" : 5, "item" : "Scarf", "sizes" : null }])9, { "_id" : 1, "item" : "ABC1", "sizes" : "S" }{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }6 giải quyết cho một mảng phần tử duy nhất.

Tài liệu

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

1 và

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

2 không trả lại bất cứ điều gì vì trường
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 không thể được giảm xuống thành một mảng phần tử.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
operations are equivalent.

db.clothing.aggregate( [ { $unwind: "$sizes" } ] )
db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

Ghi chú

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

6 and

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

7
examples use the following collection:

db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": "M" },
{ "_id" : 4, "item" : "LMN" , price: NumberDecimal("10") },
{ "_id" : 5, "item" : "XYZ", price: NumberDecimal("5.75"), "sizes" : null }
])

Cú pháp

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

4 là tùy chọn. Các hoạt động

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 sau đây là tương đương.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
operation uses the preserveNullAndEmptyArrays option to include documents whose
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 field is null, missing, or an empty array.

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
0

Các ví dụ

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

6 và

db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )

7 Sử dụng bộ sưu tập sau:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
1

Hoạt động

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 sau đây sử dụng tùy chọn PreservenAllandEmptyArrays để bao gồm các tài liệu có trường
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 là null, thiếu hoặc một mảng trống.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
operation uses the includeArrayIndex option to include the array index in the output.

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
2

Đầu ra bao gồm những tài liệu trong đó trường

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 là null, thiếu hoặc một mảng trống:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
3

Hoạt động

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 sau đây sử dụng tùy chọn bao gồm inflay Index để bao gồm chỉ mục mảng vào đầu ra.
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3
, create a sample collection named
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }
8 with the following documents:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
4

Hoạt động thư giãn mảng

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 và bao gồm chỉ mục mảng trong trường
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }
3 mới. Nếu trường
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 không giải quyết được cho một mảng đông dân nhưng không bị thiếu, null hoặc một mảng trống, trường
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }
3 là

db.inventory.aggregate( [ { $unwind : "$sizes" } ] )

8.

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
5

Trong
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3, hãy tạo một bộ sưu tập mẫu có tên
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }
8 với các tài liệu sau:

Đường ống sau khi thư giãn mảng

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 và nhóm các tài liệu kết quả bởi các giá trị kích thước không chắc chắn:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
stage outputs a new document for each element in the
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 array. The stage uses the preserveNullAndEmptyArrays option to include in the output those documents where
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 field is missing, null or an empty array. This stage passes the following documents to the next stage:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
1

Giai đoạn đầu tiên:

Giai đoạn

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 xuất ra một tài liệu mới cho mỗi phần tử trong mảng
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6. Giai đoạn sử dụng tùy chọn PreservenAllandEmptyArrays để đưa vào đầu ra các tài liệu đó trong đó trường
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 bị thiếu, null hoặc một mảng trống. Giai đoạn này chuyển các tài liệu sau đây cho giai đoạn tiếp theo:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
7

Giai đoạn thứ hai:

Giai đoạn

db.clothing.aggregate( [ { $unwind: "$sizes" } ] )
db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )
3 nhóm các tài liệu bằng
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
6 và tính toán giá trung bình của mỗi kích thước. Giai đoạn này chuyển các tài liệu sau đây cho giai đoạn tiếp theo:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
8

Mẹo

Trong

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3, hãy tạo một bộ sưu tập mẫu có tên
db.clothing.aggregate( [ { $unwind: "$sizes" } ] )
db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )
8 với các tài liệu sau:
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
3
, create a sample collection named
db.clothing.aggregate( [ { $unwind: "$sizes" } ] )
db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )
8 with the following documents:

{
$unwind:
{
path: ,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
}
}
9

Các nhóm hoạt động sau đây các mục được bán bởi thẻ của họ và tính toán tổng số tiền bán hàng cho mỗi thẻ.

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

0

Giai đoạn đầu tiên

Giai đoạn

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 đầu tiên xuất ra một tài liệu mới cho mỗi phần tử trong mảng
db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": "M" },
{ "_id" : 4, "item" : "LMN" , price: NumberDecimal("10") },
{ "_id" : 5, "item" : "XYZ", price: NumberDecimal("5.75"), "sizes" : null }
])
0:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
stage outputs a new document for each element in the
db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": "M" },
{ "_id" : 4, "item" : "LMN" , price: NumberDecimal("10") },
{ "_id" : 5, "item" : "XYZ", price: NumberDecimal("5.75"), "sizes" : null }
])
0 array:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

1

Giai đoạn thứ hai

Giai đoạn

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4 thứ hai xuất ra một tài liệu mới cho mỗi phần tử trong mảng
db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": "M" },
{ "_id" : 4, "item" : "LMN" , price: NumberDecimal("10") },
{ "_id" : 5, "item" : "XYZ", price: NumberDecimal("5.75"), "sizes" : null }
])
2:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

4
stage outputs a new document for each element in the
db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": "M" },
{ "_id" : 4, "item" : "LMN" , price: NumberDecimal("10") },
{ "_id" : 5, "item" : "XYZ", price: NumberDecimal("5.75"), "sizes" : null }
])
2 arrays:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

2

Giai đoạn thứ ba

Giai đoạn

db.clothing.aggregate( [ { $unwind: "$sizes" } ] )
db.clothing.aggregate( [ { $unwind: { path: "$sizes" } } ] )
3 nhóm các tài liệu theo thẻ và tính toán tổng số lượng bán hàng với mỗi thẻ:

db.inventory.insertOne({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })

3

Mẹo

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

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