此文章主要记录部分主要的 MongoDB Collection 的 DML 操作。

文章中的 Collection 名字为 yourColl,每一次操作包含以下两条初始数据

{
"_id": ObjectId("5438df36309dca34635d4460"),
"username": "name1",
"mail": "name1@abc.com"
},
{
"_id": ObjectId("5438df40309dca34635d4461"),
"username": "name2",
"mail": "name2@abc.com",
"age":
}

一、C

  • db.collection.insert()

    db.yourColl.insert({
    username: "mayj",
    mail: "test@abc.com"
    })
  • db.collection.save()

    db.yourColl.save({
    username: "mayj",
    mail: "test@abc.com"
    })

    Note: save 操作时如果 documents 当中包含有 主键(_id),去判断主键是否存在,如果存在,则更新原记录,如果不存在,则插入新记录。insert 与save 的区别在于如果一个 document 当中包含有主键(_id)且存在,则不处理处理,返回错误。

二、R

  • db.collection.find()

    • 查询所有记录

      db.yourColl.find();

      相当于 MySQL 的

      Select * from yourColl;
    • 查询 distinct 某列不重复的数据
      db.yourColl.distinct("username"); 

      相当于 MySQL 的

      Select distict name from yourColl; 
    • 查询 age=28 的数据
      db.yourColl.find({
      age: 28,
      })

      相当于 MySQL 的

      Select * from yourColl where age = 28;
    • 查询 age >20 的数据
      db.yourColl.find({
      age: {$gt:20}
      })

      相当于 MySQL 的

      Select * from yourColl where age > 20;
    • 查询 age >=20 的数据
      db.yourColl.find({
      age: {$gte:20}
      })

      相当于 MySQL 的

      Select * from yourColl where age >= 20;
    • 查询 age >=20且 age < 30 的数据
      db.yourColl.find({
      age: {$gte: 20, $lt: 30}
      })

      相当于 MySQL 的

      Select * from yourColl where age >= 20 and age < 30; 
    • 查询 mail 中包含 abc 的数据
      db.yourColl.find({
      mail: /abc/i
      })

      相当于 MySQL 的

      Select * from yourColl where mail like "%abc%";
    • 查询 username 中以 name 开头的数据
      db.yourColl.find({
      mail: /^abc/i
      })

      相当于 MySQL 的

      Select * from yourColl where mail like "%^abc%";
    • 查询 username =name1,mail =name1@abc.com 的数据
      db.yourColl.find({
      username: "name1",
      mail: "name1@abc.com"
      })

      相当于 MySQL 的

      Select * from yourColl where username = "name1" and mail = "name1@abc.com";
    • 查询指定列 username 和 mail 的数据
      db.youColl.find({
      
      },
      {
      username: true,
      mail: 1
      })

      相当于 MySQL 的

      Select username, mail from yourColl;

      其中 true 可以用1代替。

    • 按照 username 升序,mail 降序排列
      db.yourColl.find({}).sort({username:1,mail:-1})

      相当于 MySQL 的

      Select * from yourColl order by username ASC, mail DESC;
    • 查询1-2条数据
      db.yourColl.find({}).limit(2).skip(1)

      相当于 MySQL 的

      Select * from yourColl limit 1,1 
    • 查询 username = name1 或者 username =name2 的数据

      db.yourColl.find({
      $or:[{username: "name1"}, {username: "name2"}]
      })

      相当于 MySQL 的

      Select * from yourColl where username = "name1" or username = "name2";
    • 查询符合条件的记录数
      db.collect1.find({}).count()

      相当于 MySQL 的

      Select count(*) from yourColl
  • db.collection.findOne()
    findOne 是一种对find的补充, 与find的区别是:
    • 如果存在符合条件的记录,findOne只返回其中的第一条记录,而不是返回cursor。
    • 当不存在符合条件记录的时候,findOne返回null。

三、U

  • db.collection.update()

    • 更新 username = name2 的用户的 age 为30

      db.yourColl.update(
      {
      username: "name2"
      },
      {
      $set: {
      age: 30
      }
      },
        false,
        true
      )

      相当于 MySQL 的

      Update yourColl set age = 30 where username = "name2";
    • 更新 username = name2 的用户的 age 为 age + 30
      db.yourColl.update(
      {
      username: "name2"
      },
      {
      $inc: {
      age: 30
      }
      },
        false,
        true
      )

      相当于 MySQL 的

      Update yourColl set age = age + 30 where username = "name2";
    • 更新 username = name2 的用户的 username = name3 age 为 age + 30
      db.yourColl.update(
      {
      username: "name2"
      },
      {
      $inc: {
      age: 30
      },
      $set: {
      username: "name3"
      }
      },
        false,
        true
      )

      相当于 MySQL 的

      Update yourColl set age = age + 30, username = "name3" where username = "name2";

四、D

  • db.collection.remove()

    • 删除 username = name3 的 记录

      db.yourColl.remove({
      username: "name3"
      })

      相当于 MySQL 的

      Delete from yourColl where username = "name3";

五、RUD

  • db.collection.findAndModify() & db.collection.runCommond()
  • db.users.findAndModify({
    query: {age: {$gte: 25}},
    sort: {age: -1},
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
    });
    db.runCommand({
    findandmodify : "users",
    query: {age: {$gte: 25}},
    sort: {age: -1},
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
    });
    参数 详解 默认值 
    query 查询过滤条件 {} 
    sort 如果多个文档符合查询过滤条件,将以该参数指定的排列方式选择出排在首位的对象,该对象将被操作 {} 
    remove 若为true,被选中对象将在返回前被删除 N/A 
    update 一个 修改器对象 N/A 
    new 若为true,将返回修改后的对象而不是原始对象。在删除操作中,该参数被忽略。 false 

外部资源链接:

MongoDB CRUD Introduction

MongoDB 入门之基础 DML的更多相关文章

  1. MongoDB 入门之基础 DCL

    此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...

  2. MongoDB 入门之基础 DDL

    此文章主要记录部分主要的 MongoDB 的 DDL 操作. db  查看当前所在的数据库(默认 test) > db test > show dbs  查看当前数据库服务器上的数据库名字 ...

  3. MongoDB入门实践

    MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...

  4. mongodb入门篇

    MongoDB 入门篇 分类: NoSQL, 故障解决 undefined 1.1 数据库管理系统 在了解MongoDB之前需要先了解先数据库管理系统 1.1.1 什么是数据? 数据(英语:data) ...

  5. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  6. MongoDB入门三:MongoDB shell

    MongoDB shell MongDB shell是一个功能完备的Javascript解释器,可以运行Javascript程序.也可以用于连接MongoDB服务器,执行脚本,对数据库进行操作.类似于 ...

  7. Mongodb FAQ fundamentals(基础篇)

    Mongodb FAQ(基础篇),是官方文档的翻译.如有翻译不到之处,还请谅解. 1.Mongdb是什么数据库? mongodb是一个面向文档(document)的数据库,既不支持表连接,也不支持事务 ...

  8. MongoDB 入门之查询(find)

    MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...

  9. MongoDB入门简介

    MongoDB入门简介 http://blog.csdn.net/lolinzhang/article/details/4353699 有关于MongoDB的资料现在较少,且大多为英文网站,以上内容大 ...

随机推荐

  1. 413. Arithmetic Slices

    /**************************Sorry. We do not have enough accepted submissions.*********************** ...

  2. JavaScript的内置对象和浏览器对象

    在javascript中对象通常包括两种类型:内置对象和浏览器对象,此外,用户还可以自定义对象. 对象包含两个要素:1.用来描述对象特性的一组数据,也就是若干变量,通常称为属性.2.用来操作对象特性的 ...

  3. 让ASP.NET MVC不使用jsonp也可以跨域访问

    跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服务器 ...

  4. Linux Shell 高级编程技巧2----shell工具

    2.shell工具    2.1.日志文件        简介            创建日志文件是很重要的,记录了重要的信息.一旦出现错误,这些信息对于我们排错是非常有用的:监控的信息也可以记录到日 ...

  5. 【JAVA线程间通信技术】

    之前的例子都是多个线程执行同一种任务,下面开始讨论多个线程执行不同任务的情况. 举个例子:有个仓库专门存储货物,有的货车专门将货物送往仓库,有的货车则专门将货物拉出仓库,这两种货车的任务不同,而且为了 ...

  6. 算法系列:geometry

    1.基本几何变换及变换矩阵 基本几何变换都是相对于坐标原点和坐标轴进行的几何变换,有平移.比例.旋转.反射和错切等. 1.1 平移变换 是指将p点沿直线路径从一个坐标位置移到另一个坐标位置的重定位过程 ...

  7. golang json 包简单分析

    首先上代码: func main() { b := true a1, _ := json.Marshal(b) a2, _ := Marshal(b) fmt.Println(string(a1)) ...

  8. APUE包含头文件"apue.h"问题

    下载源码 从unix高级编程书籍官网下载书籍的上的所有源码. wget http://www.apuebook.com/src.tar.gz 解压这个文件 tar -zxvf src.tar.gz 解 ...

  9. office excel 装Visual Studio后报错解决方案

    安装完vs后,vs会向office安装COM加载项,但是在启动Excel时会发生弹出此加载项安装出错的消息,如下图. 名称: 从: file:///D:/Program Files (x86)/Mic ...

  10. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...