条件操作符用于比较两个表达式并从 mongoDB 集合中获取数据。
这些操作符可以分为以下几类:比较操作符、逻辑操作符、元素操作符、数组操作符、以及其他常用操作符。
比较操作符有:
操作符 | 描述 | 示例 |
---|---|---|
$eq | 等于 | { age: { $eq: 18 } } |
$ne | 不等于 | { age: { $ne: 18 } } |
$gt | 大于 | { age: { $gt: 18 } } |
$gte | 大于等于 | { age: { $gte: 18 } } |
$lt | 小于 | { age: { $lt: 18 } } |
$lte | 小于等于 | { age: { $lte: 18 } } |
$in | 在指定的数组中 | { age: { $in: [18, 30, 35] } } |
$nin | 不在指定的数组中 | { age: { $nin: [18, 30, 35] } } |
查找年龄大于等于 18 且专业为 “人工智能” 的文档:
db.skd.find({ age: { $gte: 18 }, major: "人工智能" });
{
"_id" : "6690faaf49bbaa53d170d4f5",
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300001"
}
{
"_id" : 1,
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300006"
}
逻辑操作符有:
操作符 | 描述 | 示例 |
---|---|---|
$and | 逻辑与,符合所有条件 | { $and: [ { age: { $gt: 18 } }, { major: “人工智能” } ] } |
$or | 逻辑或,符合任意条件 | { $or: [ { age: { $lt: 18 } }, { majro: “人工智能” } ] } |
$not | 取反,不符合条件 | { age: { $not: { $gt: 18 } } } |
$nor | 逻辑与非,均不符合条件 | { $nor: [ { age: { $gt: 18 } }, { major: “人工智能” } ] } |
查找年龄大于等于18 或专业为 “人工智能” 的文档:
//db.skd.find({ $or: [ { age: { $gt: 18 } }, { major: "人工智能" } ] });
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wqz",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "物理系",
"grade" : "少年班",
"sno" : "2386300009"
}
{
"_id" : "6690faaf49bbaa53d170d4f5",
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300001"
}
...
元素操作符有:
操作符 | 描述 | 示例 |
---|---|---|
$exists | 字段是否存在 | { age: { $exists: true } } |
$type | 字段的 BSON 类型 | { age: { $type: “int” } } |
查找包含 age 字段的文档:
db.skd.find({ age: { $exists: true } });
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用",
"sno" : "2386300002"
}
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wqz",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "物理系",
"grade" : "少年班",
"sno" : "2386300009"
}
...
数组操作符有:
操作符 | 描述 | 示例 |
---|---|---|
$all | 数组包含所有指定的元素 | { hobby: { $all: [“running”, “football”] } } |
$elemMatch | 数组中的元素匹配指定条件 | { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } } |
$size | 数组的长度等于指定值 | { hobby: { $size: 3 } } |
查找数组 hobby中包含 “running” 和 “football” 的文档:
db.skd.find({ hobby: { $all: ["running", "football"] } });
{
"_id" : "669a25042a0d21e03b193eb4",
"name" : "wzr",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史",
"sno" : "2386300005"
}
{
"_id" : "669a25692a0d21e03b193eb7",
"name" : "sjk",
"age" : 20,
"gender" : "男",
"hobby" : [
"football",
"riding",
"running"
],
"major" : "计算机应用",
"sno" : "2386300007"
}
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wqz",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "物理系",
"grade" : "少年班",
"sno" : "2386300009"
}
...
还有一些其他操作符如下:
操作符 | 描述 | 示例 |
---|---|---|
$regex | 匹配正则表达式 | { name: { $regex: /^y/ } } |
$text | 进行文本搜索 | { $text: { $search: “coffee” } } |
$where | 使用 JavaScript 表达式进行条件过滤 | { $where: “this.age > 18” } |
查找名字以 “y” 开头的文档:
db.skd.find({ name: { $regex: /^y/ } });
{
"_id" : "6690faaf49bbaa53d170d4f5",
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300001"
}
{
"_id" : 1,
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300006"
}
...
查找年龄大于 18 且专业为 “人工智能” ,名字以 “y” 开头的文档:
db.skd.find({
$and:[
{age:{$gte:18}},
{"name":{$regex:/^y/}},
{"major":"人工智能"}
]
})
{
"_id" : "6690faaf49bbaa53d170d4f5",
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300001"
}
{
"_id" : 1,
"name" : "ych",
"age" : 18,
"gender" : "女",
"hobby" : [
"music",
"drawing",
"volleyball"
],
"major" : "人工智能",
"sno" : "2386300006"
}