10.Query an Array-官方文档摘录
2017-07-28 09:28
246 查看
1.插入
2.匹配数组
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170727092458625-1565259313.png)
使用$all可以找到自己制定包含在数据中的元素
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170727092853140-1342028290.png)
可以根据数组索引位置查找元素
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170728092439446-231187319.png)
通过数组长度查询
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170728092716352-1524858538.png)
This page provides examples of query operations on array fields using the
Copy
You can run the operation in the web shell below:
The following example queries for all documents where the field
Copy
If, instead, you wish to find an array that contains both the elements
Copy
The following example queries for all documents where
Copy
To specify conditions on the elements in the array field, use query operators in the query filter document:
For example, the following operation queries for all documents where the array
Copy
Copy
The following example queries for documents where the
Copy
The following example queries for all documents where the second element in the array
Copy
Copy
Query Documents
Query on Embedded/Nested Documents
Query an Array of Embedded Documents
db.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 ] } ]);
2.匹配数组
db.inventory.find({tags:["red","blank"]})
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170727092458625-1565259313.png)
使用$all可以找到自己制定包含在数据中的元素
db.inventory.find({tags:{$all:["red","blank"]}})
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170727092853140-1342028290.png)
test@gzxkvm52> db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ) { "_id" : ObjectId("59793fec7d548f819c1d7a1e"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("59793fec7d548f819c1d7a1f"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("59793fec7d548f819c1d7a20"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("59793fec7d548f819c1d7a22"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] } test@gzxkvm52> db.inventory.find( { dim_cm: { $elemMatch: { $gt: 15, $lt: 20 } } } ) { "_id" : ObjectId("59793fec7d548f819c1d7a22"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] } 如果使用{“x”:{"$gt":10, "$lt":20}进行查询,只会匹配“X”键的值大于等于10并且小于等于20的文档。但是假如某个文档的x字段是一个数组, 如果x的键的某一个元素与查询的任何一条语句相匹配,那么这个文档就会返回luoxi $elemMatch 和 $slice运算符是对数组进行projection的唯一途径
可以根据数组索引位置查找元素
db.inventory.find({"dim_cm.1":{$gt:25}})
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170728092439446-231187319.png)
通过数组长度查询
db.inventory.find({"tags":{$size:3}})
![](https://images2017.cnblogs.com/blog/754297/201707/754297-20170728092716352-1524858538.png)
This page provides examples of query operations on array fields using the
db.collection.find()method in the
mongoshell. The examples on this page use the
inventorycollection. To populate the
inventorycollection, run the following:
Copy
db.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 ] } ]);
You can run the operation in the web shell below:
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.
The following example queries for all documents where the field
tagsvalue is an array with exactly two elements,
"red"and
"blank", in the specified order:
Copy
db.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
$alloperator:
Copy
db.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.
The following example queries for all documents where
tagsis an array that contains the string
"red"as one of its elements:
Copy
db.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_cmcontains at least one element whose value is greater than
25.
Copy
db.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 thedim_cmarray contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than
15condition and another element can satisfy the less than
20condition, or a single element can satisfy both:
Copy
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
Query for an Array Element that Meets Multiple Criteria
Use$elemMatchoperator 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
dim_cmarray contains at least one element that is both greater than (
$gt)
22and less than (
$lt)
30:
Copy
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
Query for an Element by the Array Index Position
Using the 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.The following example queries for all documents where the second element in the array
dim_cmis greater than
25:
Copy
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
Query an Array by Array Length
Use the$sizeoperator to query for arrays by number of elements. For example, the following selects documents where the array
tagshas 3 elements.
Copy
db.inventory.find( { "tags": { $size: 3 } } )
Additional Query Tutorials
For additional query examples, see:Query Documents
Query on Embedded/Nested Documents
Query an Array of Embedded Documents
相关文章推荐
- Influxdb-官方文档摘录
- 16.Update Methods-官方文档摘录
- Influxdb-官方文档摘录
- Cocos Creator 监听和发射事件(官方文档摘录)
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- 8.Query Documents-官方文档摘录
- 22.Atomicity and Transactions-官方文档摘录
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- 2.Access the mongo Shell Help-官方文档摘录
- Cocos Creator系统内置事件和玩家输入事件(官方文档摘录)
- Cocos Creator 生命周期回调(官方文档摘录)
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- 20.Bulk Write Operations-官方文档摘录
- Cocos Creator中使用动作系统(官方文档摘录)
- Cocos Creator中使用动作列表(官方文档摘录)