插入测试数据
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 ] } ]);
后面的栗子都会用到这里的测试数据
精确匹配数组
> db.inventory.find( { tags: ["red", "blank"] } )
{ "_id" : ObjectId("60b5fb209ba88b2120d5de24"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
不仅数组的值要完全一致,顺序也得保持一致
$all 操作符
如果希望找到的是包含 red、blank 两个元素的数组,可以使用 $all 操作符
> db.inventory.find({tags:{$all:["red","blank"]}}) { "_id" : ObjectId("60b5fb209ba88b2120d5de23"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de24"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de25"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de26"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
后面再展开细讲这个操作符
查询数组字段包含一个指定值元素的所有文档
> db.inventory.find( { tags: "red" } ) { "_id" : ObjectId("60b5fb209ba88b2120d5de23"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de24"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de25"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de26"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
找到 tags 字段包含 red 元素的所有文档
对数组字段中的元素指定单个条件
语法格式
{ <array field>: { <operator1>: <value1>, ... } }
{ 数组字段名 : { 操作符:值, 操作符2: 值2, ..... }}
实际栗子
查询数组 dim_cm 中至少包含一个值大于 25 的元素的所有文档
> db.inventory.find( { dim_cm: { $gt: 25 } } )
{ "_id" : ObjectId("60b5fb209ba88b2120d5de26"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
对数组元素指定多个条件
在数组元素上指定复合条件时,可以指定查询使得单个数组元素满足这些条件或数组元素的任意组合满足条件
在数组元素上使用复合条件
> db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ) { "_id" : ObjectId("60b5fb209ba88b2120d5de23"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de24"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de25"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] } { "_id" : ObjectId("60b5fb209ba88b2120d5de27"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }
dim_cm 数组包含在某种组合中满足查询条件的元素
- 满足大于 15 的条件
- 满足小于20的条件
- 同时满足这两个条件
多个条件是或的关系
查询满足多个条件的数组元素
上面的栗子虽然指定了复合条件,但只需要满足其中一个就匹配成功
如果想必须同时满足多个条件呢?
使用 $elemMatch 运算符在数组元素上指定多个条件,使得至少一个数组元素满足所有指定条件
小栗子
查询 dim_cm 数组包含至少一个大于 ($gt) 22 且小于 ($lt) 30 的元素的文档
> db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
{ "_id" : ObjectId("60b5fb209ba88b2120d5de26"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
按数组索引位置查询元素
查询 dim_cm 数组第二个元素大于 25 的文档(索引位置从 0 开始哦)
> db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
{ "_id" : ObjectId("60b5fb209ba88b2120d5de26"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
按数组长度查询数组
查询包含长度= 3 的 tags 数组的文档
> db.inventory.find( { "tags": { $size: 3 } } )
{ "_id" : ObjectId("60b5fb209ba88b2120d5de25"), "item" : "paper", "qty" : 100, "tags" : [ "red