- 2.1 Diving into the MongoDB shell
- 2.1.1 Starting the shell
- 2.1.2 Databases, collections, and documents
- 2.1.3 Inserts and queries
- 2.1.4 updating documents
- 2.1.5 Deleting data
- 2.1.6 Other shell features
- 2.2 Creating and querying with indexes
- 2.2.1 Creating a large collection
- 2.3 Basic administration
- 2.3.1 Getting database information
- 2.3.2 How commands work
2.1 Diving into the MongoDB shell
mongodb的admin数据库账号密码:
mongo admin -uadmin -p123456
2.1.1 Starting the shell
mongo
2.1.2 Databases, collections, and documents
- MongoDB存储信息在documents中
- collections类似于表
- databases只是一个namespaces来区分collections
- 默认的database是test

databases和collections只有documents第一次被插入的时候才会创建
2.1.3 Inserts and queries


_id字段
可以认为该值为document’s 主键

pass a query predicate
传递一个query selector
传递多个字段在query predicate中
db.users.find({_id: ObjectId("62a69c886f65c86c70e88dff"),username: "smith"})

这里隐式的使用了and在字段中
db.users.find({ $and: [{_id: ObjectId("62a69c886f65c86c70e88dff")},{username: "smith"}]})

显示使用and
db.users.find({ $or: [{username: "smith"},{username: "jones"}]})
2.1.4 updating documents
db.users.update({username:"smith"}, {$set: {country: "Canada"}})

db.users.find({username:"smith"})

Replacement update
另一种方式更新文档是替换它而不是设置它
db.users.update({username:"smith"}, {country:"Canada"})db.users.find({country:"Canada"})

通过id可以看出,该语句是替换而不是添加
通过unset操作可以简单的移出一个字段
db.users.update({country: "Canada"}, {$set: {username:"smith"}})db.users.find({country:"Canada"})db.users.update({username:"smith"}, {$unset: {country: 1}})db.users.find({username:"smith"})

Updating Complex Data
db.users.update({username:"smith"}, {$set: {favorites: {cities: ["Chicago", "Cheyenne"],movies: ["Casablanca", "For a Few Dollars More", "The String"]}}})db.users.update({username: "jones"}, {$set: {favorites: {movies: ["Casablanca", "Rocky"]}}})
db.users.find().pretty()

pretty是为了打印的时候更漂亮,但是不用shell的话可以不加这个
db.users.find({"favorites.movies": "Casablanca"})
More Advanced Updates
相比于$set,你更应该使用$push or $addToSet
这些操作可以添加一个item到array
第二种是可以预防重复添加的
db.users.update({"favorites.movies": "Casablanca"}, {$addToSet: {"favorites.movies": "The Maltease Falcon"}}, false, true)
第三个参数,是判断如果文档不存在,是否应该插入文档
第四个参数,是表明是否多次更新。也就是如果匹配到了多个文档,是否都进行更新
2.1.5 Deleting data
db.users.remove({"favorites.cities": "Cheyenne"})
这个remove操作其实并没有完全删除collection;它只是从collection中删除了document
如果想要删除这个collection以及他的indexes,使用drop
db.users.drop()
2.1.6 Other shell features
2.2 Creating and querying with indexes
2.2.1 Creating a large collection
for(i = 0; i < 20000; i++) {db.numbers.save({num: i});}db.numbers.count()
Range Query
- $gt
- $lt
也可以两者结合使用: ```shell db.numbers.find({num: {“$gt”:500, “$lt”:520}})db.numbers.find({num: {"$gt":500}})
- $gte- $lte- $ne<a name="Qoyik"></a>## 2.2.2 Indexing and explainexplain描述了查询的路径和允许开发者通过确定查询使用了哪些索引来诊断慢操作```shelldb.numbers.find({num: {"$gt": 200}}).explain("executionStats")

为该集合创建一个索引:
db.numbers.createIndex({num:1})

在mongodb3中,createIndex方法代替了ensureIndex方法
{num:1}表示应该在numbers集合中的所有documents的num键上建立一个升序的索引
确认这个索引被创建,通过getIndexes
db.numbers.getIndexes()

该collection有两个indexes,第一个是id,这个是自动构建的;第二个是你创建的 num,这些索引在这些字段中叫做_id和num_1
2.3 Basic administration
2.3.1 Getting database information
show dbs

show collections

