Query an Array查询数组
On this page本页内容
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.
This page provides examples of query operations on array fields using the Collection.find() method in the MongoDB Node.js Driver.本页提供了使用MongoDB Node.js驱动程序中的Collection.find()
方法对数组字段进行查询操作的示例。
The examples on this page use the 此页面上的示例使用inventory
collection. Connect to a test database in your MongoDB instance then create the inventory
collection:inventory
集合。连接到MongoDB实例中的测试数据库,然后创建inventory
集合:
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]
}
]);
Match an Array匹配数组
To specify equality condition on an array, use the query document 要在数组上指定相等条件,请使用查询文档{ <field>: <value> }
where <value>
is the exact array to match, including the order of the elements.{ <field>: <value> }
,其中<value>
是要匹配的确切数组,包括元素的顺序。
The following example queries for all documents where the field 以下示例查询所有文档,其中字段tags
value is an array with exactly two elements, "red"
and "blank"
, in the specified order:tags
值是一个数组,正好有两个元素,"red"
和"blank"
,按指定顺序排列:
const cursor = db.collection('inventory').find({
tags: ['red', 'blank']
});
If, instead, you wish to find an array that contains both the elements 相反,如果您希望找到一个同时包含"red"
and "blank"
, without regard to order or other elements in the array, use the $all
operator:"red"
和"blank"
元素的数组,而不考虑数组中的顺序或其他元素,请使用$all
运算符:
const cursor = db.collection('inventory').find({
tags: { $all: ['red', 'blank'] }
});
Query an Array for an Element在数组中查询元素
To query if the array field contains at least one element with the specified value, use the filter 若要查询数组字段是否至少包含一个具有指定值的元素,请使用筛选器{ <field>: <value> }
where <value>
is the element value.{ <field>: <value> }
,其中<value>
是元素值。
The following example queries for all documents where 以下示例查询所有文档,其中tags
is an array that contains the string "red"
as one of its elements:tags
是包含字符串"red"
作为其元素之一的数组:
const cursor = db.collection('inventory').find({
tags: 'red'
});
To specify conditions on the elements in the array field, use query operators in the query filter document:要在数组字段中的元素上指定条件,请在查询筛选器文档中使用查询运算符:
{ <array field>: { <operator1>: <value1>, ... } }
For example, the following operation queries for all documents where the array 例如,以下操作查询数组dim_cm
contains at least one element whose value is greater than 25
.dim_cm
至少包含一个值大于25
的元素的所有文档。
const cursor = db.collection('inventory').find({
dim_cm: { $gt: 25 }
});
Specify Multiple Conditions for Array Elements为数组元素指定多个条件
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.在数组元素上指定复合条件时,可以指定查询,以便单个数组元素满足这些条件,或者数组元素的任意组合满足这些条件。
Query an Array with Compound Filter Conditions on the Array Elements对数组元素使用复合筛选条件查询数组
The following example queries for documents where the 以下示例查询文档,其中dim_cm
array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15
condition and another element can satisfy the less than 20
condition, or a single element can satisfy both:dim_cm
数组包含在某种组合中满足查询条件的元素;例如,一个元素可以满足大于15
的条件,而另一个元素则可以满足小于20
的条件,或者单个元素可以同时满足这两个条件:
const cursor = db.collection('inventory').find({
dim_cm: { $gt: 15, $lt: 20 }
});
Query for an Array Element that Meets Multiple Criteria查询满足多个条件的数组元素
Use 使用$elemMatch
operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.$elemMatch
运算符在数组的元素上指定多个条件,以便至少有一个数组元素满足所有指定的条件。
The following example queries for documents where the 以下示例查询dim_cm
array contains at least one element that is both greater than ($gt
) 22
and less than ($lt
) 30
:dim_cm
数组至少包含一个大于($gt
)22
且小于($lt
)30
的元素的文档:
const cursor = db.collection('inventory').find({
dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } }
});
Query for an Element by the Array Index Position按数组索引位置查询元素
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.数组使用基于零的索引。
When 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 以下示例查询数组dim_cm
is greater than 25
:dim_cm
中第二个元素大于25
的所有文档:
const cursor = db.collection('inventory').find({
'dim_cm.1': { $gt: 25 }
});
Query an Array by Array Length按数组长度查询数组
Use the 使用$size
operator to query for arrays by number of elements. For example, the following selects documents where the array tags
has 3 elements.$size
运算符可以按元素数查询数组。例如,下面选择数组tags
包含3个元素的文档。
const cursor = db.collection('inventory').find({
tags: { $size: 3 }
});
Additional Query Tutorials其他查询教程
For additional query examples, see:有关其他查询示例,请参阅: