1. 程式人生 > 其它 >校友錄小程式雲資料庫日常操作指令碼整理

校友錄小程式雲資料庫日常操作指令碼整理

介紹

校友錄小程式採用騰訊雲開發技術,雲開發提供了一個 JSON 資料庫,顧名思義,資料庫中的每條記錄都是一個 JSON 格式的物件。一個數據庫可以有多個集合(相當於關係型資料中的表),集合可看做一個 JSON 陣列,陣列中的每個物件就是一條記錄,記錄的格式是 JSON 物件。

其主要特性:

  • 安全性:對於資料庫而言,資料安全是第一位的;
  • 易用性:與小程式的特徵類似,“開箱即用,用完即走”,簡單上手,免運維;
  • 低成本:按量收費,精細化成本控制;
  • 高效能:Nosql,支援高併發讀寫;
  • 靈活性:無固定的資料庫表模式(no-schema),支援彈性伸縮;

在本案例中,校友錄小程式總共用到了16個集合,包含校友使用者,校友相簿,校友活動,校友互助,校友聚會,校友後臺管理員,校友資訊,校友日誌等


在雲開發控制檯有個高階操作,這裡可以執行開發者輸入的指令碼,比如清空集合,根據某個條件刪除集合內部分資料,查詢集合等等

常用指令碼

1、清空操作

清空校友使用者集合

db.collection('t_user')
  .where({
    _id: _.exists(true)
  })
  .remove()

2、刪除操作

刪除1990年入學的校友使用者

db.collection('t_user')
  .where({
    USER_ENROLL: 1990
  })
  .remove()

3、查詢操作

查詢名字為“覃建平”的校友使用者的所有資料

db.collection('t_user')
  .where({
    USER_NAME: 
'覃建平' }) .get()

多條件,指定欄位查詢

db.collection('t_user')
  .where({
    USER_INVITE_ID: ''
  })
   .field({
     USER_INVITE_ID:true
   })
  .skip(0)
  .limit(10)
  .get()

4、去掉某個欄位

刪除校友使用者集合的USER_VIP_MONEY,USER_VIP_RETURN_MONEY,UER_VIP_LEAVE_MONEY欄位

db.collection('t_user').where({_id:_.neq(1)})
  .update({
  data: {
    USER_VIP_MONEY:_.remove(),
    USER_VIP_RETURN_MONEY:_.remove(),
    USER_VIP_LEAVE_MONEY:_.remove(),
    }
  })

5、更新某個欄位或者新增某個欄位

更新校友使用者集合的USER_VIP_MONEY欄位,如果原來沒有這個欄位,則自動新增該欄位且賦值

db.collection('t_user').where({_id:_.neq(1)}) 
  .update({
    data: {
      USER_VIP_MONEY: 1111,  
    }
  })
 

6、複雜的多條件查詢

對於校友使用者集合按畢業年份,行業,學校,班級,專業,使用者身份,最近來訪,訪問次數等多維度查詢,排序

    /** 取得使用者分頁列表 */
    async getUserList(userId, {
        search, // 搜尋條件
        sortType, // 搜尋選單
        sortVal, // 搜尋選單
        orderBy, // 排序
        whereEx, //附加查詢條件 
        page,
        size,
        oldTotal = 0
    }) {

        orderBy = orderBy || {
            USER_LOGIN_TIME: 'desc'
        };
        let fields = FILEDS_USER_BASE;


        let where = {};
        where.and = {
            USER_OPEN_SET: ['>', 0],
            USER_STATUS: [
                ['>=', UserModel.STATUS.COMM],
                ['<=', UserModel.STATUS.VIP]
            ],
            _pid: this.getProjectId() //複雜的查詢在此處標註PID
        };

        if (util.isDefined(search) && search) {
            where.or = [{
                    USER_NAME: ['like', search]
                },
                {
                    USER_ITEM: ['like', search]
                },
                {
                    USER_COMPANY: ['like', search]
                },
                {
                    USER_TRADE: ['like', search]
                },
                {
                    USER_TRADE_EX: ['like', search]
                },
            ];

        } else if (sortType && util.isDefined(sortVal)) {
            let user = {};

            // 搜尋選單
            switch (sortType) {
                case 'companyDef':
                    // 單位性質 
                    where.and.USER_COMPANY_DEF = sortVal;
                    break;
                case 'trade':
                    // 行業 
                    where.and.USER_TRADE = ['like', sortVal]
                    break;
                case 'workStatus': //工作狀態
                    where.and.USER_WORK_STATUS = sortVal;
                    break;
                case 'same_enroll': //同級
                    user = await UserModel.getOne({
                        USER_MINI_OPENID: userId
                    });
                    if (!user) break;
                    where.and.USER_ENROLL = user.USER_ENROLL;
                    break;
                case 'same_item': //同班
                    user = await UserModel.getOne({
                        USER_MINI_OPENID: userId
                    });
                    if (!user) break;
                    where.and.USER_ITEM = user.USER_ITEM;
                    break;
                case 'same_trade': //同行
                    user = await UserModel.getOne({
                        USER_MINI_OPENID: userId
                    });
                    if (!user) break;
                    let trade = user.USER_TRADE;
                    if (trade.includes('-')) trade = trade.split('-')[0];
                    where.and.USER_TRADE = ['like', trade];
                    break;
                case 'same_city': //同城
                    user = await UserModel.getOne({
                        USER_MINI_OPENID: userId
                    });
                    if (!user) break;
                    where.and.USER_CITY = user.USER_CITY;
                    break;
                case 'enroll': //按入學年份分類
                    switch (sortVal) {
                        case 1940:
                            where.and.USER_ENROLL = ['<', 1950];
                            break;
                        case 1950:
                            where.and.USER_ENROLL = [
                                ['>=', 1950],
                                ['<=', 1959]
                            ];
                            break;
                        case 1960:
                            where.and.USER_ENROLL = [
                                ['>=', 1960],
                                ['<=', 1969]
                            ];
                            break;
                        case 1970:
                            where.and.USER_ENROLL = [
                                ['>=', 1970],
                                ['<=', 1979]
                            ];
                            break;
                        case 1980:
                            where.and.USER_ENROLL = [
                                ['>=', 1980],
                                ['<=', 1989]
                            ];
                            break;
                        case 1990:
                            where.and.USER_ENROLL = [
                                ['>=', 1990],
                                ['<=', 1999]
                            ];
                            break;
                        case 2000:
                            where.and.USER_ENROLL = [
                                ['>=', 2000],
                                ['<=', 2009]
                            ];
                            break;
                        case 2010:
                            where.and.USER_ENROLL = ['>=', 2010];
                            break;
                    }
                    break;
                case 'sort':
                    // 排序
                    if (sortVal == 'new') { //最新
                        orderBy = {
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'last') { //最近
                        orderBy = {
                            'USER_LOGIN_TIME': 'desc',
                            'USER_ADD_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'enroll') { //入學  
                        orderBy = {
                            'USER_ENROLL': 'asc',
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'info') {
                        orderBy = {
                            'USER_INFO_CNT': 'desc',
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'album') {
                        orderBy = {
                            'USER_ALBUM_CNT': 'desc',
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'meet') {
                        orderBy = {
                            'USER_MEET_CNT': 'desc',
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'login_cnt') {
                        orderBy = {
                            'USER_LOGIN_CNT': 'desc',
                            'USER_LOGIN_TIME': 'desc'
                        };
                    }
                    break;
            }
        }
        let result = await UserModel.getList(where, fields, orderBy, page, size, true, oldTotal, false);


        return result;
    }

查詢結果

交流vx: cclinux0730