MongoDB权威指南学习笔记

Chapter 1 Introduction

secondary indexes
range queries
sorting
aggregations
geospatial indexes

MongoDB is a document-oriented database, not a relational one.

  • scale out easier
  • represent complex hierarchical relationships with a single record
  • no predefined schemas

scaling a database = scale up(get a bigger machine) OR scale out(partition data across more machines - cheaper and more scalable but hard to administer)

MongoDB was designed to scale out. document-oriented data model makes it easier to split up data across multiple servers.

Features of MongoDB

  • Indexing: support generic secondary indexes, allowing a variety of fast queries, and provides unique, compound, geospatial and full-text indexing
  • Aggregation: support "aggregation pipeline" that allows building complex aggregation
  • Special collection types: MongoDB supports time-to-live collections such as sessions, fixed-size collections useful for holding recent data, such as logs
  • File storage: easy-to-use protocol for storing large files and file metadata

Chapter 2 Getting Started

Documents

  • a document is the basic unit of data for MongoDB and is roughly equivalent to a row in relational DB. document -> row

  • collection -> table with dynamic schema

  • a single instance of MongoDB host multiple independent databases, each of which can have its own collections

  • Every document has a special key, _id unique within a collection

  • MongoDB comes with a JavaScript shell, which is useful for the administration of MongoDB instances and data manipulation

  • MongoDB is type-sensitive and case-sensitive. Ex. {"foo" : 3} {"foo" : "3"} type-sensitive {"foo" : 3} {"Foo" : 3} case-sensitive

  • MongoDB cannot contain duplicate keys. Ex. illegal: {"greeting" : "hello", "greeting" : "hello, mongo"} duplicate keys

  • Key/value pairs in documents are ordered: Ex. {"x" : 1, "y" : 2}

Collections

a collection is a group of documents.
dynamic schemas means that the documents within a single collection can have any number of different "shapes"
{"greeting" : "Hello"} {"foo" : 5}

Subcollections: just a convention for organizing collections is to use namespaced subcollections separated by the . character. Ex. blog.posts blog.authors only for organizational purposes only

Databases

databases -> group collections -> group documents

a single instance of MongoDB can host several databases, each database has its own permissions, and each database is stored in separate files on disk.
good design: store all data for a single application in the same database

several reserved database names:
admin: this is the root database, in terms of authentication.
local: this database will never be replicated and can be used to store any collections that should be local to a single server
config: When MongoDB is being used in a sharded setup, it uses the config database to store information about the shards

https://www.youtube.com/watch?v=DX15WbKidXY

Download
Move the folder to $mv mongodb-osx-ssl-x86_64-4.0.5 usr/local/mongodb
create the default data directory $sudo mkdir -p /data/db
give permission to write to the directory before starting MongoDB: sudo chown codebind /data/db

default socket connections on port 27017
User mongod to start the MongoDB server and wait for a connection
mongod also sets up a very basic HTTP server that listens on a port 1000 higher than the main port, open a web browser and go to http://localhost:28017

Running the shell 【用shell来对mongodb 进行操作】

Use mongo to start the shell, the shell automatically attempts to connect to a MongoDB server on startup, make sure start mongodb before starting the shell
The shell is a full-featured JavaScript interpreter, capable of running JavaScript programs, we can use standard JavaScript libraries and call JavaScript functions

A MongoDB Client

db show the current database
use foobar: select another database
db.blog.insert(post): insert function adds a document to a collection
db.blog.find(): see the inserted post
findOne()
db.blog.update()
db.blog.remove(): called with no parameters, it removes all documents from a collcetion. It can also take a document specifying criteria for removal.

Data Types

posted @ 2018-12-20 21:11  AugusKong  阅读(329)  评论(0编辑  收藏  举报