# 启动mongodb服务
sudo service mongod start

# 关闭mongodb服务
sudo service mongod stop

#进入mongodb shell
mongo

# 创建数据库 my_mongodb_01

>use my_mongodb_01
switched to db my_mongodb_01

# 查看数据库:
> show dbs             
admin          0.000GB 
config         0.000GB 
local          0.000GB 
my_mongodb_01  0.000GB

# 创建表teacher
db.createCollection(name, options)
>db.createCollection("teacher")

# 创建表students
>db.createCollection("students)

# 往students表中插入数据
db.students.insert({name:"张三",school:{name:"清华大学", city:"北京"}, age:19, gpa:3.97})
db.students.insert({name:"李四",school:{name:"北京大学", city:"北京"}, age:20, gpa:3.3})
db.students.insert({name:"王二",school:{name:"交通大学", city:"上海"}, age:22, gpa:3.68})
db.students.insert({name:"小牛",school:{name:"哈工大", city:"哈尔滨"}, age:21, gpa:3.50})
db.students.insert({name:"小马",school:{name:"交通大学", city:"西安"}, age:21, gpa:3.70})
db.students.insert({name:"小朱"})
# 查询students表中的数据
> db.students.find({name:"张三"})                                                 
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "na 
me" : "清华大学", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }                       
>      

> db.students.find({"school.name":"交通大学"})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学
", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
>

                                                                      
> db.students.find({"school.name":"交通大学", "school.city":"西安"})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
>
> db.students.find({$and:[{"school.name":"交通大学"},{"school.city":"西安"}]})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
>
> db.students.find({$or:[{"school.city":"北京"},{"school.city":"西安"}]})                       
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "name" : "清华大学  
", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }                                              
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学  
", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }                                               
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学  
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }                                               
>                                                                                           
> db.students.find({$or:[{"school.city":"北京"},{"school.name":"交通大学"}]})                       
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "name" : "清华大学    
", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }                                                
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学    
", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }                                                 
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学    
", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }                                                
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学    
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }                                                 
>                                                                                             
# 查找出年龄等于20的
> db.students.find({age:20})                                                                
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学  
", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }                                               
>                                                                                           

# 查找出年龄大于20的
> db.students.find({age:{$gt:20}})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学
", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大",
 "city" : "哈尔滨" }, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
>

# 查找出大于等于20的
> db.students.find({age:{$gte:20}})                                                             
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学      
", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }                                                   
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学      
", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }                                                  
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大",     
 "city" : "哈尔滨" }, "age" : 21, "gpa" : 3.5 }                                                    
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学      
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }                                                   
>                                                                                               
# 查找出年龄小于20的
> db.students.find({age:{$lt:20}})                                                           
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "name" : "清华大学   
", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }                                               
>                                                                                            

# 查找出年龄小于等于20
> db.students.find({age:{$lte:20}})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "name" : "清华大学
", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学
", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }
>

# 查找出年龄不等于20
> db.students.find({age:{$ne:20}})
{ "_id" : ObjectId("5bb9cdc4b8dd7ec496450222"), "name" : "小朱" }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "name" : "清华大学
", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学
", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大",
 "city" : "哈尔滨" }, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学
", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
>