MongoDB 入门之基础 DML

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

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

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

一、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

posted @ 2014-10-14 17:24  TannerBG  阅读(464)  评论(0编辑  收藏  举报