Hướng dẫn how do i filter a field in mongodb? - làm cách nào để lọc một trường trong mongodb?

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB 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.
Hướng dẫn how do i filter a field in mongodb? - làm cách nào để lọc một trường trong mongodb?

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.

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3 có cú pháp sau: has the following syntax:

{
$filter:
{
input: <array>,
cond: <expression>,
as: <string>,
limit: <number expression>
}
}

Đồng ruộng

Sự chỉ rõ

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
5

Một biểu thức giải quyết thành một mảng.

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
6

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

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
5 riêng lẻ với tên biến được chỉ định trong
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
8.

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
8

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

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
5. Nếu không có tên nào được chỉ định, tên biến mặc định là
{
$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
}
}
1.

{
$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

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à

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3 trả về. Bạn không thể chỉ định giới hạn nhỏ hơn
{
$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
}
}
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.
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3
returns. You cannot specify a limit less than
{
$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
}
}
4. The matching array elements are returned in the order they appear in the input array.

Nếu

{
$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 được chỉ định lớn hơn số lượng phần tử mảng phù hợp,
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3 trả về tất cả các phần tử mảng phù hợp. Nếu giới hạn là
{
$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
}
}
7,
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3 sẽ trả về tất cả các phần tử mảng phù hợp.
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3
returns all matching array elements. If the limit is
{
$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
}
}
7,
{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
3
returns all matching array elements.

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

Thí dụ

Kết quả

{
$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: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and:[
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $gte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
limit: 2
}
}
9

{
$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
}
}

{
$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 ]}
}
}
0

{
$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 ]}
}
}

{
$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 ]}
}
}
1

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") ] }
] }
}
}
0

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.

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}
1

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") ] }
] }
}
}
2

Làm cách nào để lọc các yếu tố trong MongoDB?

Bộ lọc phần tử mảng MongoDB bằng toán tử $ Filter Toán tử này sử dụng ba biến: Đầu vào - Điều này thể hiện mảng mà chúng tôi muốn trích xuất. Cond - Điều này đại diện cho tập hợp các điều kiện phải được đáp ứng. AS - Trường tùy chọn này chứa một tên cho biến đại diện cho từng phần tử của mảng đầu vào.Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

Làm cách nào để chọn một trường cụ thể trong MongoDB?

Bạn có thể chọn một trường duy nhất trong MongoDB bằng cú pháp sau: db.yourcollectionName.find ({"yourFieldName": yourValue}, {"yoMingleFieldFieldName": 1, _id: 0});db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

Làm cách nào để lọc một chuỗi trong MongoDB?

Bằng cách kết hợp việc sử dụng hàm tương tự với ký tự đại diện, chúng ta có thể tìm thấy tất cả các giá trị bắt đầu, kết thúc hoặc chứa một chuỗi ở một vị trí nhất định.Các nhà phát triển đã làm việc với SQL Server rất quen thuộc với % đại diện cho 0 hoặc bất kỳ số lượng ký tự nào.Bạn nhận được ý tưởng., we can find all values that start, end, or contain a string at a given position. Developers who had worked with SQL Server are well familiar with the % wildcard that represents 0 or any number of characters. You get the idea.

Bộ lọc MongoDB hoạt động như thế nào?

Bộ lọc MongoDB được sử dụng để lọc dữ liệu từ một mảng sử dụng điều kiện được chỉ định mà chúng tôi đã sử dụng trong truy vấn của mình.Nếu mảng tài liệu chứa giá trị trống thì kết quả sử dụng toán tử bộ lọc sẽ trả về mảng trống.used to filter the data from an array using the specified condition which was we have used in our query. If the document array which was contains the empty value then the result using the filter operator will return the empty array.