Mongodb---操作备忘
mysql/mongodb对比
CREATE TABLE USERS (a Number, b Number)
|
|
INSERT INTO USERS VALUES(1,1)
|
$db->users->insert(array("a" => 1, "b" => 1));
|
SELECT a,b FROM users
|
$db->users->find(array(), array("a" => 1, "b" => 1));
|
SELECT * FROM users WHERE age=33
|
$db->users->find(array("age" => 33));
|
SELECT a,b FROM users WHERE age=33
|
$db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
|
SELECT a,b FROM users WHERE age=33
|
$db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
|
SELECT a,b FROM users WHERE age=33 ORDER BY name
|
$db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1));
|
SELECT * FROM users WHERE age>33
|
$db->users->find(array("age" => array('$gt' => 33)));
|
SELECT * FROM users WHERE age<33
|
$db->users->find(array("age" => array('$lt' => 33)));
|
SELECT * FROM users WHERE name LIKE "%Joe%"
|
$db->users->find(array("name" => new MongoRegex("/Joe/")));
|
SELECT * FROM users WHERE name LIKE "Joe%"
|
$db->users->find(array("name" => new MongoRegex("/^Joe/")));
|
SELECT * FROM users WHERE age>33 AND age<=40
|
$db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
|
SELECT * FROM users ORDER BY name DESC
|
$db->users->find()->sort(array("name" => -1));
|
CREATE INDEX myindexname ON users(name)
|
$db->users->ensureIndex(array("name" => 1));
|
CREATE INDEX myindexname ON users(name,ts DESC)
|
$db->users->ensureIndex(array("name" => 1, "ts" => -1));
|
SELECT * FROM users WHERE a=1 and b='q'
|
$db->users->find(array("a" => 1, "b" => "q"));
|
SELECT * FROM users LIMIT 10 SKIP 20
|
$db->users->find()->limit(10)->skip(20);
|
SELECT * FROM users WHERE a=1 or b=2
|
$db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
|
SELECT * FROM users LIMIT 1
|
$db->users->find()->limit(1);
|
EXPLAIN SELECT * FROM users WHERE z=3
|
$db->users->find(array("z" => 3))->explain()
|
SELECT DISTINCT last_name FROM users
|
$db->command(array("distinct" => "users", "key" => "last_name"));
|
SELECT COUNT(*y) FROM users
|
$db->users->count();
|
SELECT COUNT(*y) FROM users where AGE > 30
|
$db->users->find(array("age" => array('$gt' => 30)))->count();
|
SELECT COUNT(AGE) from users
|
$db->users->find(array("age" => array('$exists' => true)))->count();
|
UPDATE users SET a=1 WHERE b='q'
|
$db->users->update(array("b" => "q"), array('$set' => array("a" => 1)));
|
UPDATE users SET a=a+2 WHERE b='q'
|
$db->users->update(array("b" => "q"), array('$inc => array("a" => 2)));
|
DELETE FROM users WHERE z="abc"
|
$db->users->remove(array("z" => "abc"));
|
sc.exe create MongoDB30 binPath= "d:\mongodb30\bin\mongod.exe -auth --logpath D:\mongodb\log\MongoDB.log --logappend --dbpath d:\mongodb\data --service " DisplayName= "MongoDB30" start= "auto"
db.test.find({"name":"joe"})
//不区分大小写的查询
db.test.find({"name":{"$regex":"WILLIAM","$options":"i"}})
db.test.find({"name":"joe"}).limit(20)
db.test.find({"name":"joe"}).limit(20).skip(20)
//select * from test where name like '%william%' 不含有
- db.test.find({"name":/.*william.*/i})
- //不含有
- db.test.find({"name":{$not:/.*william.*/i}})
db.test.ensureIndex({"name":1})
db.test.update({},{$unset:{"email":""}},{multi:true})
db.test.getIndexes()
db.test.renameCollection("newtablename")
mongoexport -d dbname -c tablename -u test -p -o "c:/db.json"
mongoexport -d dbname -c tablename -u test -p test -f "fieldname" -csv -o "c:/db.csv"
mongoimport -d test -c news -u test -p password "c:/db.json"
db.test.find({"name":"joe","atta.b":{$exists:true}}).count()
db.test.find({"name":"joe","atta.b":"bbb"}
db.test.update({"name":"william"},{"$pull":{"email":"aa@qq.com"}})
db.test.update({"sn" : "1","stock.fnfp.pn":"aa"} , {$push : { "stock.$.fnfp": {"price":"555"}}})
db.test.update({"name":"joe"},{$push:{"atta":{"d":"ddd"}}})
db.test.update({"name":"joe"},{$set:{"email":"a.163.com"}})
db.test.find({name: {$type: 2}})
db.test.find({age: {$type: 16}})
db.users.find({name: /^b.*/i})
db.users.find({age: {$gt: 18}}) db.users.find({$where: "this.age > 18"}) db.users.find("this.age > 18") f = function() {return this.age > 18} db.users.find(f)
db.users.find().limit(3).forEach(function(user) {print('my age is ' + user.age)})
结果
db.news.find().forEach(function(x){db.news_bak.insert(x)})
db.currentOp()
db.killOp(opid)
db.serverStatus()
db.stats()
db.collection.stats()
db.collection.dataSize()
db.collection.totalIndexSize()
db.changeUserPassword("reporting", "SOh3TbYhxuLiW8ypJPxmt1oOfL")
{ "name":"joe" "email": [ "aaa@163.com", {"qqemail": "aa@qq.com"} ] } db.test.update({"name":"joe"},{$pull:{"email":"aaa@163.com"}})
或者:
db.test.update({"name":"joe"},{$pull:{"email":{"qqemail":"aa@qq.com"}}}) upsert db.post.update({count:100},{"$inc":{count:10}},true)
在找不到count=100这条记录的时候,自动插入一条count=100,然后再加10,最后得到一条 count=110的记录
mongodump -h 127.0.0.1 -d dbname -u username -p -o d:/dbname.bak
mongorestore -h 127.0.0.1 -d dbname -u username -p password dbname.bak/dbname
db.test.find() { "_id":ObjectId("52428f5d5ab08c5e801f7209"), "price":[ {"digk":{"nt1":1,"nt2":2}}, {"avn":{"nt1":22,"nt2":23}} ], "sn":1 }
db.test.update({"sn":1},{$pop:{"price":0}})
移除price数组下的第0项
db.test.update({"sn":1},{$pull:{"price":{"avn":{"nt1":22,"nt2":23}}}})
缺陷必须知道price中的avn的
var cursor=db.test.find(); while(cursor.hasNext()){ var y=cursor.next(); for(var i=0;i<y.price.length;i++){ delete y.price[i].avn; } db.test.save(y); }
var cursor=db.test.find(); var i = 1; while(cursor.hasNext()){ var y=cursor.next(); var _id = y._id; db.test.update({"_id":_id},{$set:{"sn":i}}); i++; }
db.test.save({"sn":1, "contact": { "China":[ {"city":"beijing","email":"a@163.com","tel":"010-6666"}, {"city":"shanghai","email":"b@263.com","tel":"021-8888"}, {"city":"shenzhen","email":"c@sohu.com","tel":"0755-9999"} ], "United-States":[ {"city":"Newyork","email":"a@163.com","tel":"010-6666"}, {"city":"Washington","email":"a@163.com","tel":"010-6666"} ] } }) db.test.save({"sn":2, "contact": { "Japan":[ {"city":"tokyo","email":"a@163.com","tel":"010-6666"}, {"city":"Osaka","email":"b@263.com","tel":"021-8888"}, {"city":"Hokkaido","email":"c@sohu.com","tel":"0755-9999"} ], "Canada":[ {"city":"Vancouver","email":"a@163.com","tel":"010-6666"}, {"city":"Ottawa","email":"a@163.com","tel":"010-6666"} ] } })
var cursor = db.test.find({"sn":1}); while(cursor.hasNext()){ var y = cursor.next(); for(var i =0;i < y.contact.China.length;i++){ if(y.contact.China[i].city == 'shenzhen'){ delete y.contact.China[i]; } } db.test.save(y); }
db.test.update({"sn":1,"contact.China.city":"beijing"},{$set:{"contact.China.$.city":"guangzhou"}})
db.usersinfo.renameCollection("users")
use admin db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:'anyAction']}]})
use test db.grantRolesToUser("test",[{role:'sysadmin',db:'admin'}]) key:[{'id':123,'value':11},{'id':124,'value':12}]
查询匹配key里id与value皆匹配:find({'key':{$elemMatch:{"id":123,"value":12}}})可以做到返回不出结果。
Mongodb---操作备忘的更多相关文章
- Axure常用操作备忘
目录 前言 技巧 边框重合 复制对象文本居中 复制粘贴样式 文本自适应 给图形添加连接点 导出图片无空白 前言 下面列出Axure画图过程中曾经遇到过的问题,备忘一下,避免别人也走弯路,法布施一下~ ...
- mongo日常操作备忘
修改 普通修改 插入数据: db.students.insert({ "name":"swrd", "age":32, "grad ...
- DOS命令批量重命名文件配合Excel 操作备忘
批量取得文件夹下文件名 有时候文件夹下有好多图片什么的,文件名称不是统一的格式,想统一一下,于是google找到以下方法,进入要操作的目录,按住shift键的同时,单击鼠标右键,你会看到在此处打开命令 ...
- JavaScript 数组操作备忘
数组的定义: 方法1. var mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]=&qu ...
- docker常用操作备忘
一.docker安装 参考资料:阿里云镜像加速1. 安装/升级Docker客户端 curl -fsSL https://get.docker.com | bash -s docker --mirror ...
- MongoDB 基本命令备忘
使用admin数据库: use admin 显示数据库: show dbs 创建用户,并制定该用户的角色: db.createUser({user: "root",pwd:&quo ...
- mongo操作备忘
#查看collection内 某个字段条目数 db.dictionary_system.find({"name":"xxx"}).count() #清空某个co ...
- QT常规控件操作备忘
QLabel设置边框和颜色: label->setFrameShape (QFrame::Box); label->setStyleSheet("border: 1px soli ...
- eclipse如何修改dynamic web module version;——eclipse操作备忘
1.eclipse如何修改dynamic web module version 一.修改工程属性: 右键eclipse的工程,选择属性,再选择Project Facets里面中选择Dynamic We ...
- iscsi 操作备忘
#查找iscsi节点 iscsiadm -m discovery -t st -p 192.168.20.225 #列出可用节点 iscsiadm -m node -T iqn.com.exapmle ...
随机推荐
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- SharePoint2007 管理中心TimerJob”计时器作业定义”页面报错
部署完成自己开发的TimerJob,发现"计时器作业定义"页面报错,进不去了,但是TimerJob可以正常运行,这样,肯定不行的,估计还是代码有问题,把这个脆弱的sharepoin ...
- 创建Sencha touch第一个应用
最近学习Sencha touch ,是一个菜鸟级别.废话不多说,让我们来创建Sencha touch的第一应用. 首先,我们下载Sencha touch2.0 sdk 和SDK工具. SDK工具直接 ...
- 数据准备<2>:数据质量检查-实战篇
上一篇文章:<数据质量检查-理论篇>主要介绍了数据质量检查的基本思路与方法,本文作为补充,从Python实战角度,提供具体的实现方法. 承接上文,仍然从重复值检查.缺失值检查.数据倾斜问题 ...
- 飞鱼相册笔记(1)----外置SD卡文件夹名称不区分大小写
飞鱼相册笔记(1)----外置SD卡文件夹名称不区分大小写 在飞鱼相册发布的第一个测试版中,很多用户表示无法查看外置SD卡中的照片.乍一听觉得加个外置SD卡的根目录,然后在扫描所有图片的时候把这个根目 ...
- RecyclerView 与 Scrollview 搭配使用的两个坑
RecyclerView & Scrollview & wrap_content RecyclerView wrap_content 用android.support.v4.widge ...
- python 3下基于select模型的事件驱动机制程序
它的基本原理就是select/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程.它的流程如图: 当用户进程调用了select,那么整个 ...
- 解决:MySQL 报错:1045 - Access denied for user 'root'@'localhost'(using password YES)
一.前言 今年疯狂迷上了开源,只要看到好的开源项目,就会不顾一切一股脑扎进去研究,五一期间发现一个很好的关于众筹的开源项目,但不巧,这个项目竟然是 PHP 写的,没学过 PHP,自然对这个开源项目毫无 ...
- CoolBlog开发笔记第5课:请求与响应
教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 1.3 CoolBlog开发笔记第3课:创建Django应用 1.4 CoolBlog ...
- Java 8 Stream
1.关于Java8部分新特性介绍 Java8的新特性很多,在此就不一一介绍了,这里只说一下我自己在工作用用得比较多的几点: 1.1.Lambda表达式 Lambda允许把函数作为一个方法的参数(函数作 ...