zoukankan      html  css  js  c++  java
  • mongodb

    mongodb

     安装:`apt-get install mongodb`
    

    基本使用

    • show dbs 查看数据库
    • use <db name> 创建并切换数据库
      • 如果新建数据库的内容为空,用show dbs不会显示空的新建数据库
    • db dropDatabase() 删除当前所在的数据库

    集合的操作语句

    • db.createCollections("<collection name>") 创建集合
    • show collections 查看集合
    • db.<collection name>.drop 删除集合
    • db.<collection name>.insert(value) 向集合中插入数据
      • 如果集合不存在,会自动创建集合
      • db.student.insert({"name": "ivy", sex: "male", age: 22}) 单条插入
      • db.student.insert([{"name": "ivy", sex: "male", age: 22}, {"name": "ivy", sex: "male", age: 22}]) 多条插入
      • 如果不指定_id参数,mongodb会自动给该条数据制定一个唯一的_di
    • db.<collection name>.find() 无条件查询
    • db.<collection name>.find(condictions) ** 有条件查询**
      • db.student.find({age: 22})
    • db.<collection name>.find().pretty() 格式化输出

    逻辑运算符

    操作符 描述
    $ne 不等于
    $gt 大于
    $lt 小于
    $gte 大于等于
    $lte 小于等于
    • 使用

      • db.<collection name>.find({age: {$lte: 25}}) 查找年龄小于等于25的数据
    • db.<collection name>.find({$or: [{condiction}, {condiction}, ....]}) ** or查询**

      • db..find({$or: [{name: ivy}, {age: 23}]})`` 查找name为ivy或者age为23的数据
    • db.<collection name>.find({$and: [{condiction}, {condiction}, ....]}) and查询

    • db.<collection name>.update(<query>, <value>,{multi:false}) 更新数据, 当multi为true时,会修改所有匹配到的数据

      • db.<collection name>.update({name: ivy},{age:20}) 将name为ivy的数据的全部改为age为20 全字段更新
      • db.<collection name>.update({name: ivy},{$set: {age:20}}) 将name为ivy的数据的age该为20 局部字段更新
    • db.<collection name>.remove(<query>, {justOne: false})

  • 相关阅读:
    MySQL锁
    MySQL索引
    MySQL基础
    删除文件时提示:一个意外错误使您无法复制该文件夹0x80070570
    教育部认可的44项全国学科竞赛名单
    打开dnsmasq log
    使用gdb调试user程序
    ipv6获取地址
    vlc产生组播流
    xxl-job搭建、部署、SpringBoot集成xxl-job
  • 原文地址:https://www.cnblogs.com/ivy-blogs/p/11762486.html
Copyright © 2011-2022 走看看