上一篇中嘗試了一下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感興趣的朋友和我聯繫,大家多多交流,共同進步。