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 ] }
不仅数组的值要完全一致,顺序也得保持一致
如果希望找到的是包含 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 数组包含在某种组合中满足查询条件的元素
多个条件是或的关系
上面的栗子虽然指定了复合条件,但只需要满足其中一个就匹配成功
使用 $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", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }