此文章主要记录部分主要的 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. Mysql源码分析--csv存储引擎

    一直想分析下mysql的源码,开始的时候不知道从哪下手,先从csv的文件存储开始吧,这个还是比较简单的.我是用的是mysql5.7.16版本的源码. csv源码文件在mysql源码的mysql-5.7 ...

  2. MVC中Form表单的提交

    概述 Web页面进行Form表单提交是数据提交的一种,在MVC中Form表单提交到服务器.服务端接受Form表单的方式有多种,如果一个Form有2个submit按钮,那后台如何判断是哪个按钮提交的数据 ...

  3. Install PIL on mac osX10.9

    follow this instruction: http://blog.csdn.net/liushuaikobe/article/details/8729652 and if you encoun ...

  4. spring 集成 log4j 配置

    在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> < ...

  5. Spring XML配置文件示例(二)——web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...

  6. android 入门-git之上传本地代码到github

    github部分: 1.首先去github网站 上注册一个用户 2.说明 https://guides.github.com/activities/hello-world/ 2.点击 New repo ...

  7. POJ 1061 同余方程

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是 它们出发之前忘记了一件很重要的事情,既没有问清楚对方的 ...

  8. Visual Studio 2015将在7月20号RTM

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:用了3个多月的VS 2015终于要迎来RTM了,不过感觉有点淡淡的忧伤(为什么呢?请看 ...

  9. Java 对象序列化(Serialization Object)

    官网文档:https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html 优秀博客: http://www.cnblogs.com/g ...

  10. 1:A+B Problem

    总时间限制:  1000ms  内存限制:  65536kB 描述 Calculate a + b 输入 Two integer a,,b (0 ≤ a,b ≤ 10) 输出 Output a + b ...