Hướng dẫn what is an array in mongodb? - một 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

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
3 là một mảng có chính xác hai phần tử,
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
4 và
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
5, 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ử

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
4 và
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
5, 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ử
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
8:

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

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
3 là một mảng chứa chuỗi
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
4 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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
1 chứa ít nhất một phần tử có giá trị lớn hơn
db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
2.

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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
1 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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
4 và một phần tử khác có thể thỏa mãn điều kiện nhỏ hơn
db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
5 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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
6 để 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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
1 chứa ít nhất một phần tử vừa hơn (
db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
8)
db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
9 và nhỏ hơn (
NEW_COMMENT = {
  user: "DB Learner",
  text: "Nice post, can I know more about the arrays in MongoDB?",
  updated: ISODate()
}

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $push: { comments: NEW_COMMENT } }
)
0)
NEW_COMMENT = {
  user: "DB Learner",
  text: "Nice post, can I know more about the arrays in MongoDB?",
  updated: ISODate()
}

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $push: { comments: NEW_COMMENT } }
)
1:

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.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
1 lớn hơn
db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
2:

Sử dụng toán tử

NEW_COMMENT = {
  user: "DB Learner",
  text: "Nice post, can I know more about the arrays in MongoDB?",
  updated: ISODate()
}

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $push: { comments: NEW_COMMENT } }
)
4 để 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
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
3 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

Các toán tử mảng trả về dữ liệu dựa trên các điều kiện mảng.

Ghi chú

Để biết chi tiết về một nhà điều hành cụ thể, bao gồm cú pháp và ví dụ, nhấp vào liên kết đến trang tham chiếu của nhà điều hành.

Tên

Sự mô tả

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
8

Các mảng phù hợp có chứa tất cả các yếu tố được chỉ định trong truy vấn.

db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
6

Chọn tài liệu nếu phần tử trong trường Mảng khớp với tất cả các điều kiện

db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}
6 được chỉ định.

NEW_COMMENT = {
  user: "DB Learner",
  text: "Nice post, can I know more about the arrays in MongoDB?",
  updated: ISODate()
}

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $push: { comments: NEW_COMMENT } }
)
4

Chọn tài liệu nếu trường mảng là một kích thước được chỉ định.

Để biết các ví dụ về các trường 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

Xuất bản ngày 25 tháng 5 năm 2020 Cập nhật ngày 02 tháng 6 năm 2020

1. Giới thiệu

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 (

{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
0,
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
1,
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
2, v.v.) và các loại tổng hợp (
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
3 và
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
4). Trong bài viết này, chúng tôi sẽ xem xét một ví dụ về việc sử dụng loại trường Array.collections and a collection has documents. A document has fields and values, like in a JSON. The field types include scalar types (
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
0,
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
1,
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
2, etc.) and composite types (
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
3 and
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
4). In this article we will look at an example of using the array field type.

Ví dụ là một ứng dụng nơi người dùng tạo bài đăng trên blog và viết nhận xét cho các bài đăng. Mối quan hệ giữa các bài viết và bình luận là một-nhiều; tức là, một bài đăng có thể có nhiều ý kiến. Chúng tôi sẽ xem xét một bộ sưu tập các bài đăng trên blog với ý kiến ​​của họ. Đó là một tài liệu bài cũng sẽ lưu trữ các ý kiến ​​liên quan. Trong mô hình tài liệu của MongoDB, dữ liệu mối quan hệ 1: N có thể được lưu trữ trong một bộ sưu tập; Đây là một hình thức dữ liệu không chuẩn hóa. Các dữ liệu liên quan được lưu trữ cùng nhau và có thể được truy cập (và cập nhật) cùng nhau. Các ý kiến ​​được lưu trữ dưới dạng một mảng; Một mảng các đối tượng bình luận.

Một tài liệu mẫu của các bài đăng trên blog với nhận xét:

{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content ...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [ "mongodb", "arrays" ],
        "comments" : [
                {
                        "user" : "DB Learner",
                        "content" : "Nice post.",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}

Trong một ứng dụng, một bài đăng trên blog được tạo, nhận xét được thêm vào, truy vấn, sửa đổi hoặc bị xóa bởi người dùng. Trong ví dụ, chúng tôi sẽ viết mã để tạo một tài liệu bài đăng trên blog và thực hiện một số hoạt động CRUD với các bình luận cho bài đăng.

2. Tạo và truy vấn một tài liệu

Hãy tạo một tài liệu bài đăng trên blog. Chúng tôi sẽ sử dụng cơ sở dữ liệu có tên là

{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
5 và một bộ sưu tập được gọi là
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
6. Mã được viết bằng
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
7SHELL (giao diện JavaScript tương tác với MongoDB). Mongo Shell được bắt đầu từ dòng lệnh và được kết nối với máy chủ MongoDB. Từ vỏ:

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)

Trả về kết quả

{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
8 chỉ ra rằng một tài liệu mới được tạo. Đây là một sự thừa nhận phổ biến khi bạn thực hiện một hoạt động viết. Khi một tài liệu được chèn vào một bộ sưu tập lần đầu tiên, bộ sưu tập sẽ được tạo (nếu nó không tồn tại). Phương thức
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}
9 chèn một tài liệu vào bộ sưu tập.

Bây giờ, hãy truy vấn bộ sưu tập:query the collection :

db.posts.findOne()
{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        "user" : "Database Rebel",
        "desc" : "Maintaining an array of objects in a document",
        "content" : "some content...",
        "created" : ISODate("2020-05-20T16:28:55.468Z"),
        "updated" : ISODate("2020-05-20T16:28:55.468Z"),
        "tags" : [
                "mongodb",
                "arrays"
        ]
}

Phương pháp

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
0 lấy một tài liệu phù hợp từ bộ sưu tập. Lưu ý các trường vô hướng
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
1 (loại chuỗi) và
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
2 (loại ngày) và trường mảng
use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
3. Trong tài liệu mới được chèn không có ý kiến.

3. Thêm phần tử mảng

Hãy thêm một nhận xét cho bài đăng này, bởi người dùng "DB người học":

NEW_COMMENT = {
  user: "DB Learner",
  text: "Nice post, can I know more about the arrays in MongoDB?",
  updated: ISODate()
}

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $push: { comments: NEW_COMMENT } }
)

Trả lại:

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
4

Phương thức

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
5 cập nhật các trường của tài liệu dựa trên điều kiện được chỉ định.
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
6 là toán tử cập nhật mảng thêm một phần tử vào một mảng. Nếu mảng không tồn tại, nó sẽ tạo một trường mảng và sau đó thêm phần tử.array update operator which adds an element to an array. If the array doesn't exist, it creates an array field and then adds the element.

Hãy truy vấn bộ sưu tập và xác nhận nhận xét mới một cách trực quan, sử dụng phương thức

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
0:

{
        "_id" : ObjectId("5ec55af811ac5e2e2aafb2b9"),
        "name" : "Working with Arrays",
        ...
        "comments" : [
                {
                        "user" : "DB Learner",
                        "text" : "Nice post, can I know more about the arrays in MongoDB?",
                        "updated" : ISODate("2020-05-20T16:35:57.461Z")
                }
        ]
}

Lưu ý trường

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
8 có các đối tượng bình luận là các yếu tố. Hãy thêm một bình luận nữa bằng cách sử dụng cùng một toán tử cập nhật
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
6. Nhận xét mới này (bởi
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
0 "Cơ sở dữ liệu Rebel") được thêm vào mảng
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
8:

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]

4. Cập nhật phần tử mảng

Hãy cập nhật nhận xét được đăng bởi "Cơ sở dữ liệu Rebel" với trường

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
2 đã sửa đổi:
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
3.
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
3.

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)

Toán tử cập nhật

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
4 được sử dụng để thay đổi giá trị của trường. Toán tử $ định vị xác định một phần tử trong một mảng để cập nhật mà không chỉ định rõ ràng vị trí của phần tử trong mảng. Phần tử khớp đầu tiên được cập nhật. Đối tượng nhận xét được cập nhật:update operator is used to change a field's value. The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array. The first matching element is updated. The updated comment object:

"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]

5. Xóa phần tử mảng

Người dùng đã thay đổi suy nghĩ của mình và muốn xóa bình luận, và sau đó thêm một bình luận mới.

db.posts.updateOne( 
  { _id" : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $pull: { comments: { user: "Database Rebel" } } }
)

Toán tử cập nhật

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
5 loại bỏ các phần tử khỏi một mảng phù hợp với điều kiện được chỉ định - trong trường hợp này là
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
6.update operator removes elements from an array which match the specified condition - in this case
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
6.

Một nhận xét mới được thêm vào mảng sau khi hoạt động xóa ở trên, với văn bản sau:

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
7.

6. Thêm một trường mới vào tất cả các đối tượng trong mảng

Hãy thêm một trường mới

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8 cho tất cả các nhận xét trong mảng.

db.posts.updateOne( 
  { "_id : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $set: { "comments.$[].likes": 0 } }
)

Tất cả toán tử vị trí

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
9 chỉ định rằng toán tử cập nhật
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
4 sẽ sửa đổi tất cả các phần tử trong trường mảng được chỉ định. Sau khi cập nhật, tất cả các đối tượng bình luận đều có trường
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8, ví dụ:all positional operator
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
9 specifies that the update operator
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
4 should modify all elements in the specified array field. After the update, all comment objects have the
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8 field, for example:

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
0

7. Cập nhật phần tử mảng cụ thể dựa trên điều kiện

Trước tiên, hãy thêm một nhận xét mới khác bằng toán tử cập nhật

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
6:

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
1

Lưu ý trường

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8 bị thiếu trong tài liệu đầu vào. Chúng tôi sẽ cập nhật nhận xét cụ thể này trong mảng
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
8 với điều kiện trường
db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8 bị thiếu.

use blogs

NEW_POST =
  { 
    name: "Working with Arrays",
    user: "Database Rebel",
    desc: "Maintaining an array of objects in a document",
    content: "some content...",
    created: ISODate(),
    updated: ISODate(),
    tags: [ "mongodb", "arrays" ]
}

db.posts.insertOne(NEW_POST)
2

Trường

db.posts.updateOne( 
  { _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
  { $set: { "comments.$.text": NEW_CONTENT } }
)
8 được cập nhật bằng toán tử cập nhật
"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]
7 (điều này tăng giá trị của trường hoặc nếu không tồn tại sẽ thêm trường và sau đó tăng). Toán tử vị trí được lọc
"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]
8 xác định các phần tử mảng phù hợp với các điều kiện
"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]
9 cho hoạt động cập nhật.update operator (this increments a field's value, or if not exists adds the field and then increments). The filtered positional operator
"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]
8 identifies the array elements that match the
"comments" : [
                {
                        "user" : "Database Rebel",
                        "text" : "Thank you, please look for updates - updated",
                        "updated" : ISODate("2020-05-20T16:48:25.506Z")
                }
 ]
9 conditions for an update operation.


8. Kết luận

Bài viết này cho thấy thực hiện các hoạt động CRUD cơ bản trên một loạt các đối tượng:

  • Tạo và thêm các yếu tố vào một mảng.
  • Truy vấn, cập nhật và xóa một phần tử mảng.
  • Cập nhật tất cả các phần tử mảng hoặc một yếu tố cụ thể dựa trên một điều kiện.

MongoDB cũng cho phép lập chỉ mục các phần tử mảng - trong trường hợp này, các trường của các đối tượng nhận xét của mảng

"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
8. Ví dụ: nếu bạn đang truy vấn các bình luận của
db.posts.updateOne( 
  { _id" : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $pull: { comments: { user: "Database Rebel" } } }
)
1 và cần truy cập nhanh, bạn có thể tạo một chỉ mục cho trường đó. Các chỉ mục trên các trường mảng được gọi là các chỉ mục multikey. Các chỉ mục trường mảng có thể là một phần của các chỉ mục hợp chất.indexing the array elements - in this case, fields of the comment objects of the
"comments" : [
        {
            "user" : "DB Learner",
            "text" : "Nice post, can I know more about the arrays in MongoDB?",
            "updated" : ISODate("2020-05-20T16:35:57.461Z")
        },
        {
            "user" : "Database Rebel",
            "text" : "Thank you, please look for updates",
            "updated" : ISODate("2020-05-20T16:48:25.506Z")
        }
]
8 array. For example, if you are querying on the comments by
db.posts.updateOne( 
  { _id" : ObjectId("5ec55af811ac5e2e2aafb2b9") },
  { $pull: { comments: { user: "Database Rebel" } } }
)
1 and need fast access, you can create an index for that field. Indexes on array fields are called as Multikey Indexes. Array field indexes can be part of Compound Indexes.

Chúng tôi có thể thực hiện nhiều hoạt động khác trên các mảng - dự đoán, truy vấn và cập nhật - sử dụng các toán tử khác nhau cũng như đường ống tổng hợp. Ngoài ra, thực hiện các hoạt động này trên các mảng lồng nhau (mảng trong mảng). Chúng tôi sẽ thấy một số tính năng này trong một bài viết sau.operations on the arrays - projections, querying and updating - using various operators as well as the Aggregation Pipeline. Also, perform these operations on nested arrays (arrays within arrays). We will see some of these features in a later article.


8.1. Liên kết hữu ích

  • Vỏ Mongo
  • db.collection.InsertOne
  • db.collection.updateOne
  • db.collection.findOne
  • Các nhà khai thác cập nhật: $ set, $ push, $ pull, $ inc, $, $ [], $ []

Bạn có thể có một mảng trong MongoDB?

Không giống như các mô hình cơ sở dữ liệu quan hệ, các tài liệu MongoDB có thể có các trường có giá trị dưới dạng mảng.Ví dụ nguyên mẫu trong hầu hết các tài liệu MongoDB là một tài liệu có trường thẻ, có giá trị là một loạt các chuỗi, chẳng hạn như ["NoQuery", "Ruby", "MongoDB"].MongoDB documents can have fields which have values as arrays. The prototypical example in almost all MongoDB documentation is a document having a tags field, whose value is an array of strings, such as ["NoSQL", "Ruby", "MongoDB"] .

Làm thế nào để tôi tìm thấy một mảng trong MongoDB?

Để tìm kiếm mảng đối tượng trong MongoDB, bạn có thể sử dụng toán tử elemmatch $.Toán tử này cho phép chúng tôi tìm kiếm nhiều hơn một thành phần từ một đối tượng mảng.use $elemMatch operator. This operator allows us to search for more than one component from an array object.

Là mảng MongoDB được đặt hàng?

Yep MongoDB giữ thứ tự của mảng .. giống như động cơ javascript .... just like Javascript engines..

Chỉ số mảng trong MongoDB ở đâu?

Bắt đầu từ MongoDB phiên bản 3.4, chúng ta có thể sử dụng toán tử $ indexofArray để trả về chỉ mục mà tại đó một phần tử nhất định có thể được tìm thấy trong mảng.$ indexofArray có ba đối số.Đầu tiên là tên của trường Array có tiền tố với $ Sign.use the $indexOfArray operator to return the index at which a given element can be found in the array. $indexOfArray takes three arguments. The first is the name of the array field prefixed with $ sign.