mongodb-条件操作符


条件操作符用于比较两个表达式并从 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"
}

文章作者: 云上的小雨滴
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 云上的小雨滴 !
评论
  目录