• Mongodb简单操作


    1、库

    #创建/切换数据库(如果数据库不存在,则创建数据库,否则切换到指定数据库)
    > use mongod
    
    #查看(只显示有数据的db,新创建db需插入数据才能显示)
    > show dbs
    > db.mongodb.insert({"name":"测试"})
    WriteResult({ "nInserted" : 1 })
    
    #删除(先use到数据库里)
    > use mongodb
    switched to db mongodb
    > db.dropDatabase()
    { "ok" : 1 }
    > 

    2、集合

    语法:
    db.createCollection(name, options)
    
    name:   要创建的集合名称
    options: 可选参数, 指定有关内存大小及索引的选项
    字段类型描述
    capped 布尔 (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。
    当该值为 true 时,必须指定 size 参数
    size 数值 (可选)为固定集合指定一个最大值,即字节数。
    如果 capped 为 true,也需要指定该字段。
    max 数值 (可选)指定固定集合中包含文档的最大数量。
        在插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段。

     2.1 查看/创建

    > use test_1
    switched to db test_1
    
    #查看当前数据库下的集合
    > show collections
    > show tables
    
    #创建集合
    > db.createCollection("big")
    { "ok" : 1 }
    
    
    #创建固定集合,整个集合空间大小 65535B, 文档最大个数为 10000个
    > db.createCollection("mo",
    ... {capped : true,
    ... size : 65535,
    ... max : 10000})
    {
        "ok" : 1
    }
    #查询集合的所有记录(默认每页显示20条)
    > db.big.find()
    
    #查询所有记录,并格式化输出
    > db.big.find().pretty()
    
    #查看第一条记录
    > db.big.findOne()
    
    #查询集合总的记录数
    > db.big.count()
    
    #查看集合的存储信息
    > db.集合名.stats()

    #集合中索引+数据压缩存储后的大小 > db.big.totalSize() #集合中数据压缩存储的大小 > db.user.storageSize()

     2.2 删除

    语法:
    db.collection.drop()
    
    示例:
    > use test_1
    switched to db test_1
    > show collections
    big
    mo
    > db.mo.drop()
    true
    > show collections
    big
    > 

    3、文档

      mongo所有存储在集合中的数据都是 BSON 格式。

      BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。

     3.1 插入

      先use到db中;若插入时集合不在当前数据库中,会自动创建集合并插入文档。

      字符串带双引号和单引号没有区别,数值带引号就成了字符串;键最好是加上引号,键的本质是一个字符串,但不加大多数情况下编译器能够识别

    语法:
    #插入数据的时候会自动生成主键(_id)若插入的数据主键已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常,提示主键重复,不保存当前数据。
    db.COLLECTION_NAME.insert(document)
    
    #向集合插入一个新文档
    db.collection.insertOne(
       <document>,
       {
          writeConcern: <document>
       }
    )
    
    #向集合插入一个多个文档
    db.collection.insertMany(
       [ <document 1> , <document 2>, ... ],
       {
          writeConcern: <document>,
          ordered: <boolean>
       }
    )
    
    #参数说明:
    document:    要写入的文档。
    writeConcern:  写入策略,默认为 1,即要求确认写操作,0 是不要求。
    ordered:     指定是否按顺序写入,默认 true,按顺序写入。

     3.2 查看

    > db.big.insert({name:"wang",
    ... age:"18",
    ... sex:"M"})
    WriteResult({ "nInserted" : 1 })
    > db.big.insert({name:"wang", age:"22", sex:"M"})
    WriteResult({ "nInserted" : 1 })
    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "wang", "age" : "18", "sex" : "M" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "M" }
    > 

     3.3 更新

      3.3.1 update() 更新已存在的文档
    语法:
    db.collection.update(
       <query>,
       <update>,
       {
         upsert: <boolean>,
         multi: <boolean>,
         writeConcern: <document>
       }
    )
    
    参数说明:
    query :       update的查询条件,类似sql update查询内where后面的。
    update :      update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
    upsert :      可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
    multi :       可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
    writeConcern : 可选,抛出异常的级别。

      修改第一条发现的文档

    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "wang", "age" : "18", "sex" : "M" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "M" }
    > db.big.update(
    ... {"name" : "wang"},
    ... {$set:{"name" : "jun"}}
    ... )
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "jun", "age" : "18", "sex" : "M" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "M" }
    > 

      修改多条匹配到的文档,需要设置 multi 参数为 true

    > db.big.update( {"sex" : "M"}, {$set:{"sex" : "W"}}, {multi:true} )
    WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "jun", "age" : "18", "sex" : "W" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "W" }
    > 

      添加数据

    > db.big.update( {"name" : "wang"}, {$set:{"test" : "wang"}} )
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "jun", "age" : "18", "sex" : "W" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "W", "test" : "wang" }
    > 
      3.3.2 save() 通过传入的文档来替换已有文档,_id 主键存在就更新,不存在就插入
    语法:
    db.collection.save(
       <document>,
       {
         writeConcern: <document>
       }
    )
    
    参数说明:
    document :   文档数据。
    writeConcern :可选,抛出异常的级别。
    > db.big.find().pretty()
    {
        "_id" : ObjectId("61f363ee7a04356bb71ef433"),
        "name" : "jun",
        "age" : "18",
        "sex" : "W"
    }
    > db.big.save({
    ... "_id" : ObjectId("61f363ee7a04356bb71ef433"),
    ... "name" : "jun",
    ... "age" : "25",
    ... "sex" : "x"})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.big.find().pretty()
    {
        "_id" : ObjectId("61f363ee7a04356bb71ef433"),
        "name" : "jun",
        "age" : "25",
        "sex" : "x"
    }

     3.4 删除

      remove()方法用于从集合中删除文档,但是面对一个数据量十分庞大的集合,使用remove()方法难以直接删除该集合,所以使用会使用delete()方法重新建立索引删除,这样的效率会高很多

      delete()方法中有两个函数,一个是deleteOne(),另一个是deleteMany() 

      3.4.1 remove() 
    语法:
    db.collection.remove(
       <query>,
       <justOne>
    )
    
    2.6版本+
    db.collection.remove(
       <query>,
       {
         justOne: <boolean>,
         writeConcern: <document>
       }
    )
    
    参数说明:
    query :  (可选)删除的文档的条件。
    justOne : (可选)如果设为 true1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
    writeConcern :(可选)抛出异常的级别。
    #删除所有name=a的记录
    > db.mo.find()
    { "_id" : ObjectId("61f3a0d57a04356bb71ef436"), "name" : "a" }
    { "_id" : ObjectId("61f3a1b37a04356bb71ef437"), "name" : "a", "age" : "b", "sex" : "c" }
    { "_id" : ObjectId("61f3a1b67a04356bb71ef438"), "name" : "a", "age" : "b", "sex" : "c" }
    > db.mo.find().pretty()
    { "_id" : ObjectId("61f3a0d57a04356bb71ef436"), "name" : "a" }
    {
        "_id" : ObjectId("61f3a1b37a04356bb71ef437"),
        "name" : "a",
        "age" : "b",
        "sex" : "c"
    }
    {
        "_id" : ObjectId("61f3a1b67a04356bb71ef438"),
        "name" : "a",
        "age" : "b",
        "sex" : "c"
    }
    > db.mo.remove({"name":"a"})
    WriteResult({ "nRemoved" : 3 })
    > 
    
    #删除匹配到的第一条记录
    > db.mo.remove({"name":"a"},1)
    WriteResult({ "nRemoved" : 1 })
    >

    #删除所有数据
    > db.mo.remove({})
      3.4.2 deleteOne() 

      从集合中删除单个文档

    语法:
    db.collection.deleteOne(
       <filter>,
       {
          writeConcern: <document>,
          collation: <document>
       }
    )
    
    参数说明:
    filter:    使用查询运算符指定删除条件。要删除集合中的所有文档,请传入一个空文档({ })
    writeConcern: 可选, MongoDB写入安全机制,如果在事务中运行,请不要为操作明确设置writeConcern
    collation:  可选, 允许用户为字符串比较指定特定于语言的规则,例如字母大写和重音符号的规则
    hint:      可选,一个文档或 用于指定支持查询谓词索引的字符串,可以是索引文档或索引名称字符串
    #删除name=a的第一个文档
    > db.mo.deleteOne({"name":"a"})
      3.4.3 deleteMany() 

       从集合中删除所有匹配到的文档,用法参照deleteone() 

    语法:
    db.collection.deleteMany(
       <filter>,
       {
          writeConcern: <document>,
          collation: <document>
       }
    )

     3.5 查询

    语法:
    #非结构化方式查询集合下所有文档
    >db.COLLECTION_NAME.find()
    
    #只返回第一个文档
    > db.COLLECTION_NAME.findOne()
    
    #格式化方式
    >db.COLLECTION_NAME.find().pretty()
    > db.big.find()
    { "_id" : ObjectId("61f363ee7a04356bb71ef433"), "name" : "jun", "age" : "25", "sex" : "x" }
    { "_id" : ObjectId("61f364f67a04356bb71ef434"), "name" : "wang", "age" : "22", "sex" : "W", "test" : "wang" }
    { "_id" : ObjectId("61f398f07a04356bb71ef435"), "name" : "jun", "age" : "18", "sex" : "M" }
    > db.big.find().pretty()
    {
        "_id" : ObjectId("61f363ee7a04356bb71ef433"),
        "name" : "jun",
        "age" : "25",
        "sex" : "x"
    }
    {
        "_id" : ObjectId("61f364f67a04356bb71ef434"),
        "name" : "wang",
        "age" : "22",
        "sex" : "W",
        "test" : "wang"
    }
    {
        "_id" : ObjectId("61f398f07a04356bb71ef435"),
        "name" : "jun",
        "age" : "18",
        "sex" : "M"
    }
    > db.big.findOne()
    {
        "_id" : ObjectId("61f363ee7a04356bb71ef433"),
        "name" : "jun",
        "age" : "25",
        "sex" : "x"
    }
  • 相关阅读:
    mybatis :xml文件中传入参数和if标签结合使用时要点
    mysql:查询数据库版本的几种方式
    http post 方法传递参数的2种方式
    深入理解mybatis参数
    Mybatis:动态sql
    Mybatis:传入参数方式以及#{}与${}的区别
    [GLSL]着色器周记02——火焰特效 【转】
    OpenGL ES入门09-GLSL实现常见特效 [转]
    RenderMonkey 练习 第五天 【OpenGL NormalMapping】
    反射向量 及 向量投影
  • 原文地址:https://www.cnblogs.com/Xinenhui/p/15852347.html
Copyright © 2020-2023  润新知