就一数据库,掌握基本用法,其他的现学现卖就行了.

所以要把握基本套路.

创建数据库=>使用数据库=>创建集合=>使用集合=>创建文档=>使用文档

1.数据库

mongodb和mysql目前我发现了一个比较明显的区别是:

  mysql要想使用一个数据库,必须先创建一个数据库,而mongodb是直到你真正想某个数据库写入才会生成这个数据库.

数据库基本操作无非那么几个:

创建数据库:

如上所述,不存在的,简单的:use dbname

查看数据库都那些:show dbs

删除数据库:

db.dropDatabase()

总结,操作数据库:[创建数据库]=>选择数据库(use)=>删除数据库(db.dropDatabase())

实验证明,如果没有use直接删库,不会删除任何数据库.

2.数据集合

创建:

db.createCollection(collectionName,createOption)

createOption是一个词典,里面可以有4个键值对:capped(固定大小的集合),autoIndexId(真假),size(集合最大值,字节为单位),max(最大文档值)

例如,创建一个autoIndexId,无固定大小,无各种最大值的集合:

db.createCollection("collection1",{autoIndexId:true})

查看当前数据库集合状态:

show collections

删除数据库集合:

db.collectionName.drop()

3.数据行(文档)

创建|插入:

db.collectionName.insert(document)

如果插入多个文档呢???

db.collectionName.insertMany([document1,document2,...,documentn])

这里面所谓的document,实际是Binary JSON,即BSON,你可以单纯的看成是JSON,或者是Python的字典.

例如,文档格式有个字段,name,age,gender,profession,addr,tel.插入一个这样的信息,则可以写:

db.stu.insert({"name":"ZhangSan","age":22,"gender":"shemale","profession":"chairmanOfSchool","addr":"HeiLongJiang","tel":17777777777"})

MongoDB到目前为止,除了插入文档是必要之外,创建各种库,集合都是可以省略的...

由于MongoDB相对宽松的规则,所以,还可以使用save去插入|更新一些信息,核心就是保存之前不存在的当然就是插入了,保存之前有的就是更新了.用法:

db.collectionName.save(document)

查看集合中的所有文档:

db.stu.find()

删除:

db.collectionName.remove(<query>,{justOne: <boolean>,writeConcern: <document>})

参数全部可选.

删除多个:

db.collectionName.deleteMany({ "name":"ZhangSan" })

更新:

db.collectionName.update(<query>,<update>,{upsert,multi,writeConcern})

其中,后三个都为可选.upsert==update insert.writeConcern(报错级别)

例如:

db.stu.update({"tel":17777777777"},{$set:{"tel":"1"}})

如果更改某一列(字段),

db.stu.update({"tel":17777777777"},{$set:{"tel":"1"}},{multi:true})

只更新第一条记录:

db.collectionName.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

全部更新:

db.collectionName.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

只添加第一条:

db.collectionName.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

全部添加加进去:

db.collectionName.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

全部更新:

db.collectionName.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

只更新第一条记录:

db.collectionName.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );

查询:

db.collectionName.find(<query>,projection)

其中,projection是可以包含或排除的一些返回结果,所以,例如:

db.collectionName.find({"addr":HeliLongJiang"},{addr:true})

OR:

db.collectionName.find({$or:[{key1:value1},{key2:value2}]})

返回指定字段:

db.CollectionName.find({},{addr:true})

4.数据字段(列)

5.数据索引(下标)

6.数据主键(标识作用的字段)

最新文章

  1. EL表达式判断
  2. 2016&quot;百度之星&quot; - 资格赛(Astar Round1)
  3. 关于history的Linux命令行
  4. [转]搭建高可用mongodb集群(二)—— 副本集
  5. SQL语句小总结
  6. php 快速排序
  7. Office PPT保持提示无法保存Gill Sans 等非TrueType字体
  8. c++ socket编程步骤
  9. mysq双主模式
  10. Android应用插件式开发解决方法[转]
  11. EventHandlerList z
  12. .net 安装remoting服务
  13. PHPCMSV9 更改后台地址
  14. 设计模式 ( 十六 ): Mediator中介者模式 -- 行为型
  15. sqlserver高版本到低版本迁移
  16. Python自学笔记——matplotlib极坐标.md
  17. Fix “Could not flush the DNS Resolver Cache: Function failed during execution” When Flushing DNS
  18. 快速搭建一个Spring Boot + MyBatis的开发框架
  19. 三、oneinstack
  20. 安装python的jupyter notebook工具

热门文章

  1. 软工之404 Note Found团队
  2. Spring MVC中如何解决POST请求中文乱码问题,GET的又如何处理呢
  3. 将http转为https访问
  4. python 装饰器和软件目录规范一
  5. mysql底层实现
  6. sourcetree的安装及使用
  7. java入门---循环结构 - for, while 及 do...while&amp;break&amp;continue
  8. springMVC补充——springMVC的表单标签
  9. texterea 水平居中
  10. Spring学习(二)-----eclipse新建spring项目