zoukankan      html  css  js  c++  java
  • MongoDB集合的操作

    MongoDB集合的操作

    插入文档

      MongoDB的数据定义格式和JSON的数据定义格式是一样的,但是在MongoDB中我们把他称之为BSON。他的数据格式是非常丰富的,比如我们在Mysql中要联系两站表的关系我们会创建一个表,里面存放他们的关系。但是在MongoDB里面我们可以放在同一个文档里面,我们定义一个数组类型的属性,这个数组就可以存放他们之间的关系,只需要两个而不是三个。

    1 db.course.insert({
    2     "name" : "数学",
    3     "type" : "major"
    4 })
    5 
    6 向课程表里插入一条数据

    一次性插入多条数据

     1 db.student.insert([
     2     {
     3         "stu_name" : "小明",
     4         "stu_number" : "2013001",
     5         "stu_sex" : "男",
     6         "course" : [
     7             ObjectId("5bb82bb3627eb5e25bead053"),
     8             ObjectId("5bb82c5f627eb5e25bead054"),
     9             ObjectId("5bb82c5f627eb5e25bead055"),
    10             ObjectId("5bb82c5f627eb5e25bead057"),
    11             ObjectId("5bb82c5f627eb5e25bead058")
    12         ],
    13     },
    14     {
    15         "stu_name" : "小红",
    16         "stu_number" : "2013002",
    17         "stu_sex" : "女",
    18         "course" : [
    19             ObjectId("5bb82bb3627eb5e25bead053"),
    20             ObjectId("5bb82c5f627eb5e25bead054"),
    21             ObjectId("5bb82c5f627eb5e25bead055"),
    22             ObjectId("5bb82c5f627eb5e25bead056"),
    23             ObjectId("5bb82c5f627eb5e25bead058")
    24         ],
    25     }
    26 ])

    就像JSON一样。一次封装多个数据我们就用数组包裹起来就可以了,我们直接在学生表里面关联课程的信息,course就是我们学生所选的课程。

    还有一种插入的方法:

     1 db.student.save(
     2     {
     3         "stu_name" : "小峰",
     4         "stu_number" : "2013009",
     5         "stu_sex" : "男",
     6         "course" : [
     7             ObjectId("5bb82bb3627eb5e25bead053"),
     8             ObjectId("5bb82c5f627eb5e25bead054"),
     9             ObjectId("5bb82c5f627eb5e25bead055"),
    10             ObjectId("5bb82c5f627eb5e25bead057"),
    11             ObjectId("5bb82c5f627eb5e25bead058")
    12         ],
    13     }
    14 )

    save也可以插入数据,如果save的数据在集合里面就直接替换更新,没有这个数据的存在就新增加一条。

    更新文档

    1 更新文档
    2 db.student.update({
    3    查询的条件,
    4    更新的语句,
    5    附加的参数     
    6 })

    附加的参数

    查询的条件有

    and条件,挨着写默认就是and

    1 db.student.update(
    2     {
    3         "stu_name" : "小张",
    4         "stu_number" : "2013003",
    5     },
    6     {
    7         $set : {"stu_name" : "小张二"}
    8     }
    9 )

    or条件,要出现$or的关键字

     1 db.student.update(
     2     {
     3          $or : [
     4             {"stu_name" : "小张二"},
     5             {"stu_number" : "2013003"}
     6         ],
     7     },
     8     {
     9         $set : {"stu_name" : "小张"}
    10     }
    11 )

    布尔运算符

    1 $ne : 不等于
    2 $not : 不匹配结果
    3 $nor : 所有的条件都不匹配
    4 $exists : 判断元素是否存在

    $inc

    1 db.student.update(
    2     {
    3         "stu_number" : "2013001"
    4     },
    5     {
    6         $inc : {"mark" : 1}//自增1,为负数的时候就是减
    7     }
    8 );

    大于一个数据或小于一个数

    1 db.student.update(
    2     {
    3         "stu_number" : {$gte:"2013001",$lte:"2013002"}
    4     },
    5     {
    6         $inc : {"mark" : 1}//自增1,为负数的时候就是减
    7     }
    8 );

    $exists

    1 db.student.find(
    2     {
    3         "stu_number" : {$exists:true}
    4     }
    5 );

    返回这个字段存在的所有记录,当为false的时候结果就是相反;

    删除文档

    1 db.student.remove({
    2    条件 
    3 },属性)

    1 db.student.remove(
    2 {
    3     "_id":ObjectId("5bb83384627eb5e25bead061")
    4 }
    5 )

    查询文档

    $not

    1 db.student.find(
    2     {
    3         'stu_number' : {$not : {$ne : "2013002"}}
    4     }
    5 ).pretty()
    6 这种情况下也会返回没有stu_number的字段,和$gt这些比较运算符是不同的,他们只会返回有的字段

    $in,不在集合里面我们就用$nin

    1 db.student.find(
    2     {
    3         'stu_number' : { $in : ["2013001","2013002"]}
    4     }
    5 ).pretty()
    6 和Mysql的where in是一样的效果,他会查出2013001和002的数据

     模糊查询

    在mongoDB里面我们的模糊查询是按照正则表达式来进行匹配的

    1 db.student.find(
    2 {
    3     "stu_number":{$regex:/^2013/}
    4 }
    5 )

    我们要查询一个数组里面一个字段的值,我们用进行连接

    1 {
    2     "userInfo" : {
    3         "name" : "张三",
    4         "age" : 28
    5     }
    6 }
    7 我们要查询name等于张三的信息
    8 
    9 db.student.find({"userInfo.name":"张三"})

    JavaScript查询

    1 db.student.find(
    2 {
    3     "$where" : "function () {return this.stu_number == '2013001'}"
    4 }
    5 )

    排序

    1 db.student.find().sort({"stu_number":-1}).pretty()
    2 
    3 当为-1的时候是降序,1为升序

    限制

    1 db.student.find().sort({"stu_number":-1}).limit(5).pretty()
    2 
    3 限制显示5条数据

    跳过

    1 db.student.find().sort({"stu_number":-1}).limit(5).skip(2).pretty()
    2 
    3 跳过前两个并且显示5条数据

    我们所看到的限制、跳过和Mysql的limit一样,限制就是第一个参数,跳过就是第二个参数偏移量。

    查询返回特定的字段

    1 db.student.find({},{'stu_number':0,}).sort({"stu_number":-1}).limit(5).skip(2).pretty()

    当我们stu_number为0的时候,返回除了stu_number以外的数据,如果为1只返回stu_number一个字段的数据。

  • 相关阅读:
    Maven报错,没有有效的生命周期
    6张图解释IO流
    传统Java JDBC
    快速杀死占用8080端口进程的批处理(kill-8080.bat)
    ubuntu环境配置终极解答
    Linux系统下Java开发环境的配置(未完...)
    Linux常用命令及操作(第二弹)
    Linux下安装Mysql
    Linux常用命令及操作
    int转LPCTSTR
  • 原文地址:https://www.cnblogs.com/meichao/p/9746913.html
Copyright © 2011-2022 走看看