MongoDB - Getting Started With Mongo Shell

有用链接: MongoDB 官方文档 MongoDB 权威指南

数据库操作

$ mongo
  MongoDB shell version: 2.2.2
  connecting to: test
> show dbs
  local   (empty)
  test    0.203125GB
> use test
  switched to db test
> show collections
  system.indexes
  test
> use blog
  switched to db blog
> show dbs
  local   (empty)
    test    0.203125GB
> db.users.save( { username:"HenryLee" } )
> show dbs
  blog    0.203125GB
  local   (empty)
  test    0.203125GB
> show collections
  system.indexes
  users
> db.users.find()
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }

从结果来看,MongoDB 的实现与我们之前的 SQL 操作习惯有很多差别。 SQL 的数据库,需要预先创建数据库,创建表,然后才可以插入数据。 MongoDB 不需要这些过程,在运行时,根据需要帮我们自动生成数据库和表。
英语原文:When first time you save the value into the defined collection (table), under selected database, MangoDB will create the value, collection and database automatically.

CRUD 操作

创建 Create
1. Insert

    db.collection.insert( < document > )
< document > : 文档
> db.users.insert( { username:"StormZhang" } )
> db.users.find()
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }
  { "_id" : ObjectId("50e55f8fa3747d523d3b66fd"), "username" : "StormZhang" }
2. Create with Save

db.collection.save( < document > )
> db.users.save( { username:"pipi" } )
> db.users.find()
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }
  { "_id" : ObjectId("50e55f8fa3747d523d3b66fd"), "username" : "StormZhang" }
  { "_id" : ObjectId("50e55fcaa3747d523d3b66fe"), "username" : "pipi" }
3. Create with Upsert(具体用法,参考 "更新 Update")

db.collection.update( < query >, < update >, { upsert: true } )

读取 Read
1. Find

    db.collection.find( < query >, < projection > )
< query >:SQL SELECT
< projection >: 返回数据集包含的字段(returns only those fields as specified)
> db.users.find( { username:"HenryLee" } )
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }
> db.users.find( { username:"HenryLee" }, { _id:0, username:1 }
  { "username" : "HenryLee" }
2. Find One

db.collection.findOne( < query >, < projection > )
> db.users.findOne()
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }
其他用法
db.users.find().sort( { username: 1 } )
db.users.find().limit( 2 )

更新 Update
1. Save

    db.collection.save( < document > )
  
> db.users.save( { _id:10 , username:"xiaogang" } )
> db.users.find()
  { "_id" : ObjectId("50e55533a3747d523d3b66fc"), "username" : "HenryLee" }
  { "_id" : ObjectId("50e55f8fa3747d523d3b66fd"), "username" : "StormZhang" }
  { "_id" : ObjectId("50e55fcaa3747d523d3b66fe"), "username" : "pipi" }
  { "_id" : 10, "username" : "xiaogang" }
> db.users.save( { _id:10 , username:"Amy" } )
> db.users.find( { _id:10 } )
  { "_id" : 10, "username" : "Amy" }
2. Update

db.collection.update( < query >, < update >, < options > )
< update >:SQL SET
< options >:特殊参数
> db.users.update( { _id: 10, username: 'Amy' }, { $set: { 'username': 'Ryan' } } )
> db.users.find( { _id:10 } )
  { "_id" : 10, "username" : "Ryan" }

特殊参数举例
1. multi: true 将更新所有符合条件的数据。

db.users.update( { username: 'henry' }, { $set: { username: 'Henry' } }, { multi: true } )
2. upsert: true 若符合条件的数据不存在,则插入一条。
db.users.update( { username: 'henry' }, { $set: { username: 'Henry' } }, { upsert: true } )

删除 Delete
1. Remove

    db.collection.remove( < query >, < justOne > )
  
删除所以符合条件的记录
db.users.remove( { username: 'Henry' } )
只删除第一条符合条件的记录
db.users.remove( { username: 'Henry' }, 1 )

以上例子,未考虑性能优化。

2013-01-03

rocket-wing