MongoDB学习02之语法小结
2014-06-29 18:38
225 查看
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。
mongoDB 常用命令:
如果mydb数据库不存在,则会自动创建,创建后如果没有往里面建用户,建集合等任何操作,则退出时,会自动删掉。
或db.getCollectionNames();
xxx就是集合名称,比如db.student.drop()
db.student.insert({'name':'貂蝉','age':16}) 成功
db.student.insert({'name':'貂蝉','age':16}) 成功
db.student.insert({'_id':1,'name':'貂蝉','age':16}) 成功
db.student.insert({'_id':1,'name':'甄宓','age':15}) 失败,因为_id=1已存在
db.student.save({'_id':1,'name':'甄宓','age':15}) 成功,新的文档替换掉旧文档,就像JAVA的Map操作一样。
db.student.remove({});
(2)按条件删除
db.student.remove({'name':'貂蝉'})
相当于关系型数据库的update student set age = 18 where name = '貂蝉';
mongoDB 常用命令:
database
(1)查看帮助
db.help()(2)查看当前连接的数据库名称
db.getName()(3)查看所有数据库名称
show dbs(4)切换/创建数据库
use mydb如果mydb数据库不存在,则会自动创建,创建后如果没有往里面建用户,建集合等任何操作,则退出时,会自动删掉。
(5)删掉当前数据库
db.dropDatabase()(6)修复当前数据库
db.epairDatabase()(7)查看数据库版本
db.version()(8)查看当前数据库的连接地址
db.getMongo()collection
collection,可以理解为mysql或者oracle这些关系数据库中的表(1)创建一个集合(table)
db.createCollection('studnet');(2)查看所有集合
show cellections或db.getCollectionNames();
(3)查看所有集合的状态信息
db.printCollectionStats();(4)删除集合
db.xxx.drop();xxx就是集合名称,比如db.student.drop()
document
关系型数据库(mysql,oracle等)表中的数据是有许多结构相同(列的类型,个数相同)的行(row)组成的。MongoDB的集合(collection)的数据就是有许多文档(document)组成的,每一个文档不要求一定都类型相同。学习JAVA的同学都知道Map<Key,Value>,那么可以这样理解MongoDB的集合(collection)<_id,文档>._id是特有的,如果你往collection里面插入一个文档,如果不指定_id的值,则系统自动帮你生成。insert/save
使用insert, 如果不指定_id的值,则相同的文档可以插入任意多个;如果指定了_id的值,如果已存在文档,则报错.db.student.insert({'name':'貂蝉','age':16}) 成功
db.student.insert({'name':'貂蝉','age':16}) 成功
db.student.insert({'_id':1,'name':'貂蝉','age':16}) 成功
db.student.insert({'_id':1,'name':'甄宓','age':15}) 失败,因为_id=1已存在
db.student.save({'_id':1,'name':'甄宓','age':15}) 成功,新的文档替换掉旧文档,就像JAVA的Map操作一样。
remove
(1)删除所有文档db.student.remove({});
(2)按条件删除
db.student.remove({'name':'貂蝉'})
update
db.student.update({'name':'貂蝉'},{$set:{'age':18}});相当于关系型数据库的update student set age = 18 where name = '貂蝉';
query
MongoDB | Mysql |
db.student.count() | select count(*) from student |
db.student.find({'name':'貂蝉'}) | select * from student where name = '貂蝉' |
db.student.find({'name':'貂蝉'}).count() | select count(*) from student where name = '貂蝉' |
db.student.find({'age':{$lt:20}}); | select * from student where age < 20 |
db.student.find({'age':{$lte:20}}); | select * from student where age < =20 |
db.student.find({'age':{$gt:20}}); | select * from student where age > 20 |
db.student.find({'age':{$gte:20}}); | select * from student where age >= 20 |
db.student.find({'name':{$in:['貂蝉','甄宓']}}) | select * from student where name in ('貂蝉','甄宓') |
db.student.find({'name':/尚/}) | select * from student where name like '%尚%' |
db.student.find({'name':/^孙尚/}) | select * from student where name like '孙尚%' |
db.student.find({'name':/香^/}) | select * from student where name like '%香' |
db.student.distinct('name') | select distinct name from student |
db.student.find().sort({'age':-1}) | select * from student order by age desc |
db.student.find().sort({'age':1}) | select * from student order by age asc |
db.student.find().sort({'age':1},{'heigth':-1}) | select * from student order by age asc , heigth desc |
db.student.findOne() | select * from student limit 1 |
db.student.find().skip(10).limit(20) | select * from student limit 10,20 |
相关文章推荐
- MongoDB 语法使用小结
- OC语法学习小结
- OC 学习小结之点语法
- MongoDB系列教程(五):mongo语法和mysql语法对比学习
- MongoDB 语法使用小结
- Python基本语法学习小结
- MongoDB for node.js developers 七周学习-最终测试小结(1)
- Python学习02- 基本语法与数据结构
- OpenGL学习02_函数语法与命名规范
- mongodb学习02基础知识
- OC语法学习小结
- MongoDB学习小结
- objective -c OC语法学习小结
- 【JavaScript】02.基础语法学习
- MongoDB 语法和mysql语法对比学习
- Mongodb中更新的学习小结
- Mongodb中更新的学习小结
- MongoDB(五)mongo语法和mysql语法对比学习
- OC学习小结之copy语法
- mongodb 学习笔记 02 -- CURD操作