巨杉数据库SequoiaDB的常用操作

巨杉数据库的基本操作。巨杉可以通过nosql的方式对数据库进行操作,也可以通过自带的sql对对数据库进行操作。但是自带的sql方式比较弱,有以下缺点:
1.语法比较苛刻,关键字as不能省略,如select a._id as id from xx.xx as a,两个as都不能省。
2.错误提示不明显,无法清楚语法哪里出错。
3.很多语法不支持
如:db.exec("select a.* from scott.person as a")  --a.*报错
    db.exec("select a.name,a.age from scott.person as a,scott.personNameAge as b where a.name=b.name")   --报错a,b关联
4.表关联性能非常差。

--下面为巨杉数据库的常用操作
root@iZwz91f71l9kck30hh2hkoZ:~# sdb
> db = new Sdb('localhost',11810) --连接巨杉
> db.createCS("scott")  --建立集合空间
> db.scott.createCL("person")  --建立集合

> db.help()   --帮助
> db.scott.help()  
> db.scott.person.help()  

--增
> db.scott.person.insert({name:"hadoop",age:10})
> db.scott.person.insert({name:"spark",age:5})
> db.execUpdate("insert into scott.person (name,value) values ('sdb',3)")    --sql写法


--查
> db.scott.person.find()
> db.scott.person.find({},{name:1})  --select name from xx.xx
> db.scott.person.find({name:"hadoop"},{name:1})   --select name from xx.xx where name='hadoop'
> db.exec("select * from scott.person")     --sql写法


--改
> db.scott.person.update({$set:{age:11}},{name:"hadoop"})  --update xx.xx set age=11 where name='hadoop'
> db.scott.person.update({$unset:{age:""}},{name:"hadoop"})  --先匹配name='hadoop'的记录,再drop掉age字段
> db.execUpdate("update scott.person set age=11 where name='hadoop'")  --sql写法

--删
> db.scott.person.remove({name:"hadoop"})  --delete from table xx.xx where name='hadoop'
> db.scott.person.remove()    --delete from table xx.xx
> db.scott.person.truncate()    --truncate table xx.xx
> db.dropCL("person")   --drop table xx
> db.execUpdate("delete from scott.person where name='hadoop'")   --sql写法


--drop字段
> db.scott.person.update({$unset:{age:""}})    --alter table scott.person drop column age;
Takes 0.1191s.

--insert...select
db.execUpdate("insert into scott.person select * from scott.personNameAge")   --不能插入多次,objectid不能重复
db.execUpdate("insert into scott.person select id,name from scott.personNameScore")  --可以插入多次

--insert(for循环)
for(var i=0; i<=7; i++) db.scott.rq.insert({rq:i})
for(var i=1; i<=100; i++) db.scott.test.insert({id:i,name:"name"+i,rq:i%7})

--limit n offset m 从下标为m开始,取n条记录
> db.exec("select bsfwtmc from scott.t_yjd order by _id limit 3 offset 3")
{
  "bsfwtmc": "惠城区河南岸办税服务厅二厅"
}
{
  "bsfwtmc": "惠城区小金口办税服务厅"
}
{
  "bsfwtmc": "惠城区水口办税服务厅"
}
Return 3 row(s).


--主键_id的查询
db.scott.t_bsfwt.find({_id:{"$oid":"589bcbf01a7edd1f1b000000"}})

> db.exec("select max(_id) as id from scott.t_bsfwt")
{
  "id": {
    "$oid": "589bcbf01a7edd1f1b00020d"
  }
}
Return 1 row(s).
Takes 0.9621s.
> db.exec("select min(_id) as id from scott.t_bsfwt")
{
  "id": {
    "$oid": "589bcbf01a7edd1f1b000000"
  }
}

--exists查null的字段
选择集合 bar 中存在字段 age 的记录
> db.foo.bar.find({age:{$exists:1}})
选择集合 bar 中嵌套对象 content 不存在 name 字段的记录
> db.foo.bar.find({"content.name":{$exists:0}})


--like操作,正则表达式Regex查找0到9开头的记录
db.exec("select gzzkb from scott.t_bsfwt  where gzzkb like '^[0-9]' ")
db.scott.t_bsfwt.find({bsfwt_dm:{$regex:'24419.*',$options:'i'}})
> db.scott.person.find({name:Regex("b","i")})         --like '%b%'
> db.scott.person.find({name:Regex("^.bc$","i")})     --like '_bc%'
> db.scott.person.find({name:Regex("^.bb$","i")})     --like '_bb%'

--分页查询
import org.bson.BasicBSONObject;
import com.sequoiadb.base.CollectionSpace;
import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.DBCursor;
import com.sequoiadb.base.Sequoiadb;

public class TestSdb_page{
        public static void main(String[] args) {
                String connString="iZ94ps2ghdsZ:11810";
                String username="";
                String password="";
                Sequoiadb db = new Sequoiadb(connString, username, password);
               
                CollectionSpace scott = db.getCollectionSpace("scott");
                DBCollection cl = scott.getCollection("cl");
                //cl.query(matcher, selector, orderBy, hint, skipRows, returnRows)
                cl.query("", "", "", "", 0, 20)
               
        }
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值