Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $lte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } } }
3 Chọn một tập hợp con của một mảng để trả về dựa trên điều kiện được chỉ định. Trả về một mảng chỉ với các yếu tố phù hợp với điều kiện. Các yếu tố trả về theo thứ tự ban đầu.Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
3 có cú pháp sau: has the following syntax:{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $lte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } } }
{ $filter: { input: , cond: , as: , limit: } }
| Một biểu thức giải quyết thành một mảng. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Một biểu thức phân giải thành giá trị boolean được sử dụng để xác định xem một phần tử có nên được đưa vào mảng đầu ra hay không. Biểu thức tham chiếu từng phần tử của mảng | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Không bắt buộc. Một tên cho biến đại diện cho từng phần tử riêng lẻ của mảng | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Không bắt buộc. Một biểu thức số hạn chế số lượng các thành phần mảng phù hợp mà 3 trả về. Bạn không thể chỉ định giới hạn nhỏ hơn 4. Các phần tử mảng phù hợp được trả về theo thứ tự chúng xuất hiện trong mảng đầu vào. 3 returns. You cannot specify a limit less than 4. The matching array elements are returned in the order they appear in the input array.Nếu 2 được chỉ định lớn hơn số lượng phần tử mảng phù hợp, 3 trả về tất cả các phần tử mảng phù hợp. Nếu giới hạn là 7, 3 sẽ trả về tất cả các phần tử mảng phù hợp. 3 returns all matching array elements. If the limit is 7, 3 returns all matching array elements.
|
Để biết thêm thông tin về biểu thức, xem biểu thức.
|
| ||||||||||||||||||||||
|
| ||||||||||||||||||||||
|
|
Bộ sưu tập
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
2 có các tài liệu sau:db.sales.insertMany[ [ { _id: 0, items: [ { item_id: 43, quantity: 2, price: 10 }, { item_id: 2, quantity: 1, price: 240 } ] }, { _id: 1, items: [ { item_id: 23, quantity: 3, price: 110 }, { item_id: 103, quantity: 4, price: 5 }, { item_id: 38, quantity: 1, price: 300 } ] }, { _id: 2, items: [ { item_id: 4, quantity: 1, price: 23 } ] } ] ]
Ví dụ sau đây lọc mảng
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
3 chỉ bao gồm các tài liệu có { $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
4 lớn hơn hoặc bằng { $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
5:db.sales.aggregate[ [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $gte: [ "$$item.price", 100 ] } } } } } ] ]
Hoạt động tạo ra các kết quả sau:
{ "_id" : 0, "items" : [ { "item_id" : 2, "quantity" : 1, "price" : 240 } ] } { "_id" : 1, "items" : [ { "item_id" : 23, "quantity" : 3, "price" : 110 }, { "item_id" : 38, "quantity" : 1, "price" : 300 } ] } { "_id" : 2, "items" : [ ] }
Ví dụ này sử dụng bộ sưu tập
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
2 từ ví dụ trước.Ví dụ sử dụng trường
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: 2 } }
2 để xác định số lượng các thành phần phù hợp được trả về trong mỗi mảng { $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
3.db.sales.aggregate[ [ { $project: { items: { $filter: { input: "$items", cond: { $gte: [ "$$item.price", 100 ] }, as: "item", limit: 1 } } } } ] ]
Hoạt động tạo ra các kết quả sau:
{ "_id" : 0, "items" : [ { "item_id" : 2, "quantity" : 1, "price" : 240 } ] } { "_id" : 1, "items" : [ { "item_id" : 23, "quantity" : 3, "price" : 110 } ] } { "_id" : 2, "items" : [ ] }
Ví dụ này sử dụng bộ sưu tập
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
2 từ ví dụ trước.Ví dụ sử dụng trường
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: 2 } }
2 để xác định số lượng các thành phần phù hợp được trả về trong mỗi mảng { $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
3.db.sales.aggregate[ [ { $project: { items: { $filter: { input: "$items", cond: { $lte: [ "$$item.price", 150] }, as: "item", limit: 2.000 } } } } ] ]
Hoạt động tạo ra các kết quả sau:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $lte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } } }
0Ví dụ này sử dụng bộ sưu tập
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
2 từ ví dụ trước.Ví dụ sử dụng trường
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: 2 } }
2 để xác định số lượng các thành phần phù hợp được trả về trong mỗi mảng { $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and:[ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $gte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } limit: { $add: [ 0, 1 ]} } }
3.{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $lte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } } }
1Hoạt động tạo ra các kết quả sau:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong[4], "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong["-9223372036854775807"] ] }, { $lte: [ "$$num", NumberLong["9223372036854775807"] ] } ] } } }
2