1、關系型數據庫於nosql數據庫的關系
2、基本使用
//創建數據庫 const db = uniCloud.database(); //創建集合 , 如果集合存在會報錯 db.createCollection(collectionName) //操作集合(表),獲取集合的引用 db.collection(collectionName).doc(docId)
3、操作Collection
4、操作Record/Document
5、查詢篩選(db.command)
在where查詢中 eq()和:都是等於的意思 ,二者區別在於 eq查詢到的引用數據 是全等的 必須一模一樣的,而:包含就好了
in和nin表示包含與不包含關系,里邊傳需要查詢的數組。cmd.in(<Array>)
and表示且,cmd.and( cmd指令 ),一般用於單個字段中,前置語法更加方便cmd.and(語法一,語法二)
6、更新字段(db.command)
7、數據類型
注:Date表示客戶端時間,Null表示占位符,有字段當時值為空
服務端時間使用 new db.serverDate()獲取
日期比較需要先進行聚合操作進行日期轉換
'use strict'; const db = uniCloud.database() exports.main = async (event, context) => { const dbCmd = db.command const $ = dbCmd.aggregate let res = await db.collection('unicloud-test').where(dbCmd.expr( $.gte(['$time',$.dateFromString({ dateString: new Date('2020-02-02').toISOString() })]) )).get() return res };
集合新增
//新增單條 let res = await user.add({ name:'Ben' }) //新增多條 let ress = await user.add([ {name:'jack'}, {name:'rose'}, {name:'cc',like:'pp'} ])
集合查詢
支持 where()
、limit()
、skip()
、orderBy()
、get()
、field()
、count()
等
let user = db.collection('user') // 高級查詢指令 const cmd = db.command // 查詢限制 let res =await user.limit(5).get() // 查詢數量 let res1 = await user.where({name:'lisi'}).count(2) // where正則 let res2 = await user.where({name:/^l/}).get() // 高級查詢 let res3 = await user.where({ age:cmd.gt(18) }).get() // 高級查詢於過濾,排序,限制數量,指定字段 let res4 = await user .where({age:cmd.exists(true)}) .where({age:cmd.gt(18)}) .orderBy('age','asc') .limit(2) .field({'name':true,'_id':true}) .get() let res5 = await user.where({age:cmd.gt(25)}).get() let res6 = await user.where({age:cmd.gte(25)}).count() let res7 = await user.where({age:cmd.lte(5)}).field({name:true}).get() let res8 = await user.where({age:cmd.nin([25,40,5]).and(cmd.gte(18))}).get() let res9 = await user.where({ age:cmd.and(cmd.lt(40),cmd.gt(18)).or(cmd.eq(5)) }).get()
get():默認取前100條,最大取前100條,get()是查詢必備的條件
where():做篩選,字段可以使用正則匹配
count():查詢到的數量,需要跟where一起用,不需要加get
limit():查詢多少條
orderBy():結果排序,asc表升序,desc表降序;用點表示法進行連接嵌套
field():查詢需要的字段
集合刪除
集合刪除就是先找到集合的引用 然后再調用remove方法
通過id刪除:collection.doc(_id).remove()
通過條件刪除:collection.where().remove()
let user = db.collection('user') // 高級查詢指令 const cmd = db.command // docId刪除 let res =await user.doc('5f9a73ed266d14000151d653').remove() // 條件刪除 let res1 = await user.where({ age:cmd.or(cmd.eq(40),cmd.eq(5)) }).remove()