MongDB篇,第一章:数据库知识2

MongDB    数据库知识2

非关系型数据库和关系型数据库的比较
1. 不是以关系模型构建数据结构,结构比较自由
不保证数据的一致性
2. 非关系型数据库弥补了关系型数据库的一些不足,能 够在处理高并发,海量数据上体现优势
3. 非关系型数据库的个性化使其可以在节省空间,提高 效率方面发挥作用

Mongodb 文档型非关系型数据库

创建数据库 : use databaseName
删除数据库 : db.dropDatabase()

创建集合 : db.createCollection()
db.collectionName.insert()
删除集合 : db.collectionName.drop()
重命名集合 :db.collectionName.renameCollection()

查看数据库 : show dbs
查看集合 : show collections
show tables

插入文档 : insert() save()
==================================================

(1)修改集合名称:

db.collection_name.renameCollection('newname')

eg.    db.class1.renameCollection('class0')  #将class1改为class0

 

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
std     0.000GB
stu     0.000GB
> use stu
switched to db stu
> show tables;
class1
class3
> show collections
class1
class3
> db.class1.renameCollection('class0')
{ "ok" : 1 }
> show collections
class0
class3
> db.class0
stu.class0
> db.class0.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
> db.getCollection('class0').insert({name:'test_class0', age:21,sex:'w'})
WriteResult({ "nInserted" : 1 })
> db.class0.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "name" : "test_class0", "age" : 21, "sex" : "w" }
> 
View Code

 

 (2)查找(字段)

select ....from tablename  where ....

db.collection_name.find()    #返回所有字段   ----->  select * from tablename

find(query,field)

功能: 查找所有符合帅选要求的文档;

参数:  query:  筛选条件            相当于where子句

              field :  展示的域             0表示不展示该域,  1表示展示该域;

返回值:返回对应符合要求的文档;

field : 选择要展示的域;

1,以键值对的形式给每个域赋值0,1 表示是否要显示该域;

2,如果给域设置为0,则其他域自动为1, 如果给某个域设置为1, 则其他的自动设置为0,两者不能混用;

3,_id 比较特殊,默认为1,如果不想显示则设置为0. _id 为0 时,其他的是可以为1的;

4,如果不写该参数,则表示显示所有的域;

query: 查找

1,以键值的方式确定查找条件;

2,如果不写这个参数则表示查找所有文档;

eg。  db.class0.find({age:22},{_id:0})  =====》 select name,age,sex from class0 where age=22

 

> db.class0.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0,sex:0})
{ "name" : "xiaoming", "age" : 20 }
{ "name" : "ttttt", "age" : 20 }
{ "name" : "ttttt", "age" : 20 }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21 }
> 
> db.class0.find({},{age:1})
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "age" : 20 }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "age" : 20 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "age" : 20 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "age" : 11 }
{ "_id" : 1, "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "age" : 21 }
> db.class0.find({},{name:1,age:1})
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20 }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "name" : "test_class0", "age" : 21 }
> 
View Code

 

> db.class0.find({},{name:1,age:0})
Error: error: {
    "ok" : 0,
    "errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
    "code" : 2,
    "codeName" : "BadValue"
}
> db.class0.find({},{name:1})
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb" }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx" }
{ "_id" : 1, "name" : "aiai" }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "name" : "test_class0" }
> db.class0.find({},{name:0})
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "age" : 11 }
{ "_id" : 1, "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0,name:0})
{ "age" : 20, "sex" : "m" }
{ "age" : 20, "sex" : "m" }
{ "age" : 20, "sex" : "m" }
{ "age" : 18 }
{ "age" : 21 }
{ "age" : 11 }
{ "age" : 19 }
{ "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0,name:1})
{ "name" : "xiaoming" }
{ "name" : "ttttt" }
{ "name" : "ttttt" }
{ "name" : "aaa" }
{ "name" : "bbb" }
{ "name" : "xxxxx" }
{ "name" : "aiai" }
{ "name" : "test_class0" }
> db.class0.find({age:22},{_id:0})
> db.class0.find({age:20},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> 
View Code

 

 

db.collection_name.findOne()  #返回查找字段的第一条

功能:查找符合条件的第一条文档;

 参数: 同find

返回值:返回查找到的文档;

> db.class0.find({age:20},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.findOne()
{
    "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"),
    "name" : "xiaoming",
    "age" : 20,
    "sex" : "m"
}
> db.class0.findOne({age:20},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
> 
View Code

 

 

query 的更多用法:

比较操作符:

$eq         等于

> db.class0.find({age:{$eq:20}},{_id:0})   等价于> db.class0.find({age:20},{_id:0})

比较预算符也是用键值的方式,给比较运算符设置一个值,表示相应的关系;

$lt         小于

> db.class0.find({age:{$lt:20}},{_id:0})

在MongoDB中字符串也可以比较大小;

$lte      小于等于

> db.class0.find({age:{$lte:19}},{_id:0})

$gt        大于

> db.class0.find({age:{$gt:19}},{_id:0})

$gte       大于等于

> db.class0.find({age:{$gte:19}},{_id:0})

$ne     不等于

> db.class0.find({age:{$ne:20}},{_id:0})

如果某个文档没有age这个域也会显示不等于20

$in     包含 

> db.class0.find({age:{$in:[19,21]}},{_id:0})

找出年龄为19,21的文档;

$nin  不包含

> db.class0.find({age:{$nin:[19,21]}},{_id:0})

> db.class0.find({age:{$eq:20}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({age:20},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({age:{$lt:20}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({name:{$lt:'ttttt'}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$lte:20}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$lte:19}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$gt:19}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$gte:19}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$ne:20}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$in:[19,21]}},{_id:0})
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$nin:[19,21]}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
> 
View Code

 

逻辑操作符:

$and   逻辑与

 *  默认在query中逗号分隔的多个条件即为逻辑与的关系;

> db.class0.find({$and:[{age:20},{age:20}]},{_id:0})  等价于> db.class0.find({age:20,sex:'m'},{_id:0})

> db.class0.find({age:{$gt:18,$lt:21}},{_id:0})

 

$or  逻辑或

> db.class0.find({$or:[{age:20},{sex:'w'}]},{_id:0})  

> db.class0.find({$or:[{age:{$lt:20}},{age:{$gt:25}}]},{_id:0})  #小于20或者大于25

 

$not   逻辑非

> db.class0.find({age:{$not:{$eq:20}}},{_id:0})

 

$nor    既不也不(集合中的条件都不满足的文档)

 

> db.class0.find({age:{$eq:20}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({age:20},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({age:{$lt:20}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({name:{$lt:'ttttt'}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$lte:20}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$lte:19}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$gt:19}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$gte:19}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$ne:20}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$in:[19,21]}},{_id:0})
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$nin:[19,21]}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
> db.class0.find()
{ "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"), "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"), "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{ "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"), "name" : "xxxxx", "age" : 11 }
{ "_id" : 1, "name" : "aiai", "age" : 19 }
{ "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"), "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:22,sex:'w'},{_id:0})
> db.class0.find({age:21,sex:'w'},{_id:0})
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:20,sex:'w'},{_id:0})
> db.class0.find({age:20,sex:'m'},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({$and:[{age:20},{age:20}]},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find(age:{$gt:18,$lt:21},{_id:0})
2018-07-12T22:40:58.974+0800 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:18
> db.class0.find(age:{$gt:18,$lt:21}},{_id:0})
2018-07-12T22:41:12.104+0800 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:18
> db.class0.find({age:{$gt:18,$lt:21}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aiai", "age" : 19 }
>  db.class0.find({$or:{age:20},{sex:'w'}},{_id:0})
2018-07-12T22:46:11.156+0800 E QUERY    [js] SyntaxError: invalid property id @(shell):1:29
> db.class0.find({$or:{age:20},{sex:'w'}},{_id:0})
2018-07-12T22:46:33.420+0800 E QUERY    [js] SyntaxError: invalid property id @(shell):1:29
> db.class0.find({$or:[{age:20},{sex:'w'}]},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({$or:[{age:{$lt:20}},{age:{$gt:25}}]},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({$or:[{age:{$lt:19}},{age:{$gt:20}}]},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({$or:[{age:{$lt:18}},{age:{$gt:20}}]},{_id:0})
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$not:{$eq:20}}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> 
View Code

 $nor

> db.class0.find({$nor:[{age:20},{sex:'w'}]},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({$nor:[{age:20},{name:'xxxxx'}]},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> 
View Code

 

 

数组查找

  查看数据中含有game的文档

> db.class1.find({hobby:'Game'},{_id:0})

  $all    查看数组中包含多项文档;

> db.class1.find({hobby:{$all:["pingpong", "Game"]}},{_id:0})

  $size  查找数组中项数为指定个数的文档;

> db.class1.find({hobby:{$size:2}},{_id:0})

  $slice  (第二个参数)显示数组当中的前几项

 

> db.class1.find({},{_id:0,hobby:{$slice:1}})

> db.class1.find({},{_id:0,hobby:{$slice:2}})

> db.class1.find({},{_id:0,hobby:{$slice:-1}})

> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
> db.class1.find({hobby:'Game'},{_id:0})
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
> db.class1.find({hobby:{$all["pingpong", "Game"]}},{_id:0})
2018-07-14T19:11:40.013+0800 E QUERY    [js] SyntaxError: missing : after property id @(shell):1:27
> db.class1.find({hobby:{$all:["pingpong", "Game"]}},{_id:0})
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
> db.class1.insert({name:'aiai',age:18,hobby:['pipi','qinqin','baobao']})
WriteResult({ "nInserted" : 1 })
> db.class1.find()
{ "_id" : ObjectId("5b49d7eea1e0b5db3cff6a73"), "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "_id" : ObjectId("5b49d87ea1e0b5db3cff6a74"), "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "_id" : ObjectId("5b49d87ea1e0b5db3cff6a75"), "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "_id" : ObjectId("5b49d8cfa1e0b5db3cff6a76"), "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
{ "_id" : ObjectId("5b49db79a1e0b5db3cff6a77"), "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
> db.class1.find({hobby:{$size:2}},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
> 
> db.class1.find({},{_id:0,hobby:{$slice:1}})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi" ] }
> db.class1.find({},{_id:0,hobby:{$slice:2}})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin" ] }
> db.class1.find({},{_id:0,hobby:{$slice:-1}})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "baobao" ] }
> 
View Code

 

其他查找方法:

  $exists   判断一个域是否存在

> db.class0.find({sex:{$exists:true}},{_id:0})   查找存在

> db.class0.find({sex:{$exists:false}},{_id:0})   查找不存在

  $mod     通过整除余数进行查找

> db.class0.find({age:{$mod:[3,0]}},{_id:0})

> db.class0.find({age:{$mod:[3,1]}},{_id:0})

> db.class0.find({age:{$mod:[3,2]}},{_id:0})  

  $type  查找值为指定数据类型的文档

> db.class0.find({age:{$type:1}},{_id:0})

> db.class0.find({name:{$type:2}},{_id:0})

> db.class0.find({},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({sex:{$exists:true}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({sex:{$exists:false}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$mod:[3,0]}},{_id:0})
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({age:{$mod:[3,1]}},{_id:0})
{ "name" : "aiai", "age" : 19 }
> db.class0.find({age:{$mod:[3,2]}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "xxxxx", "age" : 11 }
> db.class0.find({age:{$type:1}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({name:{$type:2}},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({name:{$type:1}},{_id:0})
> db.class1.find({hobby:{$type:2}},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
> ^C
bye
View Code

 

和查询结果相关的几个函数:

  distinct()   功能:查看一个集合中某个域的值所覆盖的范围;

 

  pretty()  功能:将查询结果格式化显示;

 

  limit(n)  功能: 查询结果显示前n个文档;

 

> db.class0.find({},{_id:0}).limit(3)

  skip(n)  功能:跳过前n条文档进行显示;

 

> db.class0.find({},{_id:0}).skip(2)

  count()  功能: 对查询结果进行计数;

 

> db.class0.find({},{_id:0}).count()

> db.class0.find({age:20},{_id:0}).count()

 

sort())

功能:按照指定的字段进行排序;

参数 : 键值对, 某个域按照升序排序则值为1, 降序则值为-1

> db.class0.find({},{_id:0}).sort({age:1})    #升序排序

> db.class0.find({},{_id:0}).sort({age:-1})  # 降序排序

  

复合排序:当第一个排序项相同的时候,使用第二个排序

> db.class0.find({},{_id:0}).sort({age:1,name:-1})

> db.class0.find({},{_id:0}).sort({age:1,name:1})

> db.class0.find({},{_id:0}).sort({age:1,name:1}).count()

> use stu
switched to db stu
> 
> show collections
class0
class1
class3
> db.class0.distinct('age')
[ 20, 18, 21, 11, 19 ]
> db.class0.distinct('name')
[ "xiaoming", "ttttt", "aaa", "bbb", "xxxxx", "aiai", "test_class0" ]
> db.class1.distinct(hobby)
2018-07-14T19:56:40.323+0800 E QUERY    [js] ReferenceError: hobby is not defined :
@(shell):1:1
> db.class1.distinct('hobby')
[
    "draw",
    "song",
    "run",
    "speek",
    "football",
    "gongfu",
    "Game",
    "pingpong",
    "baobao",
    "pipi",
    "qinqin"
]
> db.class0.find().pretty()
{
    "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"),
    "name" : "xiaoming",
    "age" : 20,
    "sex" : "m"
}
{
    "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"),
    "name" : "ttttt",
    "age" : 20,
    "sex" : "m"
}
{
    "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"),
    "name" : "ttttt",
    "age" : 20,
    "sex" : "m"
}
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6f"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a70"), "name" : "bbb", "age" : 21 }
{
    "_id" : ObjectId("5b44c9eaa1e0b5db3cff6a71"),
    "name" : "xxxxx",
    "age" : 11
}
{ "_id" : 1, "name" : "aiai", "age" : 19 }
{
    "_id" : ObjectId("5b474f89a1e0b5db3cff6a72"),
    "name" : "test_class0",
    "age" : 21,
    "sex" : "w"
}
> db.class0.find().pretty().limit(3)
{
    "_id" : ObjectId("5b44c691a1e0b5db3cff6a6c"),
    "name" : "xiaoming",
    "age" : 20,
    "sex" : "m"
}
{
    "_id" : ObjectId("5b44c6d5a1e0b5db3cff6a6d"),
    "name" : "ttttt",
    "age" : 20,
    "sex" : "m"
}
{
    "_id" : ObjectId("5b44c7b7a1e0b5db3cff6a6e"),
    "name" : "ttttt",
    "age" : 20,
    "sex" : "m"
}
> db.class0.find({},{_id:0}).limit(3)
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
> db.class0.find({},{_id:0}).skip(2)
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0}).count()
8
> 
> db.class0.find({age:20},{_id:0}).count()
3
> db.class0.find({},{_id:0}).sort([])
Error: error: {
    "ok" : 0,
    "errmsg" : "Failed to parse: sort: []. 'sort' field must be of BSON type object.",
    "code" : 9,
    "codeName" : "FailedToParse"
}
> db.class0.find({},{_id:0}).sort({})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aaa", "age" : 18 }
{ "name" : "bbb", "age" : 21 }
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0}).sort({age:1})
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aaa", "age" : 18 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0}).sort({age:-1})
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "aiai", "age" : 19 }
{ "name" : "aaa", "age" : 18 }
{ "name" : "xxxxx", "age" : 11 }
> db.class0.find({},{_id:0}).sort({age:1,name:-1})
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aaa", "age" : 18 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
{ "name" : "bbb", "age" : 21 }
> db.class0.find({},{_id:0}).sort({age:1,name:1})
{ "name" : "xxxxx", "age" : 11 }
{ "name" : "aaa", "age" : 18 }
{ "name" : "aiai", "age" : 19 }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "bbb", "age" : 21 }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.find({},{_id:0}).sort({age:1,name:1}).count()
8
> 
View Code

 

 

(3) 删除文档

db.collection_name.remove(query,justOne)

功能:删除文档;

参数:query: 定位要删除的文档数据   类似sql

                    用法和查找相同

     justOne :  给bool值  ,默认false ,表示删除所有符合条件的数据文档;如果赋值为true, 则表示只删除第一条符合条件的文档;

 

> db.class0.remove({sex:{$exists:false}})  #删除不包含sex域的文档

> db.class0.remove({age:20},true)   #删除第一条

  删除集合中所有的文档

  db.collection_name.remove({})

> db.class0.remove({})
WriteResult({ "nRemoved" : 3 })

> db.class0.remove({sex:{$exists:false}})
WriteResult({ "nRemoved" : 4 })
> db.class0.find({},{_id:0}}
2018-07-14T20:23:10.214+0800 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:25
> db.class0.find({},{_id:0}})
2018-07-14T20:23:16.745+0800 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:25
> db.class0.find({},{_id:0})
{ "name" : "xiaoming", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.remove({age:20},true)
WriteResult({ "nRemoved" : 1 })
> db.class0.find({},{_id:0})
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "ttttt", "age" : 20, "sex" : "m" }
{ "name" : "test_class0", "age" : 21, "sex" : "w" }
> db.class0.remove({})
WriteResult({ "nRemoved" : 3 })
> 
View Code

 

  练习:

1. 创建数据库 名字 grade
2. 数据库中创建集合名字 class
3. 集合中插入若干文档,文档格式如下
  {name:'zhang',age:10,sex:'m',hobby:['a','b']}
    hobby 可以是 : draw sing  dance  basketball  football  pingpong  computer
4. 查找练习
查看班级所有人信息 find()
查看班级中年龄为8岁的学生信息  find({age:8})
查看年龄大于10岁的学生信息 find({age:{$gt:10}})
查看年龄在8-11岁之间的学生信息  find({age:{$gt:8,$11}})
找到年龄为9岁且为男生的学生 find({age:9,sex:'m'})
找到年龄小于7岁或者大于11岁的学生  find({$or:[{age:{$lt7}},{age:{$gt:8}}]})
找到年龄是8岁或者11岁的学生  find({$or[{age:8},{age:11}]})   ;  find({age:{$in[8,11]}})
找到有两项兴趣爱好的学生 find({hobby:{$size:2}})
找到兴趣爱好中有draw的学生  find({hobby:'draw'})
找到喜欢画画和跳舞的学生  find({hobby:{$all:['draw','dance']}})
统计兴趣有三项的学生人数  find({hobby:{$size:3}}).count()
找出本班年龄第二大的学生  find().sort({age:-1}).skip(1).limit(1)
查看学生的兴趣范围   db.class.distinct()
找到班级中年龄最大的三个学生 
find().sort({age:-1}).limit(3)
删除所有年龄小于6岁的学生 remove({age:{$lt:4},sex:'m')
5. 删除所有年龄大于12或者小于6岁的学生
View Code

 

 

(4)更新文档

    update   tablename set  ..... where......

 

db.collection_name.update(query, update, upsert, multi)

功能:更新一个文档数据

参数: query : 定位要更新的数据    , 相当于where   ,用法同查找

     update :  将数据更新成什么,  相当于set     需要配合修改器操作符来使用;

      upsert :  bool值:  默认为false  表示当定位的文档不存在 则无法修改。如果设置为true,表示如果定位不存在,则插入这条文档;

     multi  :   bool 值;  默认为false   如果query 匹配的文档有多条则只修改第一条。如果设置为true, 则修改所有匹配到的文档;

> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 11, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
> db.class1.update({name:'mimi'},{$set:{age:19}},false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
> db.class1.update({name:'timi'},{$set:{age:16}},true)
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("5b4b2e13c24e7066f995d162")
})
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 16 }
> db.class1.update({name:'timi'},{$set:{age:20}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 17, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> db.class1.update({name:'xixi'},{$set:{age:19}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 19, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> db.class1.update({age:19},{$set:{age:22}},true,false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> db.class1.update({name:'xixi'},{$set:{age:19}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 19, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 19, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> db.class1.update({age:19},{$set:{age:22}},true,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> 
View Code

 

 

update

修改器: 将数据修改成什么;

 $set     修改一个值;同时:如果域不存在就增加域;

> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 20 }
> 
> db.class1.update({name:'timi'},{$set:{age:21, sex:'m'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 21, "sex" : "m" }
> 
View Code

 

 

  $unset     删除一个域

 $rename  修改一个域名称; 如果都修改一个域名称则第4个参数为true;

> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 21, "sex" : "m" }
> db.class1.update({name:'timi'},{$unset:{sex:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 21 }



> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 21, "sex" : "m" }
> db.class1.update({name:'timi'},{$rename:{sex:'gender'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({},{_id:0})
{ "name" : "xiaohong", "age" : 12, "hobby" : [ "song", "draw" ] }
{ "name" : "lili", "age" : 15, "hobby" : [ "speek", "run" ] }
{ "name" : "xixi", "age" : 22, "hobby" : [ "football", "gongfu" ] }
{ "name" : "mimi", "age" : 22, "hobby" : [ "pingpong", "Game" ] }
{ "name" : "aiai", "age" : 18, "hobby" : [ "pipi", "qinqin", "baobao" ] }
{ "name" : "timi", "age" : 21, "gender" : "m" }
> 
View Code

 

posted on 2018-07-12 19:53  微子天明  阅读(231)  评论(0编辑  收藏  举报

导航