Hướng dẫn find in array mongodb - tìm trong mảng mongodb

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

Trên trang này

  • Khớp với một mảng
  • Truy vấn một mảng cho một phần tử
  • Chỉ định nhiều điều kiện cho các phần tử mảng
  • Hướng dẫn truy vấn bổ sung

Sử dụng menu chọn ngôn ngữ của bạn ở phía trên bên phải để đặt ngôn ngữ của các ví dụ sau.Select your language drop-down menu in the upper-right to set the language of the following examples.

Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó giá trị trường tags là một mảng có chính xác hai phần tử, "red""blank", theo thứ tự được chỉ định:

Thay vào đó, nếu bạn muốn tìm một mảng chứa cả hai phần tử "red""blank", mà không liên quan đến thứ tự hoặc các phần tử khác trong mảng, hãy sử dụng toán tử

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
2:

Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó tags là một mảng chứa chuỗi "red" là một trong các yếu tố của nó:

Ví dụ: các truy vấn hoạt động sau đây cho tất cả các tài liệu trong đó mảng

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
5 chứa ít nhất một phần tử có giá trị lớn hơn
db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
6.

Khi chỉ định các điều kiện hợp chất trên các phần tử mảng, bạn có thể chỉ định truy vấn sao cho một phần tử mảng duy nhất đáp ứng các điều kiện này hoặc bất kỳ sự kết hợp nào của các phần tử mảng đáp ứng các điều kiện.

Các truy vấn ví dụ sau đây cho các tài liệu trong đó mảng

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
5 chứa các yếu tố trong một số kết hợp thỏa mãn các điều kiện truy vấn; ví dụ: một yếu tố có thể thỏa mãn điều kiện lớn hơn
db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
8 và một phần tử khác có thể thỏa mãn điều kiện nhỏ hơn
db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
9 hoặc một phần tử duy nhất có thể thỏa mãn cả hai:

Sử dụng toán tử

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

0 để chỉ định nhiều tiêu chí về các phần tử của một mảng sao cho ít nhất một phần tử mảng đáp ứng tất cả các tiêu chí được chỉ định.

Các truy vấn ví dụ sau đây cho các tài liệu trong đó mảng

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
5 chứa ít nhất một phần tử vừa hơn [

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

2]

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

3 và nhỏ hơn [

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

4]

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

5:

Sử dụng ký hiệu DOT, bạn có thể chỉ định các điều kiện truy vấn cho một phần tử tại một chỉ mục hoặc vị trí cụ thể của mảng. Mảng sử dụng lập chỉ mục dựa trên không.

Ghi chú

Khi truy vấn sử dụng ký hiệu DOT, trường và trường lồng nhau phải ở bên trong dấu ngoặc kép.

Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó phần tử thứ hai trong mảng

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
5 lớn hơn
db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]
6:

Sử dụng toán tử

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

8 để truy vấn cho các mảng theo số phần tử. Ví dụ: các tài liệu chọn sau đây trong đó mảng tags có 3 yếu tố.

Để biết các ví dụ truy vấn bổ sung, xem:

  • Tài liệu truy vấn

  • Truy vấn trên các tài liệu nhúng/lồng nhau

  • Truy vấn một loạt các tài liệu nhúng

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

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 Trình điều khiển
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 chọn các tài liệu trong đó giá trị của một trường bằng bất kỳ giá trị nào trong mảng được chỉ định. Để chỉ định biểu thức
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0, hãy sử dụng nguyên mẫu sau:

The

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 operator selects the documents where the value of a field equals any value in the specified array. To specify an
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
expression, use the following prototype:

{ field: { $in: [, , ... ] } }

Để so sánh các giá trị loại BSON khác nhau, hãy xem thứ tự so sánh BSON được chỉ định.

Nếu

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
3 giữ một mảng, thì toán tử
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 chọn các tài liệu có
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
3 giữ một mảng chứa ít nhất một phần tử phù hợp với giá trị trong mảng được chỉ định [ví dụ:
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
6,
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
7, v.v.].
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
operator selects the documents whose
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
3 holds an array that contains at least one element that matches a value in the specified array [for example,
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
6,
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
7, and so on].

Toán tử

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 so sánh từng tham số với từng tài liệu trong bộ sưu tập, có thể dẫn đến các vấn đề về hiệu suất. Để cải thiện hiệu suất:
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
operator compares each parameter to each document in the collection, which can lead to performance issues. To improve performance:

  • Bạn nên giới hạn số lượng tham số được chuyển cho toán tử

    { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
    { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
    0 đến hàng chục giá trị. Sử dụng hàng trăm tham số trở lên có thể tác động tiêu cực đến hiệu suất truy vấn.
    { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
    { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
    0
    operator to tens of values. Using hundreds of parameters or more can negatively impact query performance.

  • Tạo một chỉ mục trên

    { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
    { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
    3 bạn muốn truy vấn.

Ghi chú

Tạo bộ sưu tập

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
1:

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]

Xem xét ví dụ sau:

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

Truy vấn này chọn tất cả các tài liệu trong bộ sưu tập

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
1 trong đó giá trị của trường
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
3 là 5 hoặc 15.

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }

Mặc dù bạn có thể viết truy vấn này bằng toán tử

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
4, hãy sử dụng toán tử
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 thay vì toán tử
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
4 khi thực hiện kiểm tra bình đẳng trên cùng một trường.
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
operator rather than the
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
4 operator when performing equality checks on the same field.

Hoạt động

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
7 sau đây đặt trường
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
8 thành
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
9 khi mảng tags có ít nhất một phần tử khớp với
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
1 hoặc
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
2.

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]

Ví dụ đầu ra:

{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}

Để biết các ví dụ bổ sung trong các mảng truy vấn, xem:

  • Truy vấn một mảng

  • Truy vấn một loạt các tài liệu nhúng

Để biết các ví dụ bổ sung trong truy vấn, xem:

  • Tài liệu truy vấn

Toán tử

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0 có thể chỉ định các giá trị phù hợp bằng cách sử dụng các biểu thức thông thường của mẫu
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
4. Bạn không thể sử dụng các biểu thức toán tử
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
5 bên trong
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0
operator can specify matching values using regular expressions of the form
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
4. You cannot use
{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}
5 operator expressions inside an
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
0

Xem xét ví dụ sau:

db.inventory.find[ { tags: { $in: [ /^be/, /^st/ ] } } ]

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

Truy vấn này chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
1 trong đó giá trị của trường
db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]
3 là 5 hoặc 15.

Bài Viết Liên Quan

Chủ Đề