Sequoiadb 测试体验系列之三 – shell 控制台再探


上一篇中尝试了一下SequoiaDB的 shell控制台的使用,研究了一下在控制台中创建、删除集合空间和集合,插入查找记录等一系列基础的操作,这一篇中来尝试一下控制台中匹配符、更新符和聚集符的应用。


进入控制台

admin@sdbserver1:/opt/sequoiadb/bin/sdb
/opt/sequoiadb/.sequoiadb_shell_history
Welcome to SequoiaDB shell!
help() for help, Ctrl+c or quit to exit


新建一个默认SequoiaDB实例,默认连接到11810端口,并赋给新建的变量db

> db = new Sdb()
localhost:11810
Takes 0.3325s.



查看现有的集合空间,有一个建好的名叫“foo”的集合空间
> db.listCollectionSpaces()
{
"Name": "foo"
}
Return 1 row(s).
Takes 0.1714s.



查看现有的集合,有一个建好的名叫“bar”的集合在集合空间“foo”中
> db.listCollections()
{
"Name": "foo.bar"
}
Return 1 row(s).
Takes 0.1762s.



将集合空间foo赋给变量cs
> cs = db.getCS("foo")
localhost:11810.foo
Takes 0.1688s.



将集合空间bar赋给变量cl
> cl = cs.getCL("bar")
localhost:11810.foo.bar
Takes 0.1205s.



查看集合中的记录,返回空
> cl.find()
Return 0 row(s).
Takes 0.2083s.


插入3条格式1的记录

> for(vari=0;i<3;i++){cl.insert({"a":i,"b":i})}
Takes 0.7291s.

插入3条格式1的记录

> for(vari=3;i<6;i++){cl.insert({"a":i,"c":i})}
Takes 0.2165s.


>查看集合中的记录,返回刚刚插入的两种格式的6条记录
> cl.find()
{
"_id": {
"$oid":"53c36441d0d8286f2000001b"
},
"a": 0,
"b": 0
}
{
"_id": {
"$oid":"53c36441d0d8286f2000001c"
},
"a": 1,
"b": 1
}
{
"_id": {
"$oid":"53c36441d0d8286f2000001d"
},
"a": 2,
"b": 2
}
{
"_id": {
"$oid":"53c36453d0d8286f2000001e"
},
"a": 3,
"c": 3
}
{
"_id": {
"$oid":"53c36453d0d8286f2000001f"
},
"a": 4,
"c": 4
}
{
"_id": {
"$oid":"53c36453d0d8286f20000020"
},
"a": 5,
"c": 5
}
Return 6 row(s).
Takes 0.6080s.


利用$exists匹配符,删除所有格式1(包含字段"b")的三条记录

$exists匹配符,选择集合中是否存在指定“<字段名>"的记录。"0"表示选择不存在指定"<字段名>"的记录,"1"表示选择存在指定"<字段名>"的记录。

> cl.remove({b:{$exists:1}})
Takes 0.1776s.


查看集合中的记录,返回剩余的三条格式2的记录,格式1的三条记录已经被删除

> cl.find()
{
"_id": {
"$oid":"53c36453d0d8286f2000001e"
},
"a": 3,
"c": 3
}
{
"_id": {
"$oid":"53c36453d0d8286f2000001f"
},
"a": 4,
"c": 4
}
{
"_id": {
"$oid":"53c36453d0d8286f20000020"
},
"a": 5,
"c": 5
}
Return 3 row(s).
Takes 0.4593s.


重新插入3条格式1的记录

> for(vari=0;i<3;i++){cl.insert({"a":i,"b":i})}
Takes 0.2396s.


确认记录被插入

> cl.find()
{
"_id": {
"$oid":"53c36441d0d8286f2000001b"
},
"a": 0,
"b": 0
}
{
"_id": {
"$oid":"53c36441d0d8286f2000001c"
},
"a": 1,
"b": 1
}
{
"_id": {
"$oid":"53c36441d0d8286f2000001d"
},
"a": 2,
"b": 2
}
{
"_id": {
"$oid":"53c36453d0d8286f2000001e"
},
"a": 3,
"c": 3
}
{
"_id": {
"$oid":"53c36453d0d8286f2000001f"
},
"a": 4,
"c": 4
}
{
"_id": {
"$oid":"53c36453d0d8286f20000020"
},
"a": 5,
"c": 5
}
Return 6 row(s).
Takes 0.6080s.


使用$or匹配符,删除所有格式1(包含字段"b")或者格式2(包含字段“c“)的记录

$or 是逻辑”或“操作

>cl.remove({$or:[{b:{$exists:1}},{c:{$exists:1}}]})
Takes 0.1379s.


查看记录,所有的格式1或者格式2的记录都已经被删除

> cl.find()
Return 0 row(s).
Takes 0.1247s.


重新插入格式1和2的记录各三条

> for(vari=0;i<3;i++){cl.insert({"a":i,"b":i})}
Takes 0.2974s.
> for(vari=3;i<6;i++){cl.insert({"a":i,"c":i})}
Takes 0.2045s.
> cl.find()
{
"_id": {
"$oid":"53c36507d0d8286f20000024"
},
"a": 0,
"b": 0
}
{
"_id": {
"$oid":"53c36507d0d8286f20000025"
},
"a": 1,
"b": 1
}
{
"_id": {
"$oid":"53c36507d0d8286f20000026"
},
"a": 2,
"b": 2
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000028"
},
"a": 4,
"c": 4
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000029"
},
"a": 5,
"c": 5
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000027"
},
"a": 3,
"c": 3
}
Return 6 row(s).
Takes 0.6644s.



使用update方法和$inc更新符,和$gt, $exists匹配符,选择字段"c"的值大于4的记录,并将这些记录字段"a"的值增加10

$inc操作是给指定“<字段名>”增加指定的“<值>”。

$gt选择满足“字段名”的值大于(>)指定“值”的记录。

>cl.update({$inc:{a:10}},{c:{$gt:4,$exists:1}})
Takes 0.39929s.


查看操作结果,字段"c"的值是"5"的记录的字段"a”的值增加了10

> cl.find()
{
"_id": {
"$oid":"53c36507d0d8286f20000024"
},
"a": 0,
"b": 0
}
{
"_id": {
"$oid":"53c36507d0d8286f20000025"
},
"a": 1,
"b": 1
}
{
"_id": {
"$oid":"53c36507d0d8286f20000026"
},
"a": 2,
"b": 2
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000028"
},
"a": 4,
"c": 4
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000029"
},
"a": 15,
"c": 5
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000027"
},
"a": 3,
"c": 3
}
Return 6 row(s).
Takes 0.5286s.



使用$unset更新符,将所有记录中的字段"b"和"c"删除

$unset操作是删除集合中指定的字段名。如果记录中没有指定的字段名,跳过。

>cl.update({$unset:{b:"",c:""}})
Takes 0.1858s.


查看记录,所有的字段"b"和"c"都已经被删除

> cl.find()
{
"_id": {
"$oid":"53c36507d0d8286f20000024"
},
"a": 0
}
{
"_id": {
"$oid": "53c36507d0d8286f20000025"
},
"a": 1
}
{
"_id": {
"$oid":"53c36507d0d8286f20000026"
},
"a": 2
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000028"
},
"a": 4
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000029"
},
"a": 15
}
{
"_id": {
"$oid":"53c3650ed0d8286f20000027"
},
"a": 3
}
Return 6 row(s).
Takes 0.2611s.


使用$lt匹配符,删除所有包含字段"a"的值小于30的记录

>$lt选择满足“字段名”的值小于(<)指定“值”的记录。

>cl.remove({a:{$lt:30}})
Takes 0.1272s.


查看记录,所有的记录都已经被删除了

> cl.find()
Return 0 row(s).
Takes 0.1069s.


插入5条格式3的记录

> for(vari=0;i<6;i++){cl.insert({"name":"name"+i,"age":10+i,"index":i})}
Takes 0.4366s.
> cl.find()
{
"_id": {
"$oid":"53c36708d0d8286f2000002a"
},
"name": "name0",
"age": 10,
"index": 0
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002b"
},
"name": "name1",
"age": 11,
"index": 1
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002c"
},
"name": "name2",
"age": 12,
"index": 2
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002d"
},
"name": "name3",
"age": 13,
"index": 3
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002e"
},
"name": "name4",
"age": 14,
"index": 4
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002f"
},
"name": "name5",
"age": 15,
"index": 5
}
Return 6 row(s).
Takes 0.6854s.



使用aggregate方法,聚集所有记录并隐藏字段"index"

$project,选择需要输出的字段名,"1"表示输出,"0"表示不输出。

返回结果还是输出了"index"字段,原因不清楚

>cl.aggregate({$project:{"index":0}})
{
"_id": {
"$oid":"53c36708d0d8286f2000002a"
},
"name": "name0",
"age": 10,
"index": 0
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002b"
},
"name": "name1",
"age": 11,
"index": 1
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002c"
},
"name": "name2",
"age": 12,
"index": 2
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002d"
},
"name": "name3",
"age": 13,
"index": 3
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002e"
},
"name": "name4",
"age": 14,
"index": 4
}
{
"_id": {
"$oid":"53c36708d0d8286f2000002f"
},
"name": "name5",
"age": 15,
"index": 5
}
Return 6 row(s).
Takes 0.7558s.





使用aggregate方法,聚集所有记录并,这次设置显示"name",“age”字段,隐藏字段"index"

返回成功,"index"字段被隐藏了,可是还是很奇怪,为什么必须要设置显示其他字段才能隐藏字段

> cl.aggregate({$project:{name:1,age:1,index:0}})
{
"name": "name0",
"age": 10
}
{
"name": "name1",
"age": 11
}
{
"name": "name2",
"age": 12
}
{
"name": "name3",
"age": 13
}
{
"name": "name4",
"age": 14
}
{
"name": "name5",
"age": 15
}
Return 6 row(s).
Takes 0.5398s.


利用$project设置只显示"name“字段,利用$limit将返回结果限制为2条

$limit,限制返回的记录条数。

返回结果正确

>cl.aggregate({$project:{name:1,age:0,index:0}},{$limit:2})
{
"name": "name0"
}
{
"name": "name1"
}
Return 2 row(s).
Takes 0.4870s.





利用$project设置只显示"name“字段,利用$limit将返回结果限制为2条,利用$skip跳过前两条记录

$skip,控制结果集的开始点,即跳过结果集中指定条数的记录。

返回结果正确,结果集中的两条记录被跳过,返回空

>cl.aggregate({$project:{name:1,age:0,index:0}},{$limit:2},{$skip:2})
Return 0 row(s).
Takes 0.1400s.


利用$project设置只显示"name“字段,利用$skip跳过前两条记录

返回结果正确,从第三条记录开始显示

> cl.aggregate({$project:{name:1,age:0,index:0}},{$skip:2})
{
"name": "name2"
}
{
"name": "name3"
}
{
"name": "name4"
}
{
"name": "name5"
}
Return 4 row(s).
Takes 0.3248s.


清空集合中的记录

>cl.remove()
Takes 0.1073s.
> cl.find()
Return 0 row(s).
Takes 0.1135s.

至此,SequoiaDB的各种类型的有代表性的操作符都尝试实现了一下,通过这次尝试,也帮助我对JSON对象的语法格式有了一个大致的了解,对SequoiaDB也有了进一步的掌握,对NoSQL也有了更深一步的认识,希望其他对SequoiaDB感兴趣的朋友和我联系,大家多多交流,共同进步。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值