unicloud表增刪改查


'use strict';
const db=uniCloud.database();
exports.main = async (event, context) => {
//event為客戶端上傳的參數
console.log(event.a)
//const res=[]
if(event.a==1){
//增
console.log(111)
const collection=db.collection('uni-id-users');
var res=await collection.add([
{
"_id_":2,
"username":"胖虎",
"mobile":"18253590666",
"gong":1
}
])
}
if(event.a == "2" ){
console.log(222)
//查


const collection=db.collection('uni-id-users');
//限制條數
// var res=await collection.limit(2).get()
var res=await collection.get()
/*var res=await collection.where({

gong:1
}).get()*/
//單條
// var res=await collection.doc('60ab465c3b7d3500014a294d').get()

//根據條件,倒敘,分頁

var res=await collection.where({
xh: event.xh
}).orderBy("time", "desc").skip((event.page) * 20).limit(20).get()


}
if(event.a == "3" ){
//改
const collection=db.collection('uni-id-users');

//如果當前_id的記錄不存在,那么不會更新任何數據
/*var res=await collection.doc('60ab465c3b7d3500014a294d').update({
username:'老胖虎'
})*/
//如果當前_id的記錄不存在,會增加一跳數據
var res=await collection.doc('60ab465c3b7d3500014a294d').set({
username:'老胖虎'
})
}
if(event.a == "4" ){
console.log(222)
//刪除


const collection=db.collection('uni-id-users');

var res=await collection.where({

_id:'60ab465c3b7d3500014a294d'
}).remove()
}
//返回數據給客戶端
return {
code:200,
data:res,
msg:'OK'
}
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM