基本命令

mongo

启动 Mongo

show dbs

显示数据库

use mydb

使用名为"­myd­b"的数据库

db

显示选择数据库

help

取得帮助

show collec­tions

从数据库中显示集合

Finds - db.col­lection

.find() Displays documents from "­col­lec­tio­n" (first 10)
it Type "­it" to see more documents after the prev command
.find(query[,fields]) Find all documents by condit­ions, with optional fields selection
.findO­ne(­query) Find one document by conditions
.find(query).limit(n:number) Find n document by conditions
.find(­).p­retty() Format results in Mongo Shell
.find().sort(key:1|-1) Sort by key in ascending (1) or descending (-1) order
.find(­).s­kip(5) Skip 5 documents (similar to offset)

Modify - db.col­lection

.insert(document(s) [,options]) Insert a new document or multiple docume­nts(if provided an array of documents) in the collec­tion. Options: writeC­oncern, ordered
.inser­tOn­e(d­ocu­ment, [,opti­ons]) Same as insert from 3.2
.inser­tMa­ny(­array of documents, [,opti­ons]) Same as in sert from 3.2
.updat­e(q­uery, update [,opti­ons]) Update the documents matched by the query. See update operators. Options: upsert­(insert if no match), multi(aply to multiple elements), writeC­oncern
.remov­e(query [,opti­ons]) Remote some documents from a collec­tion. {} for all. options: {justOne, writeC­oncern}
.delet­eOn­e(q­uery), delete­Man­y(q­uery) Similar to remove

查询

_id

通过ID查询

key: value

通过键值组合搜索

{ subkey: value }

使用子文档搜索

$in : [ e1, e2, .. ]

Search IN Array

查询选择器:元素

$exist­s:B­oolean

Check if property exists or not

$mod:[D,R]

Checks if a property divided by D has the specified R

$type:Int

检查属性是否是指定的类型

D = Divisor
R = Reminder
Bson Types
http:/­/do­cs.m­on­god­b.o­rg/­man­ual­/re­fer­enc­e/g­los­sar­y/#­ter­m-bson

查询选择器:比较

$gt­:Val

Greater then Val

$gte­:Val

Greater then equals Val

$lt­:Val

Lower then Val

$lte­:Val

Lower then equals Val

$all­:­Array

All Array elements are included in field array value

$in­:Array

Elements with values contained in Array

$nin­:­Array

Elements with values Not contained in Array

$ne­:Val

Not equal

查询选择器:逻辑

$and:AOE

AND operation between all AOE expres­sions

$nor:AOE

all AOE expres­sions must fail

$not:Expr

Negate a SubDoc­ument (doesn't work with $regex)

$or:AOE

OR operation between all AOE expres­sions

Expr = Expression
AOE = Array Of Expres­sions
( eg: Expression = price : 20 )