mongodb基础篇--文档的CRUD操作-查询

find查询

没有查询条件时,可以使用

db.collection.find()

或者

db.collection.find({})

如果需要指定相等的查询条件,比如:字段type = “技术”

db.getCollection('test').find({"type": "技术"})

等效于mysql中 select * from test where type = “技术”;

查询操作符

等值查询比较符

MongoDB 提供了一系列用于比较的比较符,它们分别是:

  • $eq 匹配等于指定值的值
  • $gt 匹配大于指定值的值
  • $gte 匹配大于或等于指定值的值
  • $in 匹配数组中指定的任何值
  • $lt 匹配小于指定值的值
  • $lte 匹配小于或等于指定值的值
  • $ne 匹配所有不等于指定值的值
  • $ nin 不匹配数组中指定的任何值
$eq

其中,$eq, $gte, $lt, $lte, g t , gt, gtne 的语法是相同的。以 $eq 为例,其语法格式如下:

{ <field>: { $eq: <value> } }

示例:

db.getCollection('test').find({"type": {$eq:"技术"}})
$in 和 $nin

$in 和 $nin 的语法相同,示例:

db.getCollection('test').find({"tag": {$in:["A"]}})

另外,$in 和 $nin 均支持正则表达式.

逻辑查询操作符

MongoDB 中的逻辑查询操作符共有 4 种,它们是:

  • $and 匹配符合多个条件的文档
  • $not 匹配不符合条件的文档
  • $nor 匹配不符合多个条件的文档
  • $or 匹配符合任一条件的文档

其中,$and, $nor 和 $or 语法格式相同。

$and是隐式的,不用在查询语句中表明。

$or, $ not,$nor 均采用显式写法。

$and

and是隐式的:

db.getCollection('test').find({"type": "技术", "num":20})

等效于mysql中select * from test where type = “技术” and num = 20;

$or

查询type="技术"或者num=30的文档:

db.getCollection('test').find({$or:[{"type": "技术"},{ "num":30}]})
$nor

查询num不等于20也不等于30的文档:

db.getCollection('test').find({$nor:[{"num": 20},{ "num":30}]})

$not

查询num存在且不大于20的文档:

db.getCollection('test').find({"num":{$exists:true,$not:{$gt:20}}})

元素查询操作符

MongoDB 中的元素查询操作符只有 2 种,它们是:

  • $exists 匹配具有指定字段的文档
  • $type 匹配字段值符合类型的文档
$exists

$exists判断字段是否存在,true为存在,false为不存在

db.getCollection('test').find({"num":{$exists:true}})
$type

$type查询类型匹配的文档

db.getCollection('test').find({"num":{$type:"number"}})

匹配数组,表示or的关系。比如num类型是number或者string的:

db.getCollection('test').find({"num":{$type:["number","string"]}})

评估查询操作符

MongoDB 中的评估查询操作符共有 6 种,它们是:

  • $expr 允许在查询语句中使用聚合表达式
  • $jsonSchema 根据给定的 JSON 模式验证文档
  • $mod 对字段的值执行模运算,并选择具有指定结果的文档
  • $regex 匹配与正则表达式规则相符的文档
  • $text 执行文本搜索
  • $where 匹配满足 JavaScript 表达式的文档
$regex

$regex可以在查询语句中使用正则表达式。
$regex 语法格式如下:

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
db.getCollection('test').find({"name":{$regex:/redis设计与实现/i}})

i表示不区分大小写。

$where

$ where可以将包含 JavaScript 表达式的字符串或函数传递给查询系统。显然,$where 提供了更高的灵活性,但它会将 JavaScript 表达式或函数应用在集合中的每个文档上,因此它对查询速度有一定影响。

可用属性:

args, MaxKey, MinKey

支持的函数:

  • assert()
  • BinData()
  • DBPointer()
  • DBRef()
  • doassert()
  • emit()
  • gc()
  • HexData()
  • hex_md5()
  • isNumber()
  • isObject()
  • ISODate()
  • isString()
  • Map()
  • MD5()
  • NumberInt()
  • NumberLong()
  • ObjectId()
  • print()
  • printjson()
  • printjsononeline()
  • sleep()
  • Timestamp()
  • tojson()
  • tojsononeline()
  • tojsonObject()
  • UUID()
  • version()
db.getCollection('test').find({$where : function(){
        return this.num == 20
    }})

数组查询操作符

MongoDB 中的数组查询操作符共有 3 个,它们是:

  • $all 匹配包含查询中指定条件的所有元素的数组。
  • $elemMatch 匹配数组字段中至少有 1 个元素与指定条件相符的文档。
  • $size 匹配数组元素数符合指定大小的文档。
$all

$ all是完全匹配,与$ and的操作相当。比如db.getCollection(‘test’).find({“tag”: {$ all:[“A”, “B”]}})等效于
db.getCollection(‘test’).find({$and :[{“tag”:“A”}, {“tag”:“B”}]})

$elemMatch

$elemMatch操作符将匹配数组中至少有 1 个元素满足查询条件的文档。

比如查询tag数组中大于B小于E的文档:

db.getCollection('test').find({"tag":{$elemMatch:{$gt:"B", $lt:"E"}}})

$ all可以和$ elemMatch一起使用:

db.getCollection('test').find({"tag": {
$all:[
    {$elemMatch:{num:20,type:"技术"}}, 
    {$elemMatch:{num:30}}
]
}})
$size

$size 用于匹配数组元素数符合指定大小的文档

db.getCollection('test').find({"tag":{$size:3}})

返回指定字段

查询返回值name,type:

db.getCollection('test').find({},{"name":1,"type":1})

返回值:

{
    "_id" : ObjectId("5de22784c5b99911d484e4fe"),
    "name" : "redis设计与实现(第一版)",
    "type" : "技术"
}

如果取反,将1改为0。
_id是默认返回的,如果不想要_id。

db.getCollection('test').find({},{"name":1,"type":1,"_id":0})

MongoDB 提供了 4 种投影操作符,它们是:

  • $ 投影数组中与指定条件匹配的第一个元素。
  • $elemMatch 投影数组中与 $elemMatch 指定条件匹配的第一个元素。
  • $ meta 投影在$text操作期间分配的文档分数。
  • $slice 限制从数组投射的元素数量。支持跳过和限制切片。

$ 的作用是投影数组中符合指定条件的第一个元素
查询tag存在的tag第一个元素:

db.getCollection('test').find({"tag":{$exists:true}},{"tag.$":1})

返回值:

{
    "_id" : ObjectId("5de33ef84fce381c011c72d3"),
    "tag" : [ 
        "C"
    ]
}

$elemMatch 的作用是投影数组中与 $elemMatch 指定条件匹配的第一个元素

比如:我们文档中有数据如下:

{
    "_id" : ObjectId("5de370efe492c4e276fd5d1b"),
    "name" : "Tomcat架构解析",
    "type" : "技术",
    "desc" : "Tomcat相关介绍",
    "price" : "40",
    "num" : 20.0,
    "tag" : [ 
        "A", 
        "B", 
        "C"
    ],
    "author" : [ 
        {
            "name" : "zhangsan",
            "country" : "China"
        }, 
        {
            "name" : "zhangsan",
            "country" : "USA"
        }
    ]
}

查询author.name = "zhangsan"的数据:

db.getCollection('test').find({"author":{$exists:true}},{"author":{$elemMatch:{"name":"zhangsan"}}})

返回值:

{
    "_id" : ObjectId("5de370efe492c4e276fd5d1b"),
    "author" : [ 
        {
            "name" : "zhangsan",
            "country" : "China"
        }
    ]
}

$slice 作用于数组,它的作用是限制从数组投影的元素数量

$slice 接受多种格式的参数,包括整负值和数组。参数为正代表数组从头到尾的顺序,参数为负代表从尾到头。

db.getCollection('test').find({"author":{$exists:true}},{"author":{$slice:1}})
db.getCollection('test').find({"author":{$exists:true}},{"author":{$slice:-1}})

从第2个开始返回两个:

db.getCollection('test').find({"author":{$exists:true}},{"author":{$slice:[2,2]}})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值