云數(shù)據(jù)庫:
云數(shù)據(jù)庫提供高性能的數(shù)據(jù)庫寫入和查詢服務(wù)。通過騰訊云開發(fā)(Tencent CloudBase.TCB)的SDK,可以直接在客戶端對數(shù)據(jù)進(jìn)行讀寫,也可以在云函數(shù)中讀寫數(shù)據(jù),還可以通過控制臺對數(shù)據(jù)進(jìn)行可視化的增、刪、查、改等操作。微信小程序云開發(fā)所使用的數(shù)據(jù)庫本質(zhì)上就是一MongoDB數(shù)據(jù)庫。MongoDB數(shù)據(jù)庫是介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品:是非關(guān)系數(shù)據(jù)庫中功能最豐富、最像關(guān)系數(shù)據(jù)庫的。
數(shù)據(jù)庫:默認(rèn)情況下,云開發(fā)的函數(shù)可以使用當(dāng)前環(huán)境對應(yīng)的數(shù)據(jù)庫??梢愿鶕?jù)需要使用不同的數(shù)據(jù)庫。對應(yīng)MySQL中的數(shù)據(jù)庫。
集合:數(shù)據(jù)庫中多個記錄的集合。對應(yīng)MySQL中的表。
文檔:數(shù)據(jù)庫中的一條記錄。對應(yīng)MySQL中的行。
字段:數(shù)據(jù)庫中特定記錄的值。對應(yīng)MySQL中的列。
數(shù)據(jù)類型:
string:字符串。
number:數(shù)字。
object:對象。
array:數(shù)組。
bool:布爾值。
date:時間。
geo:多種地理位置類型。
null。
云數(shù)據(jù)庫API
觸發(fā)網(wǎng)絡(luò)請求的API
API | 說明 |
---|---|
get | 獲取集合/記錄數(shù)據(jù) |
add | 在集合上新增記錄 |
update | 更新集合/記錄數(shù)據(jù) |
set | 替換更新一個記錄 |
remove | 刪除記錄 |
count | 統(tǒng)計(jì)查詢語句對應(yīng)的記錄條數(shù) |
獲取引用的API
API | 說明 |
---|---|
database | 獲取數(shù)據(jù)庫引用,返回Database對象 |
collection | 獲取集合引用,返回Collection對象 |
doc | 獲取對一個記錄的引用,返回Document對象 |
數(shù)據(jù)庫對象的字段
API | 說明 |
---|---|
command | 獲取數(shù)據(jù)庫查詢及更新指令,返回Command |
serverDate | 構(gòu)造服務(wù)端時間 |
Geo | 獲取地理位置操作對象,返回Geo對象 |
集合對象API
API | 說明 |
---|---|
doc | 獲取對一個記錄的引用,返回Document對象 |
add | 在集合上新增記錄 |
where | 構(gòu)建一個在當(dāng)前集合上的查詢條件,返回Query,查詢條件中可使用查詢指令 |
orderBy? | 指定查詢數(shù)據(jù)的排序方式 |
limit | 指定返回?cái)?shù)據(jù)的數(shù)量上限 |
skip | 指定查詢時從選中的記錄列表中的第幾項(xiàng)之后開始返回 |
field | 指定返回結(jié)果中每條記錄應(yīng)包含的字段 |
記錄/文檔對象API
API | 說明 |
---|---|
get | 獲取記錄數(shù)據(jù) |
update | 局部更新數(shù)據(jù) |
set | 替換更新記錄 |
remove | 刪除記錄 |
field | 指定返回結(jié)果中記錄應(yīng)包含的字段 |
Command對象查詢指令
類別 | 指令 | 說明 |
---|---|---|
比較運(yùn)算 | eq | 字段是否等于指定值 |
neq | 字段是否不等于指定值 | |
lt | 字段是否小于指定值 | |
lte | 字段是否小于或等于指定值 | |
gt | 字段是否大于指定值 | |
gte | 字段是否大于或等于指定值 | |
in | 字段值是否在指定數(shù)組中 | |
nin | 字段值是否不在指定數(shù)組中 | |
邏輯運(yùn)算 | and | 條件與,表示需同時滿足多個查詢篩選條件 |
or | 條件或,表示只需滿足其中一個條件即可 | |
nor | 表示需所有條件都不滿足 | |
not | 條件非,表示對給定條件取反 | |
字段 | exists | 字段存在 |
mod | 字段值是否符合給定取模運(yùn)算 | |
數(shù)組 | all | 數(shù)組所有元素是否滿足給定條件 |
elemMatch | 數(shù)組是否有一個元素滿足所有給定條件 | |
size | 數(shù)組長度是否等于給定值 | |
地理位置 | geoNear | 找出字段值在給定點(diǎn)的附近的記錄 |
geoWithin | 找出字段值在指定區(qū)域內(nèi)的記錄 | |
geoIntersects | 找出與給定的地理位置圖形相交的記錄 |
類別 | 指令 | 說明 |
---|---|---|
字段 | set | 設(shè)置字段為指定值 |
remove | 刪除字段 | |
inc | 原子操作,自增字段值 | |
mul | 原子操作,自乘字段值 | |
min | 如果字段值小于給定值,則設(shè)為給定值 | |
max | 如果字段值大于給定值,則設(shè)為給定值 | |
rename | 字段重命名 | |
數(shù)組 | push | 往數(shù)組尾部增加指定值 |
pop | 從數(shù)組尾部刪除一個元素 | |
shift | 從數(shù)組頭部刪除一個元素 | |
unshift | 往數(shù)組頭部增加指定值 | |
addToSet | 原子操作,如果不存在給定元素則添加元素 | |
pull | 剔除數(shù)組中所有滿足給定條件的元素 | |
pullAll | 剔除數(shù)組中所有等于給定值的元素 |
云數(shù)據(jù)庫操作代碼模板:
1. get模板
db.collection('test')
.where({
price: _.gt(10)
})
.field({
name: true,
price: true,
})
.orderBy('price', 'desc')
.skip(1)
.limit(10)
.get()
2.?aggregate模板
db.collection('test')
.aggregate()
.group({
// 按 category 字段分組
_id: '$category',
// 每組有一個 avgSales 字段,其值是組內(nèi)所有記錄的 sales 字段的平均值
avgSales: $.avg('$sales')
})
.end()
3. add模板
db.collection('test')
.add({
data: [
{
_id: 'apple-1',
name: 'apple',
category: 'fruit',
price: 10,
},
{
_id: 'orange-1',
name: 'orange',
category: 'fruit',
price: 15,
},
{
_id: 'watermelon-1',
name: 'watermelon',
category: 'fruit',
price: 20,
},
{
_id: 'yaourt-1',
name: 'yaourt',
category: 'dairy',
price: 8,
},
{
_id: 'milk-1',
name: 'milk',
category: 'dairy',
price: 12,
},
{
_id: 'chocolate-1',
name: 'Lindt chocolate',
category: 'chocolate',
price: 16,
},
]
})
3. set模板
db.collection('test').doc('milk-1').set({
data: {
name: 'milk',
category: 'dairy',
price: 18,
}
})
4.?count模板
db.collection('test')
.where({
price: _.gt(10)
})
.count()
5.?collection.uodate模板
db.collection('test')
.where({
category: 'fruit'
})
.update({
data: {
price: _.inc(5)
}
})
6.?doc.update模板
db.collection('test').doc('orange-1')
.update({
data: {
price: _.inc(10)
}
})
7. remove模板
db.collection('test').doc('milk-1')
.remove()
8. 變量聲明模板
const serverDate = db.serverDate
const { Point } = db.Geo
db.collection('test').doc('milk-2').set({
data: {
name: 'milk 2',
category: 'dairy',
price: 20,
updateTime: serverDate(),
origin: Point(120, 48)
}
})
9. 正則表達(dá)式查詢模板文章來源:http://www.zghlxwxcb.cn/news/detail-483404.html
db.collection('table')
.where({
collegeName: db.RegExp({
regexp: 'string', // 正則表達(dá)式
// i:大小寫不敏感 m:跨行匹配 s:讓.可以匹配包括換行符在內(nèi)的所有字符
options: 'i|m|s',
})
}).get()
10. 多表聯(lián)查文章來源地址http://www.zghlxwxcb.cn/news/detail-483404.html
db.collection('table1')
.aggregate()
.sort({'字段': -1}) //-1:降序 1:升序
.match({
查詢條件
}).lookup({
from: 'table2',
localField: '當(dāng)前表連接字段',
foreignField: '對應(yīng)表外鍵字段',
as: '別名'
}).end()
到了這里,關(guān)于微信小程序云開發(fā)(云數(shù)據(jù)庫的使用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!