MySQL - MongoDB命令行工具及SQL语法对比

上传人:碎****木 文档编号:220862288 上传时间:2021-12-09 格式:DOCX 页数:5 大小:25.14KB
返回 下载 相关 举报
MySQL - MongoDB命令行工具及SQL语法对比_第1页
第1页 / 共5页
MySQL - MongoDB命令行工具及SQL语法对比_第2页
第2页 / 共5页
MySQL - MongoDB命令行工具及SQL语法对比_第3页
第3页 / 共5页
亲,该文档总共5页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《MySQL - MongoDB命令行工具及SQL语法对比》由会员分享,可在线阅读,更多相关《MySQL - MongoDB命令行工具及SQL语法对比(5页珍藏版)》请在金锄头文库上搜索。

1、MySQL - MongoDB 命令行工具及 SQL 语法比照传统的关系数据库一般由数据库database、表table、记录record三个层次概念组成,MongoDB 是由数据库database、集合collection、文档对象document三个层次组成。MongoDB 对于关系型数据库里的表,但是集合中没有列、行和关系概念,这表达了模式自由的特点。mysqld mysql mysqldump mysqlMySQLmongod mongo mongodump mongorestoreMongoDB说明效劳器守护进程客户端工具规律备份工具规律恢复工具mysqldump sourcegra

2、nt * privileges on *.* to show databases Show tables Show slave statusCreate table users(a int, b int)Create INDEX idxname ON users(name)Create INDEX idxname ON users(name,ts DESC) Insert into users values(1, 1)Select a, b from users Select * from usersSelect * from users where age=33 Select a, b fr

3、om users where age=33 select * from users where age33 and age30 selectDISTINCT last_namefrom users select* from users ORDER BY name select* from users ORDER BY name DESC EXPLAIN select* from users where z=3db.repairDatabase() mongoexport mongoimport Db.addUser() Db.auth()showdbsShow collections Rs.s

4、tatusdb.createCollection(“mycoll“, capped:true, size:100000)另:可隐式创立表。db.users.ensureIndex(name:1) db.users.ensureIndex(name:1,ts:-1) db.users.insert(a:1, b:1)db.users.find(,a:1, b:1) db.users.find() db.users.find(age:33) db.users.find(age:33,a:1, b:1)db.users.find(”age”:$lt:33)db.users.find(”age”:$g

5、t:33,$lte:40)db.users.find(a:1,b:”q”)db.users.find( $or : a : 1 , b : 2 ) db.users.findOne() db.users.find(name:/Joe/) db.users.find(name:/Joe/) Db.users.count()db.users.find(age: ”$gt”: 30).count() db.users.distinct(”last_name”) db.users.find().sort(name:-1)db.users.find().sort(name:-1)db.users.fin

6、d(z:3).explain()修复数据库 数据导出工具数据导入工具新建用户并权限显示库列表 显示表列表 查询主从状态创立表创立索引创立索引插入记录查询表 查询表 条件查询条件查询条件查询条件查询条件查询条件查询条件查询模糊查询模糊查询猎取表记录数猎取表记录数去掉重复值 排序排序猎取存储路径update users set a=1 where b=”q” update users set a=a+2 where b=”q”db.users.update(b:”q”, $set:a:1, false, true)更新记录db.users.update(b:”q”, $inc:a:2, false

7、, true)更新记录deletefrom users where z=“abc“ dropdatabase IF EXISTS test; droptable IF EXISTS test;db.users.remove(z:”abc”) db.users.remove()use test db.dropDatabase() db.mytable.drop()删除记录删除全部的记录删除数据库删除表/collectiondb.addUser(test, test) db.addUser(test, test, true)db.addUser(“test“,“test222“) db.syste

8、m.users.remove(user:“test“) 或者 db.removeUser(”test”)use admin db.auth(test, test) db.system.users.find() show users db.printCollectionStats()db.printReplicationInfo() show profile添加用户readOnly-false 添加用户readOnly-true 更改密码删除用户超级用户用户授权查看用户列表查看全部用户查看各 collection的状态查看主从复制状态查看 profilingdb.copyDatabase(”ma

9、il_addr”,”mail_addr_tmp”)拷贝数据库db.users.dataSize()查看 collection 数据的大小db.users.totalIndexSize()查询索引的大小mongodb 语法MongoDB 的好处挺多的,比方多列索引,查询时可以用一些统计函数,支持多条件查询,但是目前多表查询是不支持的,可以想方法通过数据冗余来解决多表查询的问题。MongoDB 对数据的操作很丰富,下面做一些举例说明,内容大局部来自官方文档,另外有局部为自己理解。查询 colls 全部数据db.colls.find() /select * from colls通过指定条件查询db.

10、colls.find(last_name: Smith);/select * from colls where last_name=Smith指定多条件查询db.colls.find( x : 3, y : “foo” );/select * from colls where x=3 and y=foo指定条件范围查询db.colls.find(j: $ne: 3, k: $gt: 10 );/select * from colls where j!=3 and k10查询不包括某内容db.colls.find(, a:0);/查询除 a 为 0 外的全部数据支持, , =查询,需用符号替代分

11、别为$lt,$lte,$gt,$gte db.colls.find( “field” : $gt: value );db.colls.find( “field” : $lt: value );db.colls.find( “field” : $gte: value );db.colls.find( “field” : $lte: value );也可对某一字段做范围查询db.colls.find( “field” : $gt: value1, $lt: value2 );不等于查询用字符$ne db.colls.find( x : $ne : 3 );in 查询用字符$indb.colls.f

12、ind( “field” : $in : array );db.colls.find(j:$in: 2,4,6);not in 查询用字符$nin db.colls.find(j:$nin: 2,4,6);取模查询用字符$moddb.colls.find( a : $mod : 10 , 1 )/ where a % 10 = 1$all 查询db.colls.find( a: $all: 2, 3 );/指定 a 满足数组中任意值时$size 查询db.colls.find( a : $size: 1 );/对对象的数量查询,此查询查询a 的子对象数目为 1 的记录$exists 查询db.

13、colls.find( a : $exists : true ); / 存在a 对象的数据db.colls.find( a : $exists : false ); / 不存在a 对象的数据$type 查询$type 值为 bson :/bsonspec.org/数据的类型值db.colls.find( a : $type : 2 ); / 匹配a 为 string 类型数据db.colls.find( a : $type : 16 ); / 匹配a 为 int 类型数据使用正那么表达式匹配db.colls.find( name : /acme.*corp/i );/类似于 SQL 中 like内嵌对象查询db.colls.find( “author.name” : “joe” );1.3.3 版本及更高版本包含$not 查询db.colls.find( name : $not : /acme.*corp/i ); db.colls.find( a : $not : $mod : 10 , 1 );sort()排序db.colls.find().sort( ts : -1 )

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号