MongoDB系列13:MongoDB查询操作符说明
邓开表同学实战MongoDB系列文章,非常不错,赞!大力推荐!
本文是第13篇,主要讲述MongoDB查询操作符说明实战操作,非常值得一看。
MongoDB系列文章:
MongoDB Compass--MongoDB DBA必备的管理工具
Change Streams构建实时同步数据流
在MongoDB中,对于集合的查询操作符大致可以分为以下几大类:
·比较查询操作符
·逻辑查询操作符
·元素查询操作符
·诊断查询操作符
·地理空间查询操作符
·数组查询操作符
·按位查询操作符
1、比较查询操作符
比较查询操作符内容如下:
操作符 |
描述 |
举例 |
$eq |
匹配等于指定的值 |
db.t_01.find( { “name”: { $eq:”david” } } ) |
$gt |
匹配大于指定的值 |
db.t_01.find( { “age” : { $gt: 30 } } ) |
$gt |
匹配大于或等于指定的值 |
db.t_01.find( { “age” : { $gte: 30 } } ) |
$in |
匹配数组中的任意一个值 |
db.t_01.find( { “age” : { $in : [ 30,40 ] } } ) |
$lt |
匹配小于指定的值 |
db.t_01.find( { “age” : { $lt: 30 } } ) |
$lte |
匹配小于等于指定的值 |
db.t_01.find( { “age” : { $lte: 30 } } ) |
$ne |
匹配不等于指定值的所有值 |
db.t_01.find( { “age” : { $ne: 30 } } ) |
$nin |
匹配不在数组中出现的值 |
db.t_01.find( { “age” : { $nin : [ 30,40 ] } } ) |
2、逻辑查询操作符
逻辑查询操作符内容如下:
操作符 |
描述 |
举例 |
$and |
逻辑和操作需要同时满足具有两个或多个表达式的数组中的条件。 |
db.t_01.find( { $and : [ {“age”:{ $gte : 28 } }, { “deparment” : { $eq : “sale_01”} } ] } ) |
$not |
逻辑否操作返回与查询表达式不匹配的文档 |
db.t_01.find( { “age” : { $not : { $gt : 30 } } } ) |
$nor |
逻辑非或操作,返回同时不能匹配数组中表达式的文档 |
db.t_01.find( { $nor: [ {“age”:30 } , { “name”:”david”} ] } ) |
$or |
逻辑或操作,返回符合任一条件的所有文档 |
db.t_01.find( { $or : [ { “deparment”:”sale_01” }, {“age”:{ $gt : 28 } } ] } ) |
3、元素查询操作符
元素查询操作符内容如下:
操作符 |
描述 |
举例 |
$exists |
匹配具有指定字段的文档 |
db.t_01.find( { “name”:{ $exists:true,$in: [“david”,”grut”] } } ) |
$type |
如果字段为指定类型,则返回文档 |
db.t_01.find( {“name” : {$type : “string” } } ) |
4、诊断查询操作符
诊断查询操作符内容如下:
操作符 |
描述 |
举例 |
$expr |
允许在查询语句中使用聚合表达式,$expr可以构建查询表达式,在匹配时,比较同一文档中的字段。 |
--两个字段比较,返回”sal”比”age”大的文档: db.t_01.find( {$expr: { $gt: [“age”,”sal”] } } ) |
$jsonSchema |
$jsonSchema可以被用于文档验证器,用于集合模式验证。 |
--定义一个users集合模式验证: db.createCollection(“users”, { validator: { $jsonSchema: { bsonType: “object”, required: [“name”,”sex”], properties: { name: { bsonType: “string”, description: “must be a string and is required” }, age: { bsonType: “int”, description: “must be a integer and is not required” }, sex: { enum: [“male” , “female”], description: “can only be one of the enum values and is required” } } } } ) --往集合users插入数据 db.users.insert({“name”:”gg”,”sex”:”male”}) |
$mod |
对字段的值执行除以指定值取余数运算。 |
--返回”age”字段值被3整除的文档 db.t_01.find( {“age” : {$mod : [3,0] } } ) |
$regex |
选择与指定正则表达式匹配的文档,MongoDB使用Perl兼容正则表达式版本8.41 |
--查询”name”结尾是tor三个字符的文档 db.t_01.find( {“name”: {$regex : /tor$/ } } ) |
$text |
$text是对具有文本索引的字段执行文本搜索。 |
--在t_01集合的”name”上创建text索引 db.t_01.createIndex( { “name” : “text”}) --使用全本搜索 db.t_01.find( {$text: {$search: “david” } } ) |
$where |
匹配满足JavaScript表达式的文档,使用$where操作符将包含JavaScript表达式的字符串或完整的JavaScript函数传递给查询系统。 |
--查询”name”字段为david的文档 db.t_01.find( { $where : function() { return (this.name == “david”) } } ) |
5、地理空间查询操作符
地理空间查询操作符内容如下:
操作符 |
描述 |
举例 |
$geoIntersects |
选择地理空间数据与指定的GeoJSON对象相交的文档,即数据和指定对象的交集为非空的文档。2dsphere索引支持$geoIntersects操作符;$geoIntersects使用$geometry操作符定义GeoJSON对象。 |
--查询与多边形相交的文档 db.places.find({ loc: { $geoIntersects: { $geometry: { type: “Polygon”, coordinates:[ [ [0,0],[3,6],[6,1],[0,0] ] ] } } } } ) |
$geoWithin |
选择具有完全存在于指定形状内的地理空间数据的文档,2dsphere和2d索引都支持$geoWithin。$geoWithin运用$geometry操作符指定GeoJSON对象。 |
--查询完全存在于GeoJSON多边形内的所有loc数据。 db.places.find({ loc: { $geoWithin: { $geometry: { type: “Polygon”, coordinates: [ [ [0,0],[3,6],[6,1],[0,0] ] ] } } } } ) |
$near |
返回接近点的地理空间对象,需要地理空间索引。2dsphere和2d索引支持$near。 |
--查询离指定的GeoJson点至少1000米的文档 db.places.find({ location:{ $near: { $geometry: { type:“Point”, coordinates:[-73.9667,40.78] }, $minDistance:1000, $maxDistance:5000 } } } ) |
$nearSphere |
返回接近球面点上的地理空间对象,2dsphere和2d索引支持$nearSphere |
--查询离指定点至少1000米,至多5000米的位置 db.places.find({ location:{ $nearSphere: { $geometry: { type:“Point”, coordinates:[-73.9667,40.78] }, $minDistance:1000, $maxDistance:5000 } } } ) |
6、数组查询操作符
数组查询操作符内容如下:
操作符 |
描述 |
举例 |
$all |
匹配包含查询中指定的所有元素的数组 |
--查询t_01集合的name字段同时包含”deng”,”groot”,”lily”的文档 db.t_01.find( { “name”:{ $all: [“deng”,”groot”,”lily”] } } ) |
$elemMatch |
返回数组字段中至少有一个元素与所有指定的元素匹配的文档 |
--查询students集合中的scores数组字段中,至少有一个大于或等于80且小于90的元素的文档 db.students.find({ scores: { $elemMatch: {$gte:80, $lt: 90} } } ) |
$size |
返回具有与指定大小一样的数组字段的文档 |
--查询students集合中scores数组字段中具有2个元素的文档。 db.students.find({ scores : { $size : 2 } } ) |
7、按位查询操作符
按位查询操作符内容如下:
操作符 |
描述 |
举例 |
$bitsAllClear |
匹配数字或二进制值,其中查询给出的所有位位置在字段中是明确的(即0)。 |
--查询字段age是否在位置1和位置5有位清除。 db.t_01.find({ “age”: { $bitsAllClear:[1,5] } } ) |
$bitsAllSet |
匹配数字或二进制值,其中查询给出的所有位位置在字段中是明确的(即1)。 |
--查询字段age是否具有在位置1和位置5设置的位1。 db.t_01.find({ “age”: { $bitsAllSet:[1,5] } } ) |
$bitsAnyClear |
匹配数字或二进制值,返回其中一组位位置中的任何位具有0的文档 |
--查询字段age在位置1或位置5具有位清除的文档。 db.t_01.find({ “age”: { $bitsAnyClear:[1,5] } } ) |
$bitsAnySet |
匹配数字或二进制值,返回其中一组位位置中的任何位具有1的文档 |
--查询字段age在位置1或位置5为1的文档。 db.t_01.find({ “age”: { $bitsAnySet:[1,5] } } ) |
猜你喜欢
加入技术讨论群
《大数据和云计算技术》社区群人数已经3000+,欢迎大家加下面助手微信,拉大家进群,*交流。
喜欢QQ群的,可以扫描下面二维码:
欢迎大家通过二维码打赏支持技术社区(英雄请留名,社区感谢您,打赏次数超过108+):