插入测试数据
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 操作符
>