Mongodb查询操作

本文介绍了MongoDB的基本数据类型,包括Double、String、Object等,并详细讲解了比较和逻辑运算符,如$eq、$gt、$and、$or等。此外,还深入探讨了MongoDB的联表查询功能,特别是$lookup操作符的用法,以及如何通过$unwind展开数组字段。示例展示了如何将学生表中的科目数据拆分成多个文档。
摘要由CSDN通过智能技术生成

前言

最近由于工作原因要使用Mongodb,所以就此整理一下,以备以后查看。

Mongodb基本数据类型

类型数字备注
Double1
String2
Object3
Array4
Binary data5
Undefined6已废弃。
Object id7
Boolean8
Date9
Null10
Regular Expression11
JavaScript13
Symbol14
JavaScript (with scope)15
32-bit integer16
Timestamp17
64-bit integer18
Min key255Query with -1.
Max key127

下面是一些mongodb常用到的逻辑运算符

比较

名字描述
$eqMatches values that are equal to a specified value.(判断相等)
$gtMatches values that are greater than a specified value.(判断大于)
$gteMatches values that are greater than or equal to a specified value.(判断大于等于)
$inMatches any of the values specified in an array.(判断在其中)
$ltMatches values that are less than a specified value.(判断小于)
$lteMatches values that are less than or equal to a specified value.(判断小于等于)
$neMatches all values that are not equal to a specified value.(判断所有值都不等于指定值)
$ninMatches none of the values specified in an array.(判断不在其中)

逻辑

名字描述
$andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.(与)
$notInverts the effect of a query expression and returns documents that do not match the query expression.(非)
$norJoins query clauses with a logical NOR returns all documents that fail to match both clauses.(异或)
$orJoins query clauses with a logical OR returns all documents that match the conditions of either clause.(或)

元素

名字描述
$existsMatches documents that have the specified field.
$typeSelects documents if a field is of the specified type.

基本查询功能在这里不在阐述,网上有很多,可以自行查找

Mongodb联表查询

命令功能描述
$project指定输出文档里的字段.
$match选择要处理的文档,与fine()类似。
$limit限制传递给下一步的文档数量。
$skip跳过一定数量的文档。
$unwind扩展数组,为每个数组入口生成一个输出文档。
$group根据key来分组文档。
$sort排序文档。
$geoNear选择某个地理位置附近的的文档。
$out把管道的结果写入某个集合。
$redact控制特定数据的访问。
$lookup多表关联
$lookup的功能及语法
db.collection.aggregate([
{
   $lookup:
     {
       from: <collection to join>, 
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}
语法值解释说明
from同一个数据库下等待被Join的集合。就是查询的表想要联接哪个集合
localField当前查询集合中的match值,如果输入的集合中,某文档没有 localField这个Key(Field),在处理的过程中,会默认为此文档含有 localField:null的键值对。
foreignField待Join的集合的match值,如果待Join的集合中,文档没有foreignField 值,在处理的过程中,会默认为此文档含有 foreignField:null的键值对。
as给新查询到的文档取名字。如果输入的集合中已存在该值,则会覆盖掉
$unwind操作符

扩展数组,为每个数组入口生成一个输出文档。

举个栗子

一个学生表,其中一个字段是数组类型,存的是科目subject,那么unwind会起到什么作用呢?

student:
{
    student_id:123 ,
    subject:[
        { type:a ,subject:"语文"},
        { type:b ,subject:"数学"},
        { type:c ,subject:"英语"}
    ]
}

//unwind操作:

db.student.aggregate([
    {$unwind:bonus}
])

//结果:
{student_id: 123 , subject:{type : a ,subject: "语文"}}
{student_id: 123 , subject:{type : b ,subject: "数学"}}
{student_id: 123 , subject:{type : c,subject: "英语"}}

这样应该一目了然了
OK,今天就到这里,以后遇到新的内容会更新文档,如若存在相似之处,请私聊!!!
不喜勿喷,指点欢迎!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值