jyc226

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" ){

//获取_id

var uid = await collection.where({
id: event.id
}).get()
var uid2=uid.data[0]["_id"]


//改
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\'
}
};

分类:

技术点:

相关文章: