Mongodb语法总结(一)

2015-01-25 19:39:51 · 作者: · 浏览: 127
  1. MySQL
  1. MongoDB
  1. 说明
  1. mysqld
  1. mongod
  1. 服务器守护进程
  1. mysql
  1. mongo
  1. 客户端工具
  1. mysqldump
  1. mongodump
  1. 逻辑备份工具
  1. mysql
  1. mongorestore
  1. 逻辑恢复工具
  1. ?
  1. db.repairDatabase()
  1. 修复数据库
  1. mysqldump
  1. mongoexport
  1. 数据导出工具
  1. source
  1. mongoimport
  1. 数据导入工具
  1. grant * privileges on *.* to …
  1. Db.addUser()
  2. Db.auth()
  1. 新建用户并权限
  1. show databases
  1. show dbs
  1. 显示库列表
  1. Show tables
  1. Show collections
  1. 显示表列表
  1. Show slave status
  1. Rs.status
  1. 查询主从状态
  1. Create table users(a int, b int)
  1. db.createCollection("mycoll", {capped:true,
  2. size:100000}) 另:可隐式创建表。
  1. 创建表
  1. Create INDEX idxname ON users(name)
  1. db.users.ensureIndex({name:1})
  1. 创建索引
  1. Create INDEX idxname ON users(name,ts DESC)
  1. db.users.ensureIndex({name:1,ts:-1})
  1. 创建索引
  1. Insert into users values(1, 1)
  1. db.users.insert({a:1, b:1})
  1. 插入记录
  1. Select a, b from users
  1. db.users.find({},{a:1, b:1})
  1. 查询表
  1. Select * from users
  1. db.users.find()
  1. 查询表
  1. Select * from users where age=33
  1. db.users.find({age:33})
  1. 条件查询
  1. Select a, b from users where age=33
  1. db.users.find({age:33},{a:1, b:1})
  1. 条件查询
  1. select * from users where age<33
  1. db.users.find({'age':{$lt:33}})
  1. 条件查询
  1. select * from users where age>33 and age<=40
  1. db.users.find({'age':{$gt:33,$lte:40}})
  1. 条件查询
  1. select * from users where a=1 and b='q'
  1. db.users.find({a:1,b:'q'})
  1. 条件查询
  1. select * from users where a=1 or b=2
  1. db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
  1. 条件查询
  1. select * from users limit 1
  1. db.users.findOne()
  1. 条件查询
  1. select * from users where name like "%Joe%"
  1. db.users.find({name:/Joe/})
  1. 模糊查询
  1. select * from users where name like "Joe%"
  1. db.users.find({name:/^Joe/})
  1. 模糊查询
  1. select count(1) from users
  1. Db.users.count()
  1. 获取表记录数
  1. select count(1) from users where age>30
  1. db.users.find({age: {'$gt': 30}}).count()
  1. 获取表记录数
  1. select DISTINCT last_name from users
  1. db.users.distinct('last_name')
  1. 去掉重复值
  1. select * from users ORDER BY name
  1. db.users.find().sort({name:-1})
  1. 排序
  1. select * from users ORDER BY name DESC
  1. db.users.find().sort({name:-1})
  1. 排序
  1. EXPLAIN select * from users where z=3
  1. db.users.find({z:3}).explain()
  1. 获取存储路径
  1. update users set a=1 where b='q'
  1. db.users.update({b:'q'}, {$set:{a:1}}, false, true)
  1. 更新记录
  1. update users set a=a+2 where b='q'
  1. db.users.update({b:'q'}, {$inc:{a:2}}, false, true)
  1. 更新记录
  1. delete from users where z="abc"
  1. db.users.remove({z:'abc'})
  1. 删除记录
  1. ?
  1. db. users.remove()
  1. 删除所有的记录
  1. drop database IF EXISTS test;
  1. use test
  2. db.dropDatabase()
  1. 删除数据库
  1. drop table IF EXISTS test;
  1. db.mytable.drop()
  1. 删除表/collection
  1. ?
  1. db.addUser(‘test’, ’test’)
  1. 添加用户
  2. readOnly-->false
  1. ?
  1. db.addUser(‘test’, ’test’, true)
  1. 添加用户
  2. readOnly-->true
  1. ?
  1. db.addUser("test","test222")
  1. 更改密码
  1. ?
  1. db.system.users.remove({user:"test"})
  2. 或者db.removeUser('test')
  1. 删除用户
  1. ?
  1. use admin
  1. 超级用户
  1. ?
  1. db.auth(‘test’, ‘test’)
  1. 用户授权
  1. ?
  1. db.system.users.find()
  1. 查看用户列表
  1. ?
  1. show users
  1. 查看所有用户
  1. ?
  1. db.printCollectionStats()
  1. 查看各collection的状态
  1. ?
  1. db.printReplicat