此文章主要记录部分主要的 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. UbuntuLinux安装java

    jdk1.7,jdk1.8详情,参见:http://www.cnblogs.com/a2211009/p/4265225.html

  2. fork与vfork的区别与联系

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 2. fo ...

  3. hdu1115(计算多边形几何重心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给出一些点,求这些点围成的多边形的重心: 思路: 方法1:直接分别求所有点的x坐标的平均值 ...

  4. 数据库支持emoji表情

    从MySQL5.5.3开始,MySQL 支持一种utf8mb4的字符集,这个字符集能够支持4字节的UTF8编码的字符.utf8mb4字符集能够完美地兼容utf8字符串.在数据存储方面,当一个普通中文字 ...

  5. Linux USB Project

    转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...

  6. GMap.Net开发之地址解析与路径查找

    上一篇介绍了如何在GMap地图上添加多边形,这篇介绍下如何使用在线的地图服务进行“地址解析”和“路径查找”. 先看地址解析,GMap中的地址解析主要用到GeocodingProvider中的如下方法: ...

  7. android 入门-布局

    android:gravity 针对本view 的位置. android:layout_gravity 本view相对于父布局view的位置. android:layout_alignParentRi ...

  8. Win10 UAP 标题栏

    //自定义标题栏 var view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); ApplicationViewTi ...

  9. 用康托展开实现全排列(STL、itertools)

    康拓展开: $X=a_n*(n-1)!+a_{n-1}*(n-2)!+\ldots +a_2*1!+a_1*0!$ X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+ ...

  10. myeclipse报错: java compiler level does not match the version of the installed java project facet

    在升级到myeclipse 9.0正式版后,很无耐地出发现了一个error级别的错误,虽然没在代码中,但是看着让人很不舒服.第一反应就是到网上搜索解决之道,结果,网站说在工程的属性中去找个叫啥&quo ...