MongoDB (mongodb以及pymongo简介)

图片描述


参考文章

http://www.cnblogs.com/Joans/p/3729914.html //命令帮助系统
    http://segmentfault.com/a/1190000002694268?_ea=184402 //基础知识
    http://api.mongodb.org/python/current/tutorial.html //pymongo tutorial

MongoDB基础知识

  • 文档html

    是MongoDB的核心概念;
       和关系型数据库中的行十分相似;
       是键值对的有序集;
       区分大小写。
  • 集合Collectionpython

    是文档的集合;
       若是文档可以表示为行,那么集合很显然就至关于一张表了;
       动态模型:集合中的文档能够是各式各样的;
       子集合:.一般用此标记来访问;
  • 基本的数据类型正则表达式

    null: 用于表示空值或者不存在的字段:
           {    
               "x": null    ​
           }
       bool: 布尔型    
           {    
               "x": true,    ​
               "y": false    
           }
       String: 字符串    
           {    ​
               "x": 
               "这是一个字符串"    
           }
       number: 数值    
           {    
               "pi": 3.14,    
               "x": 3,    
               "ni": NumberInt("3"),
               "nl": NumberLong("3")    
           }
       String: 字符串    
           {    
               "x": "这是一个字符串"    
           }
       date: 日期
           {    
               "x": new Date()    
           }
       regular expression: 正则表达式 
           {    
               "x": /foobar/i    
           }    
       array:数组    
           {    
               "a": ["x", "y", "z"]    
           }
    
       ​object id 形式以及生成方式:

    图片描述

    object id 提供了秒级别的惟一性,机器码是机器主机名的散列值(hash);
           同一个进程在同一秒内产生的Objectid是不一样的,每进程每秒最多拥有2563Objectid

JavaScript Shell

  • 命令mongodb

    mongod  --dbpath /Users/…/*开启服务器后面是配置数据库存放的位置*/
       mongo/*开启客户端 链接服务器*/
       db
       use dbName/*switched to db dbName*/
       db.createCollection(“collectionName”)
       db.collectionName.insert(post)/*post is a file’s name*/
       db.blog.find()  and db.blog.findOne()/*These are used to list the documents in  the collection that you want to index*/
       db.collectionName.update({“title”: “这是一篇文章”}, post)
       db.collectionName.remove({“title”: “这是一篇文章”})
    
       mongo mongo-db.phoenix.com:30000/ahaInsight/*链接指定的mongod*/
       mongo --nodb/*启动时能够让mongo shell不链接任何的mongod*/
    
       conn = new Mongo(“host.name:30000”)
       db = conn.getDB(“dbname”)

Mongo链接数据库

获取客户端:client = MongoClient()

    获取数据库:db = client.stack

    ​获取collection: col = db.col
相关文章
相关标签/搜索