目录
MongoDB 中可以使用的类型如下表所示:
类型 数字 备注
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined 6 已废弃。
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 13
Symbol 14
JavaScript (with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255 Query with -1.
Max key 127
语法
关系运算查询(大于、小于、等于…)
在 MongoDB 里面支持的关系查询操作:
大于($gt)、小于($lt)、大于等于($gte)、小于等于($lte)、不等于($ne)
范围查询( i n , in, in,nin)
范围之中 $in, 不在范围之中 $nin
存在查询(存在 $exists、null 查询)
逻辑运算查询(或者 $or、等于 $and、非或者 $nor 以及 $not)
(注:执行逻辑NOT运算,选择出不能匹配表达式的文档 ,包括没有指定键的文档。$not 操作符不能独立使用,必须跟其他操作一起使用(除 $regex)
正则查询($regex)
# 语法
## 基础语法
{key: 正则标记}
## 完整语法
{key: {"$regex": 正则标记, "$options": 选项}}
对于 options 主要是设置正则的信息查询的标记:
"i": 忽略字母大小写
"m": 忽多行查找
"x": 空白字符串除了被转义的或在字符类中意外的完全被忽略
"s":匹配所有的字符(圆点,“.”),包括换行内容
取模查询($mod)
// 语法
db.col.find({ field: { $mod: [ divisor, remainder ]} })
// 通俗的理解为:
{ field: { $mod: [ 数字, 小数位 ]} }
基本查询
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
{ item: "vehicle", qty: null, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
{ item: "vehicle", size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
//选择集合中的所有文档
db.inventory.find( {} )
//从inventory集合中选择status等于"D"的所有文档
db.inventory.find( { status: "D" } )
//从inventory集合中选择status不等于"D"的所有文档
db.inventory.find( { $nor: [ { "status" : "D" } ] } )
//查询数量不大于 50 (即工资小于等于 50) 的文档数据
db.inventory.find( { "qty": { "$not": { "$gt": 50 } } } ) //等同于db.inventory.find( { "qty": { "$lte": 50 } } )
//从inventory集合中检索所有文档,其中status等于"A"或"D"
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
//从inventory集合中检索status等于"A" 和 qty小于($lt)30的所有文档
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
//检索集合中status等于"A" 或 qty小于($lt)30的所有文档
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 }
} ] } )
//该查询返回集合中的两个文档
db.inventory.find( { qty: null } )
//该查询仅返回qty字段的值为null的文档
db.inventory.find( { item : { $type: 10} } )
//该查询仅返回不包含qty字段的文档(由此可以看到:$exists 操作,仅仅只是判断 key 是否存在,而不去判断值是否为 null。)
db.inventory.find( { qty: { $exists: false } } )
//复合查询文档选择集合中所有的文档,其中status等于"A" 并且 *要么qty小于($lt)30 或 item以字符p开头
db.inventory.find( {
status: "A",
//$or: [ { qty: { $lt: 30 } }, { description: { $regex: /^p/, $options: 'i' } } ]
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
//查询集合中 qty 键值为 5 的 0 次模数的所有文档,例如 qty 值等于 25的文档
db.inventory.find( { qty: { $mod: [ 5, 0 ] } } )
查询嵌入/嵌套文档
(注:使用点符号查询时,该字段和嵌套字段必须在引号内。)
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
//查询选择字段size等于文档{ h: 14, w: 21, uom: "cm" }的所有文档
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
//选择嵌套在size字段中的字段uom等于"in"的所有文档
db.inventory.find( { "size.uom": "in" } )
//以下查询size字段中嵌入的字段h上使用小于运算符($lt)
db.inventory.find( { "size.h": { $lt: 15 } } )
//以下查询选择嵌套字段h小于15,嵌套字段uom等于"in"且status字段等于"D"的所有文档
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
查询数组
(注:使用点符号查询时,该字段和嵌套字段必须在引号内。)
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 ] }
]);
//下面的示例以指定 Sequences 查询字段tags的值是正好具有两个元素"red"和"blank"的数组的所有文档
db.inventory.find( { tags: ["red", "blank"] } )
//相反,如果您希望找到一个同时包含元素"red"和"blank"的数组,而不考虑该数组中的 Sequences 或其他元素,请使用$all运算符
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
//下面的示例查询所有文档,其中tags是包含字符串"red"作为其元素之一的数组
db.inventory.find( { tags: "red" } )
//以下操作查询数组dim_cm包含至少一个值大于25的元素的所有文档
db.inventory.find( { dim_cm: { $gt: 25 } } )
//一个元素可以满足大于15的条件,而另一个元素可以满足小于20的条件,或者单个元素可以满足两个条件
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
//使用$elemMatch运算符可以在数组的元素上指定多个条件,以使至少一个数组元素满足所有指定的条件。
//以下示例查询dim_cm数组包含至少一个既大于($gt)22又小于($lt)30的元素的文档
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
//以下示例查询数组dim_cm中第二个元素大于25的所有文档
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
//使用$size运算符可按元素数查询数组。例如,以下选择数组tags具有 3 个元素的文档
db.inventory.find( { "tags": { $size: 3 } } )
查询嵌入式文档数组
(注:使用点符号查询时,该字段和嵌套字段必须在引号内。)
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
//下面的示例选择instock数组中的元素与指定文档匹配的所有文档
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
//下面的示例选择所有instock数组具有至少一个嵌入式文档的嵌入式文档,这些文档包含字段qty的值小于或等于20
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
//数组具有至少一个嵌入式文档的嵌入式使用点符号查询时,字段和索引必须在引号内。
//下面的示例选择所有包含instock数组作为其第一个元素的文档的文档,该文档包含字段qty的值小于或等于20
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
//使用$elemMatch运算符可在一组嵌入式文档上指定多个条件,以使至少一个嵌入式文档满足所有指定条件。
//下面的示例查询instock数组具有至少一个嵌入式文档的文档,这些文档同时包含等于5的字段qty和等于A的字段warehouse
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
//以下示例查询其中instock数组具有至少一个嵌入式文档的嵌入式文档,其中该嵌入式文档包含大于10且小于或等于20的字段qty
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
//如果数组字段上的复合查询条件未使用$elemMatch运算符,则查询将选择其数组包含满足条件的元素的任意组合的那些文档。
//例如,以下查询匹配文档,其中嵌套在instock数组中的任何文档的qty字段都大于10,并且数组中的任何文档(但不一定是同一嵌入式文档)的qty字段都小于或等于20
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
//以下示例查询以下文档:instock数组具有至少一个包含等于5的字段qty的嵌入式文档和至少一个包含等于A的字段warehouse的嵌入式文档(但不一定是同一嵌入式文档)
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
从查询返回的项目字段
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
//仅返回指定字段和_id 字段
//通过在投影文档中将<field>设置为1,投影可以显式包括多个字段。以下操作返回与查询匹配的所有文档。在结果集中,只有item,status以及默认情况下_id字段返回到匹配的文档中
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
//禁止_id 字段
//您可以通过在投影中将其排除范围<field>设置为0来从结果中删除_id字段,如以下示例所示
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
//返回所有除外字段
//您可以使用投影排除特定字段,而不是列出要在匹配文档中返回的字段。下面的示例返回匹配文档中status和instock字段以外的所有字段
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
//返回嵌入式文档中的特定字段
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
//禁止嵌入文档中的特定字段
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
//在数组中的嵌入式文档上投影
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
//返回数组中的项目特定数组元素
//对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$
//下面的示例使用$slice投影运算符返回instock数组中的最后一个元素
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
//$elemMatch,$slice和$是投影特定元素以包括在返回数组中的“唯一”方法。例如,您不能使用数组索引来投影特定的数组元素。例如{ "instock.0": 1 }投影不会*用第一个元素投影数组。