Hướng dẫn query array mongodb
Nội dung bài viết 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 Giả sử bạn đã
Install Mongodb Tips: Find mongoose - sử dụng async await thay thế cho callback Ví dụ 1: Hãy tìm tất cả
documents có field tags chứa chính xác "red" and "blank"; 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: { $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ụ 4: Hãy tìm tất cả documents có field dim_cm lớn hơn 25 db.inventory.find( { dim_cm: { $gt: 25 } } ) Kết quả: 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: db.inventory.find( { "dim_cm.1": { $gt: 25 } } ) Kết quả: Ví dụ 6: Sử dụng toán tử db.inventory.find( { "tags": { $size: 3 } } ) Kết quả: Resource: https://docs.mongodb.com/manual/tutorial/query-arrays/#query-an-array-by-array-length Docs Home → MongoDB Manual On this page
➤ 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 If, instead, you wish to find an array that contains both the elements The following example queries for all documents where For example, the following operation
queries for all documents where the array 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 Use The
following example queries for documents where the 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. NoteWhen querying using dot notation, the field and nested field must be inside quotation marks. The following example queries for all documents where the second element in the
array Use the For additional query examples, see:
|