mongoDB学习笔记
- 基本概念
- 与关系型数据库(RDBMS)类比
- MongoDB Package Componets
- MongoDB CRUD Operations
- Mongo by Python
- mongo的客户端
- 扩展阅读:
基本概念
- MongoDB: an open-source document database
- ORM: Object Relational Mapping
- Documents: a record in MongoDB, similar to JSON objects
- Collections: MongoDB stores documents in collections, analogous to tables in relational databases
- _id field: unique, acts as a primary key
- JSON: JavaScript Object Notation
- BSON: a binary representation of JSON documents
- CRUD: create, read, update, and delete documents
- drivers: an application communicates with MongoDB by way of a client library, including shell, python, C, java, Node.js, etc.
:–
与关系型数据库(RDBMS)类比
Terminology
RDBMS | MongoDB |
---|---|
Database | Database |
Table | Collection |
Tuple/Row | Document |
column | Field |
Table Join | Embedded Documents |
Primary key | Primary Key (Default key _id provided by mongodb itself) |
Deatabase Server and Client
RDBMS | MongoDB |
---|---|
Mysqld/Oracle | mongod |
mysql/sqlplus | mongo |
MongoDB Package Componets
mongod
: the primary daemon process(守护进程) for the MongoDB systemmongos
: MongoDB Shard. Sharding means a method for distributing data across multiple machines.mongo
: an interactive JavaScript shell interface to MongoDBmongodump
: a utility for creating a binary export of the contents of a databasemongorestore
: writes data from a binary database dump created by mongodump to a MongoDB instance.bsondump
: converts BSON files into human-readable formats, including JSON.mongooplog
: a simple tool that polls operations from the replication oplog of a remote server, and applies them to the local server.mongoimport
: imports content from an Extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.mongoexport
: a utility that produces a JSON or CSV export of data stored in a MongoDB instance.mongostat
: provides a quick overview of the status of a currently running mongod or mongos instancemongotop
: provides a method to track the amount of time a MongoDB instance spends reading and writing datamongosniff
: provides a low-level operation tracing/sniffing view into database activity in real timemongoperf
: a utility to check disk I/O performance independently of MongoDBmongofiles
: makes it possible to manipulate files stored in your MongoDB instance in GridFS objects from the command line.
MongoDB CRUD Operations
Create Operations
add new documents to a collection
db.collection.insert()
db.collection.insertOne()
db.collection.insertMany()
Read Operations
read documents from a collection, i.e. queries a collection for documents
db.collection.find()
Query Filter | Description |
---|---|
{} |
所有documents |
{a: 10} |
a filed值为10的documents |
{a: {$gt: 10}} |
a值大于10的documents。更多见comparison query operators文档。如$eq , $gt , $gte , lt , lte , etc. |
{a: 10, b: "hello"} |
a值为10且b值为“hello”的documents |
{a: {$in: [10,"hello"]}} |
a值为10或为“hello”的documents |
{$or: [{a: 10}, {b: "hello"}]} |
a值为10或b值为“hello”的documents |
{a: {$all: [10, "hello"]}} |
a是一个含有10和“hello”的数组的documents |
{"a.b": 10} |
a是一个embedded document,且b值为10的documents |
{a: /^m/} or {a: {$regex: "^m"}} |
a开头字母为m的documents |
{a: {$type: 2}} |
a为字符串类型的documents。注此处2为BSON type number。详情见文档 |
更多见文档:Query Documents
Update Operations
modify existing documents in a collection
db.collection.update()
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
Delete Operations
remove documents from a collection
db.collection.remove()
db.collection.deleteOne()
db.collection.deleteMany()
Mongo by Python
- 安装pymongo
$ pip install pymongo
- 导入pymongo
from pymongo import MongoClient
- 创建一个connection
client = MongoClient() #默认是localhost,端口27017
client = MongoClient("mongodb://mongodb0.example.net:27017")
mongo的客户端
- mongochef
- robomongo