Dec 22, 2012

随手集 信手写: MongoDB, PyMongo, BSON, django

From Evernote:

随手集 信手写: MongoDB, PyMongo, BSON, django

Clipped from: http://noteonx.blogspot.com/2011/06/mongodb-pymongo.html

mongodb 要点

参考:
不错的 Tutorial
m
inibook

MongoDB provides a simple http interface listing information of interest to administrators. This interface may be accessed at the port with numeric value 1000 more than the configured mongod port; the default port for the http interface is 28017. To access the http interface an administrator may, for example, point a browser to http://localhost:28017 if mongod is running with the default port on the local machine.


To recap, MongoDB is made up of databases which contain collections. A collection is
made up of documents. Each document is made up of fields. Collections can be indexed,
which improves lookup and sorting performance. Finally, when we get data from MongoDB we
do so through a cursor whose actual execution is delayed until necessary.
 
index 使用二叉搜索树
 
类比
collection     table
document     row
field          column
这只是类比,并不相同
Relational databases define columns at the table level whereas a document-oriented database defines its fields at the document level.
因此 document 能有自己独特的 fields
 
document-based database 是 schema-less 的。
 

配置和使用


配置:
创建一个目录,用于保存数据库
在 config-file 写入
dbpath=path-to-data-folder

mongod: server
$ mongod --config path-to-config-file
建议将这个命令存成可执行脚本

mongod 有两种方式可以连接,一种是 tcp (socket?) 连接,一种是 REST 方式
REST 方式需要再安装 REST interfaces. mongodb 安装包自带了一个简易的 REST 接口(用参数 --rest 开启),只适合admin使用.

第一种使用
mongo: shell client
pymongo python client

第二种需要再安装 REST interfaces。自带的简易服务时加参数 --rest,使用浏览器进行通信
http://localhost:28017/

django mongodb backend

关于mongodb 的思想,可见参见这个 tutorial
以 blog, comments 为例


pymongo

PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python

API Docs

pymongo 实际上由三部分组成
bson
pymongo
gridfs: location based storage?

from pymongo import Connection  connection = Connection()    db = connection.learn    collection = db.unicorns  for unicorn in collection.find():   print unicorn  

BSON

BSON is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. BSON is designed to be lightweight, traversable, and efficient.MongoDB uses BSON as the data storage and network transfer format for "documents".

BSON at first seems BLOB-like, but there exists an important difference: the Mongo database understands BSON internals. This means that MongoDB can "reach inside " BSON objects, even nested ones. Among other things, this allows MongoDB to build indexes and match objects against query expressions on both top-level and nested BSON keys.


MongoDB uses BSON documents for three things
  1. Data storage (user documents). These are the regular JSON-like objects that the database stores for us. These BSON documents are sent to the database via the INSERT operation. User documents have limitations on the "element name" space due to the usage of special characters in the JSON-like query language.
    1. A user document element name cannot begin with "$".
    2. A user document element name cannot have a "." in the name.
    3. The element name "_id" is reserved for use as a primary key id, but you can store anything that is unique in that field.
      The database expects that drivers will prevent users from creating documents that violate these constraints. 
  2. Query "Selector" Documents : Query documents (or selectors) are BSON documents that are used in QUERY, DELETE and UPDATE operations. They are used by these operations to match against documents. Selector objects have no limitations on the "element name" space, as they must be able to supply special "marker" elements, like "$where" and the special "command" operations. 
  3. "Modifier" Documents : Documents that contain 'modifier actions' that modify user documents in the case of an update (seeUpdating).




PyMongo 包含 bson 模块
bson_string = BSON.encode({"name": (1,2,3)}) # 将 python dict 编码为 bson
print BSON.decode(bson_string) # 将 bson 解码为 python dict

son:
A subclass of dict that maintains ordering of keys and provides a few extra niceties for dealing with SON. SON objects can be converted to and from BSON.

The mapping from Python types to BSON types is as follows:
Python TypeBSON TypeSupported Direction
Nonenullboth
boolbooleanboth
intnumber (int)both
floatnumber (real)both
stringstringpy -> bson
unicodestringboth
listarrayboth
dict / SONobjectboth
datetime.datetime [1] [2]dateboth
compiled reregexboth
bson.binary.Binarybinaryboth
bson.objectid.ObjectIdoidboth
bson.dbref.DBRefdbrefboth
Noneundefinedbson -> py
unicodecodebson -> py
bson.code.Codecodepy -> bson
unicodesymbolbson -> py


Http Interface - MongoDB

Source URL:http://www.mongodb.org/display/DOCS/Http+Interface

REST Interfaces

DrowsyDromedary (Ruby)

DrowsyDromedary is a REST layer for Mongo based on Ruby.

MongoDB Rest (Node.js)

MongoDB Rest is an alpha REST interface to MongoDB, which uses the MongoDB Node Native driver.

Mongodb Java REST server

Mongodb Java REST server based on Jetty.

HTTP Interfaces

Sleepy Mongoose (Python)

Sleepy Mongoose is a full featured HTTP interface for MongoDB.



 

 

0 comments: