Mac Charles 抓包 iPhone Https(详细流程)

前言

由于官方写的过于仔细,所以特意整理一份简单明了的操作指南,接下来直接进入操作,部分相关介绍可能是借鉴其他博主或官方介绍


一、连接数据库拿到表集合

// 获取数据库对象
const db = uniCloud.database();

// 获取名为 user的表集合
const user_table= db.collection("user");

二、使用步骤

1.写入数据

调用add方法,给某数据表新增数据记录时,如果该数据表不存在,会自动创建该数据表。如下代码给user数据表新增了一条数据,如果user不存在,会自动创建。


'use strict';
// 获取数据库对象
const db = uniCloud.database();

// 云函数
exports.main = async (event, context) => {
    
    

    // 获取名为 user 的集合
    const user_table = db.collection("user");
    // 单条插入
    let res = await user_table.add({
    
    
      name: 'Ben',
    })
    
    // 批量插入
    let res2 = await user_table.add([
    {
    
    
        name: '张同学'
    },{
    
    
        name: '李同学'
    },{
    
    
        name: '王同学'
    }
    ])
    
    return {
    
    
        code: 200,
        msg: '添加成功',
        data: res2
    }
}

2.引用doc()

collection对象的方法可以增和查数据,删和改不能直接操作,需要collection对象通过doc或get得到指定的记录后再调用remove或update方法进行删改。默认是通过id唯一查询

2.1 修改数据方法一: doc.set()

通过set()方法会覆盖整条数据,如果数据不存在的话会直接创建

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 将id为12345的数据进行一个覆盖
let res = await user_table.doc('12345').set({
    
    
  name: "张同学",
})

2.2 修改数据方法二: doc.update()

通过update()方法会进行数据的一个局部更新

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 将id为12345的张同学修改为新同学
let res = await user_table.doc('12345').update({
    
    
  name: "新同学",
})


2.3 删除数据: doc.remove()

通过remove()方法进行指定ID删除

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 获取id为12345的同学,并进行一个删除
user_table.doc('12345').remove().then((res) => {
    
    
        console.log("删除成功");
 })

3.读取数据

3.1 读取全部数据: collection.get()

获取集合中的记录,如果有使用 where 语句定义查询条件,则会返回匹配结果集 (触发请求)

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 获取user_table所有数据
let res = user_table.get()

rerun {
    
    
    code: 200,
    msg: '查询成功',
    data: res
}

3.2 读取筛选数据: collection.where()

设置过滤条件,where 可接收对象作为参数,表示筛选出拥有和传入对象相同的 key-value 的数据

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 获取user_table中name为张同学的数据
let res = await user_table.where({
    
    
  name: '张同学'
}).get()

rerun {
    
    
    code: 200,
    msg: '查询成功',
    data: res
}

3.3 计数: collection.count()

获取符合条件的记录条数

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 获取user_table中name为张同学的数据条数
let res = await user_table.where({
    
    
  name: '张同学',
}).count()

return {
    
    
    code: 200,
    msg: '查询成功',
    data: res.total
}


3.4 起始位置: collection.skip()

跳过指定的位置,返回该位置之后的数据,数据量很大的情况下,skip性能会很差,建议使用分页的形式处理

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 从4四条数据开始读取
let res = await user_table.skip(4).get()

3.5 结果排序: collection.orderBy()

项目 Value 项目
field string 排序的字段
orderType string 排序的顺序,升序(asc) 或 降序(desc)

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// name升序查询
let res = await user_table.orderBy("name", "asc").get()

3.6 数据量: collection.limit()

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 只返回前10条记录
let res = await user_table.limit(10).get() 

3.7 限制返回: collection.field()

指定返回字段

// 获取数据库对象,拿到表
const db = uniCloud.database();
const user_table= db.collection("user");

// 只返回name字段、_id字段,其他字段不返回
let res = await collection.field({
    
     'name': true })

三、实战综合案例

1. 分页获取数据

'use strict';
// 获取数据库对象
const db = uniCloud.database();

// 云函数
exports.main = async (event, context) => {
    
    
	//前端传入的参数:search,pageIndex ,pageSize
    let search= event.search? event.search: {
    
    } ;  
    let pageIndex = event.pageIndex ? event.pageIndex : 1 ;  // 页码
	let pageSize = event.pageSize ? event.pageSize : 10 ;   // 每页展示条数
   
    // 获取总数量
    let dataCount = await db.collection(dbName).where(filter).count() 
    let total = dataCount .total 
   
    //计算页数
    const totalPage = Math.ceil(total / pageSize) 

     //如果没有数据了,就返回false
    let hasMore ;
    if (pageIndex > totalPage || pageIndex == totalPage) {
    
     
		hasMore = false 
    } else {
    
    
	     hasMore = true 
    }

    return db.collection(dbName)
			.where({
    
    is_on_sale : true})
			.skip((pageIndex - 1) * pageSize)
			.limit(pageSize)
			.get()
			.then( res => {
    
    
				res.hasMore = hasMore ;
				res.code  = 200
				return res ;  // 返回json给客户端
			})     
}


猜你喜欢

转载自blog.csdn.net/zz00008888/article/details/128613775