Hướng dẫn match array mongodb - khớp mongodb mảng

Nội dung bài viết

Video học lập trình mỗi ngày

Mongodb là gì? Query array trong mongodb? Thì trong bài này, tôi sẽ giới thiệu một số kỹ thuật truy vấn khi document có chứa một số field được lưu trữ có dạng array. ở đây có rất nhiều cách trình bày, nhưng nó luôn được tối ưu nhất, giúp cho devjs sử dụng mongodb truy vấn một cách nhanh nhất.

Bài viết này ví dụ về các hoạt động truy vấn trên các trường mảng bằng cách sử dụng collection.find() và MongoDB Node.js Driver. Trước tiên, chúng ta thử insert data để truy vấn

Giả sử bạn đã  Install Mongodb 

Insert mongodb

await db.collection('inventory').insertMany([
  {
    item: 'journal',
    qty: 25,
    tags: ['blank', 'red'],
    dim_cm: [14, 21]
  },
  {
    item: 'notebook',
    qty: 50,
    tags: ['red', 'blank'],
    dim_cm: [14, 21]
  },
  {
    item: 'paper',
    qty: 100,
    tags: ['red', 'blank', 'plain'],
    dim_cm: [14, 21]
  },
  {
    item: 'planner',
    qty: 75,
    tags: ['blank', 'red'],
    dim_cm: [22.85, 30]
  },
  {
    item: 'postcard',
    qty: 45,
    tags: ['blue'],
    dim_cm: [10, 15.25]
  }
]);

Tips:  Find mongoose - sử dụng async await thay thế cho callback 

Match an Array mongodb

Ví dụ 1: Hãy tìm tất cả documents có field tags chứa chính xác "red" and "blank";

const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});

Kết quả:

Hướng dẫn match array mongodb - khớp mongodb mảng

Ví dụ 2: Hãy tìm tất cả documents có field tags chứa một trong hai "red" and "blank";

db.inventory.find( { tags: { $all: ["red", "blank"] } } )

Kết quả

Tips:  Database integration with node.js 

Ví dụ 3 Hãy tìm tất cả documents có field tags chứa "red";

db.inventory.find( { tags: "red" } )

Kết quả:

Ví dụ 2: Hãy tìm tất cả documents có field tags chứa một trong hai "red" and "blank";

db.inventory.find( { dim_cm: { $gt: 25 } } )

Kết quả:

Ví dụ 2: Hãy tìm tất cả documents có field tags chứa một trong hai "red" and "blank";

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

Kết quả:

Ví dụ 2: Hãy tìm tất cả documents có field tags chứa một trong hai "red" and "blank";

db.inventory.find( { "tags": { $size: 3 } } )

Kết quả:

Ví dụ 2: Hãy tìm tất cả documents có field tags chứa một trong hai "red" and "blank";

Kết quảMongoDB Manual

Tips:  Database integration with node.js 

  • Ví dụ 3 Hãy tìm tất cả documents có field tags chứa "red";
  • Ví dụ 4: Hãy tìm tất cả documents có field dim_cm lớn hơn 25
  • Ví dụ 5: Hãy tìm tất cả documents trong đó phần tử thứ hai trong field dim_cm lớn hơn 25:
  • Ví dụ 6: Sử dụng toán tử $size để truy vấn các mảng theo số phần tử. Ví dụ, sau đây chọn các tài liệu trong đó tags array lớn hơn 3 items

Resource: https://docs.mongodb.com/manual/tutorial/query-arrays/#query-an-array-by-array-lengthSelect your language drop-down menu in the upper-right to set the language of the following examples.


Docs Home → MongoDB Manual

On this page

Match an Array

Query an Array for an Element

Specify Multiple Conditions for Array Elements

Additional Query Tutorials

➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.

The following example queries for all documents where the field tags value is an array with exactly two elements,

const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
0 and
const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
1, in the specified order:

If, instead, you wish to find an array that contains both the elements

const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
0 and
const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
1, without regard to order or other elements in the array, use the
const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
4 operator:

The following example queries for all documents where tags is an array that contains the string const cursor = db.collection('inventory').find({ tags: ['red', 'blank'] });0 as one of its elements:

For example, the following operation queries for all documents where the array

const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
7 contains at least one element whose value is greater than
const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
8.

When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.

The following example queries for documents where the

const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
7 array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
0 condition and another element can satisfy the less than
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
1 condition, or a single element can satisfy both:

Use

db.inventory.find( { tags: { $all: ["red", "blank"] } } )
2 operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

  • The following example queries for documents where the

    const cursor = db.collection('inventory').find({
      tags: ['red', 'blank']
    });
    7 array contains at least one element that is both greater than (
    db.inventory.find( { tags: { $all: ["red", "blank"] } } )
    4)
    db.inventory.find( { tags: { $all: ["red", "blank"] } } )
    5 and less than (
    db.inventory.find( { tags: { $all: ["red", "blank"] } } )
    6)
    db.inventory.find( { tags: { $all: ["red", "blank"] } } )
    7:

  • Using dot notation, you can specify query conditions for an element at a particular index or position of the array. The array uses zero-based indexing.

  • Note