3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)



1排序sort()

A降序排列

db.c4.find().sort().sort({age:-1});

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

B升序排列:

db.c4.find().sort({age:-1});

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

C排序分页

db.collectionName.find().sort({age:-1}).skip(20).limit(10);

 

eg:db.c4.find().sort({age:-1}).skip(20).limit(10);

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

2 $all,$in,$nin,$or,$nor,$exists

查询集合中的文档,$all主要用来查询数组中的包含关系,查询条件中只要有一个不包含就不返回。

$all(只有都包含才会显示)

> db.c5.insert({name:’zhangsan’,age:[1,2,3,4,5,9,10]});

WriteResult({ “nInserted” : 1 })

> db.c5.insert({name:’lisi’,age:[2,4,7,9,10]});

WriteResult({ “nInserted” : 1 })

> db.c5.insert({name:’wangwu’,age:[4,7,8,16,24]});

WriteResult({ “nInserted” : 1 })

> db.c5.find({age:{$all:[4,7,9]}});

{ “_id” : ObjectId(“543e20c5e2c90313035e7d06”), “name” : “lisi”, “age” : [ 2, 4, 7, 9, 10 ] }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

$in(只要有包含1个就显示)

> db.c5.find();

{ “_id” : ObjectId(“543e2098e2c90313035e7d05”), “name” : “zhangsan”, “age” : [ 1, 2, 3, 4, 5, 9, 10

] }

{ “_id” : ObjectId(“543e20c5e2c90313035e7d06”), “name” : “lisi”, “age” : [ 2, 4, 7, 9, 10 ] }

{ “_id” : ObjectId(“543e20ece2c90313035e7d07”), “name” : “wangwu”, “age” : [ 4, 7, 8, 16, 24 ] }

>db.c5.find({age:{$in:[1,2,3]}});

{ “_id” : ObjectId(“543e2098e2c90313035e7d05”), “name” : “zhangsan”, “age” : [ 1, 2, 3, 4, 5, 9, 10

] }

{ “_id” : ObjectId(“543e20c5e2c90313035e7d06”), “name” : “lisi”, “age” : [ 2, 4, 7, 9, 10 ] }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

$nin (表示不包含的才显示)

> db.c5.find();

{ “_id” : ObjectId(“543e2098e2c90313035e7d05”), “name” : “zhangsan”, “age” : [ 1, 2, 3, 4, 5, 9, 10

] }

{ “_id” : ObjectId(“543e20c5e2c90313035e7d06”), “name” : “lisi”, “age” : [ 2, 4, 7, 9, 10 ] }

{ “_id” : ObjectId(“543e20ece2c90313035e7d07”), “name” : “wangwu”, “age” : [ 4, 7, 8, 16, 24 ] }

>db.c5.find({age:{$nin:[1,2,3]}});

{ “_id” : ObjectId(“543e20ece2c90313035e7d07”), “name” : “wangwu”, “age” : [ 4, 7, 8, 16, 24 ] }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

$or (相当于或)

> db.c6.find({$or:[{name:”zhangsan”},{age:25}]})

{ “_id” : ObjectId(“543e2409e2c90313035e7d08”), “name” : “zhangsan”, “age” : 24 }

{ “_id” : ObjectId(“543e2432e2c90313035e7d09”), “name” : “lisi”, “age” : 25 }

{ “_id” : ObjectId(“543e2451e2c90313035e7d0a”), “name” : “wangwu”, “age” : 25 }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

$nor

查询集合中的文档,$nor,表示根据条件过滤掉某些数据,例如:查询name不是user2,age不是3的文档,命令为:

db.collectionName.find({$nor:[{name:”user1”},{age:3}]});

> db.c6.find();

{ “_id” : ObjectId(“543e2409e2c90313035e7d08”), “name” : “zhangsan”, “age” : 24 }

{ “_id” : ObjectId(“543e2432e2c90313035e7d09”), “name” : “lisi”, “age” : 25 }

{ “_id” : ObjectId(“543e2451e2c90313035e7d0a”), “name” : “wangwu”, “age” : 25 }

> db.c6.find({$or:[{name:”zhangsan”},{age:25}]})

{ “_id” : ObjectId(“543e2409e2c90313035e7d08”), “name” : “zhangsan”, “age” : 24 }

{ “_id” : ObjectId(“543e2432e2c90313035e7d09”), “name” : “lisi”, “age” : 25 }

{ “_id” : ObjectId(“543e2451e2c90313035e7d0a”), “name” : “wangwu”, “age” : 25 }

> db.c6.find({$nor:[{name:”zhangsan”},{age:25}]})

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

$exists

查询集合中的文档,$exists,用于查询集合中存在某个键的文档或不存在某个键的文档,例如查询customer集合中存在name键的所有文档,可以使用db.collectionName.find({name:{$exists:1}});

$exists:1表示真,指存在

$exists:0表示假,指不存在

分别查询包含name属性的和包含sex属性的数据

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

3游标

游标(相当于数据库的一个临时的存储区,它是存放在内存中的,查找某个集合返回记录,我可以把这些记录放在游标当中,然后我可以迭代游标)。

查询集合中的文档,类似关系型数据库,mongodb中也有游标的概念

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

4更新

> show dbs;

admin (empty)

local 0.078GB

test  0.078GB

> use toto

switched to db toto

> db

toto

> use test

switched to db test

> use toto

switched to db toto

> db

Toto

> db.c1.insert({name:"zhangsan",age:19});

WriteResult({ "nInserted" : 1 })

> db.c1.insert({name:"lis",age:25});

WriteResult({ "nInserted" : 1 })

> db.c1.insert({name:"wangwu",age:23});

WriteResult({ "nInserted" : 1 })

> db.c1.insert({name:"zhaoliu",age:27});

WriteResult({ "nInserted" : 1 })

> db.c1.insert({name:"tianqi",age:33});

WriteResult({ "nInserted" : 1 })

> db.c1.find();

{ "_id" : ObjectId("543e3223e2c90313035e7d0e"), "name" : "zhangsan", "age" : 19 }

{ "_id" : ObjectId("543e3244e2c90313035e7d0f"), "name" : "lis", "age" : 25 }

{ "_id" : ObjectId("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }

{ "_id" : ObjectId("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }

{ "_id" : ObjectId("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

更新操作:

db.collectionName.update({条件},{修改的值}),函数中4个参数的介绍

1. 设置更新的条件

2. 设置更新的内容的对象

3. 如果没有符合条件的记录,是否新增一条记录(取值为0/1)

4. 如果有多条记录符合,是否全部更新(取值0/1

db.collectionName.update({条件},{修改的值})

1. 更新的时候只更新到一条,且其它的key:value覆盖掉了

2. 我只更新到一条

> db.c1.find();

{ "_id" : ObjectId("543e3244e2c90313035e7d0f"), "age" : 36 }

{ "_id" : ObjectId("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }

{ "_id" : ObjectId("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }

{ "_id" : ObjectId("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }

> db.c1.update({age:36},{$set:{name:"zhangsan"}},0,1);

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.c1.find();

{ "_id" : ObjectId("543e3244e2c90313035e7d0f"), "age" : 36, "name" : "zhangsan" }

{ "_id" : ObjectId("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }

{ "_id" : ObjectId("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }

{ "_id" : ObjectId("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }

 3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

> db.c1.find();

{ "_id" : ObjectId("543e3244e2c90313035e7d0f"), "age" : 36, "name" : "zhangsan" }

{ "_id" : ObjectId("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }

{ "_id" : ObjectId("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }

{ "_id" : ObjectId("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }

> db.c1.update({age:24},{$set:{name:"tuzuoquan",age:24}},1,0);

WriteResult({

       "nMatched" : 0,

       "nUpserted" : 1,

       "nModified" : 0,

       "_id" : ObjectId("543e3644dd76b7dcaba68faf")

})

> db.c1.find();

{ "_id" : ObjectId("543e3244e2c90313035e7d0f"), "age" : 36, "name" : "zhangsan" }

{ "_id" : ObjectId("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }

{ "_id" : ObjectId("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }

{ "_id" : ObjectId("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }

{ "_id" : ObjectId("543e3644dd76b7dcaba68faf"), "age" : 24, "name" : "tuzuoquan" }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

5 inc

更新集合中的文档,使用$inc将集合中nameuser1age1,其它键不变,$inc表示使某个键值加减指定的数值。

> for(var i=1;i<=20;i++){ db.c2.insert({name:"tuzuoquan",age:24}); }

WriteResult({ "nInserted" : 1 })

> db.c2.find();

{ "_id" : ObjectId("543e386ee2c90313035e7d13"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e386ee2c90313035e7d14"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d15"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d16"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d17"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d18"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d19"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1a"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1b"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1c"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1d"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1e"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1f"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d20"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d21"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d22"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d23"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d24"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d25"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e38a5e2c90313035e7d26"), "name" : "tuzuoquan", "age" : 24 }

db.c2.update({name:"tuzuoquan"},{$inc:{age:1}},0,1);

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

6 $unset(删除某个键)

更新集合中的文档,$unset用来删除某个键,例如删除nameuser1的文档中address键,可以使用命令:

db.c1.update({name:”user”},{$unset:{age:1}},0,1);

>db.c2.update({name:"tuzuoquan"},{$unset:{age:1}},0,1);

WriteResult({ "nMatched" : 20, "nUpserted" : 0, "nModified" : 20 })

> db.c2.find();

{ "_id" : ObjectId("543e386ee2c90313035e7d13"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e386ee2c90313035e7d14"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d15"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d16"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d17"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d18"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d19"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1a"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1b"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1c"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1d"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1e"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d1f"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d20"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d21"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d22"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d23"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d24"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d25"), "name" : "tuzuoquan" }

{ "_id" : ObjectId("543e38a5e2c90313035e7d26"), "name" : "tuzuoquan" }

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

 

上一篇:Redis——Redis持久化


下一篇:《操作系统真象还原》——0.28 MBR、EBR、DBR和OBR各是什么