Hướng dẫn mongodb aggregation add element to array - tập hợp mongodb thêm phần tử vào mảng

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

$push$push Trả về một mảng tất cả các giá trị xuất phát từ việc áp dụng một biểu thức cho các tài liệu.

$push returns an array of all values that result from applying an expression to documents.

$push có sẵn trong các giai đoạn này: is available in these stages:

  • $bucket

  • db.sales.aggregate(
    [
    { $sort: { date: 1, item: 1 } },
    {
    $group:
    {
    _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
    itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
    }
    }
    ]
    )
    0

  • db.sales.aggregate(
    [
    { $sort: { date: 1, item: 1 } },
    {
    $group:
    {
    _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
    itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
    }
    }
    ]
    )
    1

  • db.sales.aggregate(
    [
    { $sort: { date: 1, item: 1 } },
    {
    $group:
    {
    _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
    itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
    }
    }
    ]
    )
    2 (có sẵn bắt đầu từ MongoDB 5.0)

$push Cú pháp: syntax:

Để biết thêm thông tin về biểu thức, xem biểu thức.

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

db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
4 với các tài liệu sau:

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-15T12:05:10Z") }
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T14:12:12Z") }

Nhóm các tài liệu vào ngày và năm của trường

db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
5, hoạt động sau đây sử dụng bộ tích lũy $push để tính toán danh sách các mục và số lượng được bán cho mỗi nhóm:$push accumulator to compute the list of items and quantities sold for each group:

db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)

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

{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}

Mới trong phiên bản 5.0.

Tạo một bộ sưu tập

db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
7 có chứa doanh số bán bánh ở các bang California (
db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
8) và Washington (
db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
9):

db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )

Ví dụ này sử dụng $push trong giai đoạn

db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
2 để xuất ra một loạt các giá trị bán bánh
{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
2 cho mỗi
{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
3:$push in the
db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
2 stage to output an array of cake sales
{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
2 values for each
{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
3:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
quantitiesForState: {
$push: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

Trong ví dụ:

  • {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    4 Phân vùng các tài liệu trong bộ sưu tập của
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    3. Có các phân vùng cho
    db.sales.aggregate(
    [
    { $sort: { date: 1, item: 1 } },
    {
    $group:
    {
    _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
    itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
    }
    }
    ]
    )
    8 và
    db.sales.aggregate(
    [
    { $sort: { date: 1, item: 1 } },
    {
    $group:
    {
    _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
    itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
    }
    }
    ]
    )
    9.

  • {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    8 sắp xếp các tài liệu trong mỗi phân vùng theo
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    9 theo thứ tự tăng dần (
    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    0), vì vậy
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    9 sớm nhất là đầu tiên.

  • db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    2 Đặt mảng của các giá trị
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    2 bằng cách sử dụng $push cho các tài liệu trong cửa sổ tài liệu.$push for the documents in a documents window.

    Cửa sổ chứa các tài liệu giữa giới hạn thấp hơn

    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    5 và tài liệu
    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    6 trong đầu ra. Điều này có nghĩa là $push nối các giá trị
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    2 vào mảng
    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    9 cho các tài liệu giữa đầu phân vùng và tài liệu hiện tại.$push appends the
    {
    "_id" : { "day" : 46, "year" : 2014 },
    "itemsSold" : [
    { "item" : "abc", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 10 },
    { "item" : "xyz", "quantity" : 5 },
    { "item" : "xyz", "quantity" : 10 }
    ]
    }
    {
    "_id" : { "day" : 34, "year" : 2014 },
    "itemsSold" : [
    { "item" : "jkl", "quantity" : 1 },
    { "item" : "xyz", "quantity" : 5 }
    ]
    }
    {
    "_id" : { "day" : 1, "year" : 2014 },
    "itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
    }
    2 values to the
    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    9 array for the documents between the beginning of the partition and the current document.

Trong đầu ra này, mảng của các giá trị

{
"_id" : { "day" : 46, "year" : 2014 },
"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}
{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
2 cho
db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
8 và
db.sales.aggregate(
[
{ $sort: { date: 1, item: 1 } },
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]
)
9 được hiển thị trong mảng
db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )
9:

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "quantitiesForState" : [ 162 ] }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "quantitiesForState" : [ 162, 120 ] }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "quantitiesForState" : [ 162, 120, 145 ] }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "quantitiesForState" : [ 134 ] }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "quantitiesForState" : [ 134, 104 ] }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "quantitiesForState" : [ 134, 104, 140 ] }

Làm cách nào để đẩy một yếu tố vào một mảng trong MongoDB?

Trong MongoDB, toán tử Push $ được sử dụng để nối một giá trị được chỉ định vào một mảng. Nếu trường được đề cập không có trong tài liệu để cập nhật, toán tử Push $ thêm nó dưới dạng trường mới và bao gồm giá trị được đề cập làm yếu tố của nó. Nếu trường cập nhật không phải là trường loại mảng thì hoạt động không thành công.the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

Làm cách nào để thêm một trường trong tập hợp MongoDB?

Bạn có thể bao gồm một hoặc nhiều giai đoạn $ AddFields trong một hoạt động tổng hợp. Để thêm trường hoặc trường vào các tài liệu nhúng (bao gồm các tài liệu trong mảng) sử dụng ký hiệu dấu chấm. Xem ví dụ. Để thêm một phần tử vào một trường mảng hiện có với $ addfields, hãy sử dụng với $ concatarrays.use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .

Root $$ là gì?

$$ Root.Biến gốc $$ chứa các tài liệu nguồn cho nhóm.Nếu bạn muốn chuyển chúng qua không sửa đổi, bạn có thể thực hiện điều này bằng cách $ đẩy root $$ vào đầu ra từ nhóm.contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

Tập hợp MongoDB có nhanh không?

Trên các bộ sưu tập lớn hàng triệu tài liệu, tập hợp của MongoDB được chứng minh là tồi tệ hơn nhiều so với Elaticsearch.Hiệu suất xấu đi với kích thước thu thập khi MongoDB bắt đầu sử dụng đĩa do RAM hệ thống hạn chế.Giai đoạn tra cứu $ được sử dụng mà không có chỉ số có thể rất chậm.. Performance worsens with collection size when MongoDB starts using the disk due to limited system RAM. The $lookup stage used without indexes can be very slow.