MySQL_-_MongoDB命令行工具及SQL语法对比

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

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

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

2、s 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 from users where

3、 age=33 select * from users where age33 and age30selectDISTINCT 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.statusdb.createC

4、ollection(“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”:$gt:33,$lte:40)db

5、.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.find(z:3).explain(

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

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

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

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

10、e: 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 外的全部数据支持, , = 查询,需用符号替代分别为$lt,$lte,$gt,$gt

11、e 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.find( “field” : $in

12、 : 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.colls.find( a : $

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

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

最新文档


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

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