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":
}
一、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";
- 更新 username = name2 的用户的 age 为30
四、D
- db.collection.remove()
- 删除 username = name3 的 记录
db.yourColl.remove({
username: "name3"
})相当于 MySQL 的
Delete from yourColl where username = "name3";
- 删除 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/Aupdate 一个 修改器对象 N/Anew 若为true,将返回修改后的对象而不是原始对象。在删除操作中,该参数被忽略。 false
外部资源链接:
MongoDB 入门之基础 DML的更多相关文章
- MongoDB 入门之基础 DCL
此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...
- MongoDB 入门之基础 DDL
此文章主要记录部分主要的 MongoDB 的 DDL 操作. db 查看当前所在的数据库(默认 test) > db test > show dbs 查看当前数据库服务器上的数据库名字 ...
- MongoDB入门实践
MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...
- mongodb入门篇
MongoDB 入门篇 分类: NoSQL, 故障解决 undefined 1.1 数据库管理系统 在了解MongoDB之前需要先了解先数据库管理系统 1.1.1 什么是数据? 数据(英语:data) ...
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...
- MongoDB入门三:MongoDB shell
MongoDB shell MongDB shell是一个功能完备的Javascript解释器,可以运行Javascript程序.也可以用于连接MongoDB服务器,执行脚本,对数据库进行操作.类似于 ...
- Mongodb FAQ fundamentals(基础篇)
Mongodb FAQ(基础篇),是官方文档的翻译.如有翻译不到之处,还请谅解. 1.Mongdb是什么数据库? mongodb是一个面向文档(document)的数据库,既不支持表连接,也不支持事务 ...
- MongoDB 入门之查询(find)
MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...
- MongoDB入门简介
MongoDB入门简介 http://blog.csdn.net/lolinzhang/article/details/4353699 有关于MongoDB的资料现在较少,且大多为英文网站,以上内容大 ...
随机推荐
- Centos7 ZooKeeper 安装过程
www.apache.org/dist/上可以下载Hadoop整个生态环境的组件,我下的Zookeeper3.4.6版本 我一般都是在一个虚拟机上将一.二步都做完,然后克隆出来,再到克隆出来的虚拟机上 ...
- 线程变量ThreadLocal的使用
我们有时候会通过token进行多次查询(猪:token是redis中的key),比如: 一次是在登录拦截器中,一次是在controller的业务中查询,这样存在性能和资源的浪费问题!!! 那么如何将拦 ...
- 杨辉三角用java实现
代码如下: public class ErArray { public static void main(String[] args) { //杨辉三角 int[][] num = new int[1 ...
- [译] 用 Swift 创建自定义的键盘
本文翻译自 How to make a custom keyboard in iOS 8 using Swift 我将讲解一些关于键盘扩展的基本知识,然后使用iOS 8 提供的新应用扩展API来创建一 ...
- 4.1 pair类模板
在学习关联容器之前,首先先要了解一下STL中的pair类模板,因为关联容器的一些成员函数返回值都是pair对象,而且map 和multimap中的元素都是pair对象. 1)pair类模板定义 pai ...
- IOS之笑脸app
ios笑脸app实现 import UIKit @IBDesignable class FaceView: UIView { @IBInspectable var lineWidth:CGFloat= ...
- Ubuntu16.04 + Win 10 双系统 时间同步,启动项顺序,NumLock指示灯常亮
1. Ubuntu & win10 双系统时间同步: 先在ubuntu下更新一下时间,确保时间无误: sudo apt-get install ntpdate sudo ntpdate tim ...
- DevExpress DXperience 的本地化(汉化)方法
Devexpress的.net组件目前非常流行,在国内开发者中有非常高的热度,但是由于是国外控件,我们经常遇到的一个问题是汉化.目前Devexpress公司2011.2版以后使用了统一的本地化模式,针 ...
- proxifier 代理bluestack
proxycap 可以很方便的代理bluestack, 但是proxycap 的破解版现在越来越不好用了,而且不小心还会中个病毒,这个时候免费的proxifier就显得更加的可爱了. 但是有个问题,就 ...
- 杂物 git rebase