zoukankan      html  css  js  c++  java
  • mongoDB学习 概述与 CRUD基本操作

    中文社区https://mongoing.com/  

    极客时间有唐老师的课程  https://time.geekbang.org/course/intro/100040001

    mongo的其他组件

    • 可视化工具 mongodb compass 
    • Mongo BI connector(只有企业版,不知道以后开源不,可以解释SQL语法)

     ----------分割线-------------------------------------------------------------------------------------

    一.mongoDB的优势:为何要用mongoDB

    • 灵活: 快速响应业务变化

                example:无需提前初始化新建表,新增字段无需提前alter列,可以直接加入文档。

    • 快速: 最简单快速的开发方式          

        开发简洁,减少代码量,而且由于josn模型可以内嵌多层文档,大多数情况也无需多表关联的设计。

    • 原生的高可用和横向扩展能力          

        这是和mysql/orcale对比,hadoop生态组件大多数都原生自带高可用和横向扩展能力。体现在复制集、分片、mongos等

    CRUD操作

    详细的CRUD 请参考官方的文档  https://docs.mongodb.com/manual/crud/

    INSERT

    操作格式:
    db.<集合>.insertOne(<JSON对象>)
    db.<集合>.insertMany([<JSON 1>, <JSON 2>, …<JSON n>])
    示例:
    db.fruit.insertOne({name: "apple"})
    db.fruit.insertMany([
    {name: "apple"},
    {name: "pear"},
    {name: "orange"}
    ])

    find 

    关于 find:
     find 是 MongoDB 中查询数据的基本指令,相当于 SQL 中的 SELECT 。
     find 返回的是游标。
    find 示例:
    db.movies.find( { "year" : 1975 } ) //单条件查询
    db.movies.find( { "year" : 1989, "title" : "Batman" } ) //多条件and查询
    db.movies.find( { $and : [ {"title" : "Batman"}, { "category" : "action" }] } ) // and的另一种形式
    db.movies.find( { $or: [{"year" : 1989}, {"title" : "Batman"}] } ) //多条件or查询
    db.movies.find( { "title" : /^B/} ) //按正则表达式查找

    ● $lt: 存在并小于
    ● $lte: 存在并小于等于
    ● $gt: 存在并大于
    ● $gte: 存在并大于等于
    ● $ne: 不存在或存在但不等于
    ● $in: 存在并在指定数组中
    ● $nin: 不存在或不在指定数组中
    ● $or: 匹配两个或多个条件中的一个
    ● $and: 匹配全部条件
    使用 find 搜索子文档
    ● find 支持使用“field.sub_field”的形式查询子文档。假设有一个文档:
    db.fruit.insertOne({
    name: "apple",
    from: {
    country: "China",
    province: "Guangdon"
    }
    })
    ● 考虑以下查询的意义:
    db.fruit.find( { "from.country" : "China" } )  //正确
    db.fruit.find( { "from" : {country: "China"} } )  //无法找到
    使用 find 搜索数组和数组中的对象
    find 支持对数组中的元素进行搜索。假设有一个文档:
    db.fruit.insert([
    { "name" : "Apple", color: ["red", "green" ] },
    { "name" : "Mango", color: ["yellow", "green"] }
    ])
    考虑以下查询的意义:
    db.fruit.find({color: "red"})
    db.fruit.find({$or: [{color: "red"}, {color: "yellow"}]} )

    考虑以下文档,在其中搜索 db.movies.insertOne( {
    "title" : "Raiders of the Lost Ark", "filming_locations" : [ { "city" : "Los Angeles", "state" : "CA", "country" : "USA" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "Florence", "state" : "SC", "country" : "USA" } ] }) • // 查找城市是 Rome 的记录 • db.movies.find({"filming_locations.city": "Rome"})

    在数组中搜索子对象的多个字段时,如果使用 $elemMatch,它表示必须是同一个
    子对象满足多个条件。考虑以下两个查询:
    db.getCollection('movies').find({
      "filming_locations.city": "Rome",
      "filming_locations.country": "USA"
    })
    db.getCollection('movies').find({
      "filming_locations": {
      $elemMatch:{"city":"Rome", "country": "USA"}
      }
    })
    控制 find 返回的字段(projection 小技巧)
    ● find 可以指定只返回指定的字段;
    ● _id字段必须明确指明不返回,否则默认返回;
    ● 在 MongoDB 中我们称这为投影(projection);
    //id:0 不展示 title:1 展示
    ● db.movies.find({"category": "action"},{"_id":0, title:1})

     remove 删除文档

    ● remove 命令需要配合查询条件使用;
    ● 匹配查询条件的的文档会被删除;
    ● 指定一个空文档条件会删除所有文档;
    ● 以下示例:
    db.testcol.remove( { a : 1 } ) // 删除a 等于1的记录
    db.testcol.remove( { a : { $lt : 5 } } ) // 删除a 小于5的记录
    db.testcol.remove( { } ) // 删除所有记录,和drop相比索引和其他设置项不会被改变
    db.testcol.remove() //报错

    update 更新文档

    ● Update 操作执行格式:db.<集合>.update(<查询条件>, <更新字段>)
    ● 以以下数据为例:
    db.fruit.insertMany([
    {name: "apple"},
    {name: "pear"},
    {name: "orange"}
    ])
    db.fruit.updateOne({name: "apple"}, {$set: {from: "China"}})

    使用 updateOne 表示无论条件匹配多少条记录,始终只更新第一条;
    使用 updateMany 表示条件匹配多少条就更新多少条;
    updateOne/updateMany 方法要求更新条件部分必须具有以下之一,否则将报错:
      • $set/$unset
      • $push/$pushAll/$pop
      • $pull/$pullAll
      • $addToSet
    // 报错
    db.fruit.updateOne({name: "apple"}, {from: "China"})

    $push: 增加一个对象到数组底部
    $pushAll: 增加多个对象到数组底部
    $pop: 从数组底部删除一个对象
    $pull: 如果匹配指定的值,从数组中删除相应的对象
    $pullAll: 如果匹配任意的值,从数据中删除相应的对象
    $addToSet: 如果不存在则增加一个值到数组

    drop 删除集合

    ● 使用 db.<集合>.drop() 来删除一个集合
    ● 集合中的全部文档都会被删除
    ● 集合相关的索引也会被删除
    db.colToBeDropped.drop()
    
    ● 使用 db.dropDatabase() 来删除数据库
    ● 数据库相应文件也会被删除,磁盘空间将被释放
    use tempDB
    db.dropDatabase()
    show collections // No collections
    show dbs // The db is gone

      

     上面组件中  Compass和BI Connector可以提高开发效率和减少学习成本,期待mongoDB的sql解释器可以开源。

  • 相关阅读:
    动态加载JS脚本【转】
    定义并且立即执行JS匿名函数拾遗
    javascript操作ASCII码与字符对转
    win7的mklink命令
    [Yii Framework] How to get the current static page name?
    [Ubuntu] 利用Ubuntu光盘破解win7用户登录 Crark the win7 user via Ubuntu live CD
    [Ubuntu] reload the .bashrc file without logout nor restart.
    [Ubuntu] the permissions of lampp mysql and phpmyadmin
    [Zend PHP5 Cerification] Some note when studying
    [eZ publish] How to modify the $view_parameters valus in the template.
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/12687811.html
Copyright © 2011-2022 走看看