Hướng dẫn what is array in mongodb? - mảng trong mongodb là gì?

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ử $all:

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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
2 chứa ít nhất một phần tử có giá trị lớn hơn
db.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
3.

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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
2 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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
5 và một phần tử khác có thể thỏa mãn điều kiện nhỏ hơn
db.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
6 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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
7 để 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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
2 chứa ít nhất một phần tử vừa hơn (
db.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
9)
db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
0 và nhỏ hơn (
db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
1)
db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
2:

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.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
2 lớn hơn
db.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )
3:

Sử dụng toán tử

db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
5 để 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

db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
7

Xác định nếu toán hạng là một mảng. Trả lại một boolean.

db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
7 có cú pháp sau: has the following syntax:

{ $isArray: [ ] }

db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )
9 có thể là bất kỳ biểu thức hợp lệ. Để biết thêm thông tin về biểu thức, xem biểu thức.

Thí dụ

Kết quả

Ghi chú

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
0

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
1

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
2 là một chuỗi, được truyền dưới dạng chuỗi.

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
3

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
1

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
2 là một chuỗi, được truyền như một phần của mảng đối số.

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
6

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
7

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
8 là một mảng, được truyền như một phần của một mảng đối số.

Ghi chú

Biểu thức tổng hợp chấp nhận một số lượng khác nhau của các đối số. Những đối số này thường được thông qua như một mảng. Tuy nhiên, khi đối số là một giá trị duy nhất, bạn có thể đơn giản hóa mã của mình bằng cách truyền trực tiếp đối số mà không cần quấn nó trong một mảng.

Tạo bộ sưu tập

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
9:

db.warehouses.insertMany( [
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
] )

Kiểm tra xem các trường tags0 và tags1 có phải là mảng không. Nếu cả hai trường là mảng, hãy nối chúng:

db.warehouses.aggregate( [
{ $project:
{ items:
{ $cond:
{
if: { $and: [ { $isArray: "$instock" },
{ $isArray: "$ordered" }
] },
then: { $concatArrays: [ "$instock", "$ordered" ] },
else: "One or more fields is not an array."
}
}
}
}
] )

{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : "One or more fields is not an array." }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }

Mẹo

Mảng và đối tượng trong MongoDB là gì?

Trong cơ sở dữ liệu MongoDB, dữ liệu được lưu trữ trong các bộ sưu tập và một bộ sưu tập có tài liệu. Một tài liệu có các trường và giá trị, như trong JSON. Các loại trường bao gồm các loại vô hướng (chuỗi, số, ngày, v.v.) và các loại tổng hợp (mảng và đối tượng).composite types ( arrays and objects ).

Chúng ta có thể lưu trữ mảng trong MongoDB không?

Một trong những lợi ích của mô hình lược đồ phong phú của MongoDB là khả năng lưu trữ các mảng dưới dạng các giá trị trường tài liệu.Lưu trữ các mảng dưới dạng các giá trị trường cho phép bạn mô hình hóa các mối quan hệ một-nhiều hoặc nhiều đến nhiều trong một tài liệu, thay vì trên các bộ sưu tập riêng biệt như bạn có thể trong cơ sở dữ liệu quan hệ.. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.

Làm thế nào lớn một mảng có thể ở MongoDB?

Giới hạn 16 MB là cho mỗi tài liệu.Khi bạn thực hiện một phần chèn, mảng được truyền dưới dạng đối số cho phương thức giữ nhiều tài liệu - mỗi phần tử mảng là một tài liệu.16 MB limit is for each document. When you do a insertMany the array passed as an argument to the method holds multiple documents - each array element is a document.

Các loại dữ liệu trong MongoDB là gì?

Sau đây là một số loại dữ liệu được sử dụng thường xuyên nhất trong MongoDB ...
Sợi dây.Một trong những loại dữ liệu cơ bản và được sử dụng rộng rãi nhất là chuỗi.....
Số nguyên.Các giá trị số được lưu trữ bằng cách sử dụng kiểu dữ liệu số nguyên.....
Kép.....
Boolean.....
Mảng.....
Sự vật.....
Ngày.....
Timestamp..