Hướng dẫn mongodb aggregation gte - mongodb tổng hợp gte

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

$gteComparing hai giá trị và trả về:
Hướng dẫn mongodb aggregation gte - mongodb tổng hợp gte

Compares two values and returns:

  • { "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
    { "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
    { "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
    { "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
    { "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
    0 Khi giá trị đầu tiên lớn hơn hoặc tương đương với giá trị thứ hai.

  • { "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
    { "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
    { "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
    { "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
    { "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
    1 khi giá trị thứ nhất nhỏ hơn giá trị thứ hai.

$gte so sánh cả giá trị và loại, sử dụng thứ tự so sánh BSON được chỉ định cho các giá trị của các loại khác nhau.$gte compares both value and type, using the specified BSON comparison order for values of different types.

$gte có cú pháp sau: has the following syntax:

{ $gte: [ <expression1>, <expression2> ] }

Để 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

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
4 với các tài liệu sau:

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }

Hoạt động sau sử dụng toán tử $gte để xác định xem

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
6 có lớn hơn hay bằng
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
7:$gte operator to determine if
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
6 is greater than or equal to
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
7:

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)

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

{ "item" : "abc1", "qty" : 300, "qtyGte250" : true }
{ "item" : "abc2", "qty" : 200, "qtyGte250" : false }
{ "item" : "xyz1", "qty" : 250, "qtyGte250" : true }
{ "item" : "VWZ1", "qty" : 300, "qtyGte250" : true }
{ "item" : "VWZ2", "qty" : 180, "qtyGte250" : false }

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

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8Binds các biến để sử dụng trong biểu thức được chỉ định và trả về kết quả của biểu thức.

Binds variables for use in the specified expression, and returns the result of the expression.

Biểu thức

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8 có cú pháp sau:
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8
expression has the following syntax:

{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}

Đồng ruộng

Sự chỉ rõ

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0

Khối gán cho các biến có thể truy cập trong biểu thức

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1. Để gán một biến, chỉ định một chuỗi cho tên biến và gán một biểu thức hợp lệ cho giá trị.

Các bài tập biến không có ý nghĩa bên ngoài biểu thức

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1, thậm chí không nằm trong khối
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0.

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1

Các biểu thức để đánh giá.

Để truy cập các biến trong các biểu thức tổng hợp, tiền tố tên biến với các dấu hiệu đô la kép (

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
5) và được đặt trong các trích dẫn. Để biết thêm thông tin về biểu thức, xem biểu thức. Để biết thông tin về việc sử dụng các biến trong đường ống tổng hợp, hãy xem các biến trong các biểu thức tổng hợp.

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8 có thể truy cập các biến được xác định bên ngoài khối biểu thức của nó, bao gồm các biến hệ thống. can access variables defined outside its expression block, including system variables.

Nếu bạn sửa đổi các giá trị của các biến được xác định bên ngoài trong khối

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0, các giá trị mới chỉ có hiệu lực trong biểu thức
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1. Ngoài biểu thức
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1, các biến giữ lại các giá trị trước đó của chúng.

Trong khối gán

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0, thứ tự của bài tập không quan trọng và các bài tập biến chỉ có ý nghĩa bên trong biểu thức
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1. Do đó, việc truy cập giá trị của một biến trong khối gán
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0 đề cập đến giá trị của biến được xác định bên ngoài khối
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0 và không bên trong cùng một khối
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0.not matter, and the variable assignments only have meaning inside the
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
1 expression. As such, accessing a variable's value in the
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0 assignment block refers to the value of the variable defined outside the
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0 block and not inside the same
db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0 block.

Ví dụ, hãy xem xét biểu thức

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8 sau:
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8
expression:

{
$let:
{
vars: { low: 1, high: "$$low" },
in: { $gt: [ "$$low", "$$high" ] }
}
}

Trong khối gán

db.inventory.aggregate(
[
{
$project:
{
item: 1,
qty: 1,
qtyGte250: { $gte: [ "$qty", 250 ] },
_id: 0
}
}
]
)
0,
{ "item" : "abc1", "qty" : 300, "qtyGte250" : true }
{ "item" : "abc2", "qty" : 200, "qtyGte250" : false }
{ "item" : "xyz1", "qty" : 250, "qtyGte250" : true }
{ "item" : "VWZ1", "qty" : 300, "qtyGte250" : true }
{ "item" : "VWZ2", "qty" : 180, "qtyGte250" : false }
7 đề cập đến giá trị của biến được xác định bên ngoài
{ "item" : "abc1", "qty" : 300, "qtyGte250" : true }
{ "item" : "abc2", "qty" : 200, "qtyGte250" : false }
{ "item" : "xyz1", "qty" : 250, "qtyGte250" : true }
{ "item" : "VWZ1", "qty" : 300, "qtyGte250" : true }
{ "item" : "VWZ2", "qty" : 180, "qtyGte250" : false }
8 và không phải là biến được xác định trong cùng một khối ____20. Nếu
{ "item" : "abc1", "qty" : 300, "qtyGte250" : true }
{ "item" : "abc2", "qty" : 200, "qtyGte250" : false }
{ "item" : "xyz1", "qty" : 250, "qtyGte250" : true }
{ "item" : "VWZ1", "qty" : 300, "qtyGte250" : true }
{ "item" : "VWZ2", "qty" : 180, "qtyGte250" : false }
8 không được xác định bên ngoài khối biểu thức
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8 này, biểu thức không hợp lệ.
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8
expression block, the expression is invalid.

Bộ sưu tập

{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}
2 có các tài liệu sau:

{ _id: 1, price: 10, tax: 0.50, applyDiscount: true }
{ _id: 2, price: 10, tax: 0.25, applyDiscount: false }

Tập hợp sau đây sử dụng

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8 trong giai đoạn đường ống
{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}
4 để tính toán và trả về
{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}
5 cho mỗi tài liệu:
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }
8
in the
{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}
4 pipeline stage to calculate and return the
{
$let:
{
vars: { <var1>: <expression>, ... },
in: <expression>
}
}
5 for each document:

db.sales.aggregate( [
{
$project: {
finalTotal: {
$let: {
vars: {
total: { $add: [ '$price', '$tax' ] },
discounted: { $cond: { if: '$applyDiscount', then: 0.9, else: 1 } }
},
in: { $multiply: [ "$$total", "$$discounted" ] }
}
}
}
}
] )

Tập hợp trả về các kết quả sau:

________số 8

Mẹo

Xem thêm: