記一次mongodb聚合查詢

来源:https://www.cnblogs.com/du-blog/archive/2019/04/15/10713312.html
-Advertisement-
Play Games

先說一下場景,產品中用到了簡單的表單構造器,開始提供了一系列的控制項,例如單行文本框、多行文本框、單選、覆選、時間等,之後你可以拖拽控制項自己組裝你想要的表單……網上有很多的表單構造器,這裡就不細說了,可能功能有多有少,但是原理類似。因為這種表單的這種實現方式,我們不單單要存儲表單的真實數據,還要存儲對 ...


  先說一下場景,產品中用到了簡單的表單構造器,開始提供了一系列的控制項,例如單行文本框、多行文本框、單選、覆選、時間等,之後你可以拖拽控制項自己組裝你想要的表單……網上有很多的表單構造器,這裡就不細說了,可能功能有多有少,但是原理類似。因為這種表單的這種實現方式,我們不單單要存儲表單的真實數據,還要存儲對應表單的配置數據來渲染表單的樣子。這裡不細說配置相關的事情,細說一下表單數據的存儲和查詢。

  對於一條記錄有很多共有屬性:主鍵ID、創建用戶ID、創建時間、表單ID(用於讀取表單配置)等一些業務數據,這裡著重說一下表單ID也就是下麵演示中提到的TableCode,我們這種存儲方式可以將業務上的多種表都存儲在現在的一張表中,所以必須要一個欄位來區分這條記錄存儲的具體是什麼信息,以方便在查詢時找到具體的數據;還有一些就是具體的表單項了。我們採用的是Mongodb存儲,不用想肯定是複雜結構了,數組或者鍵值對對象……也不知道什麼原因了,我們採用的是數組,這裡記作FormItems,FormItems列中存儲著所有的表單項,具體的看一下資料庫的存儲結構:

{
    "_id" : "1",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-07",
    "FormItems" : [ 
        {
            "key" : "CarBrand",
            "value" : "紅旗"
        }, 
        {
            "key" : "VehicleType",
            "value" : "H7"
        }, 
        {
            "key" : "CarNum",
            "value" : "京A00001"
        }, 
        {
            "key" : "PurchaseDate",
            "value" : "2019-01-01"
        }
    ],
    "TableCode" : "CarInfo" 
}

  上面看到是一個縮減版的結構(為了方便演示),簡單的介紹一下:TableCode-表單名稱,FormItems數組中存儲著所有的表單項,key本來是唯一值,這裡僅僅為了演示所以如此,value就是具體的表單項的值了。查詢演示用到了三張表,第一張表如上,車輛信息表:車牌子、車型、牌照、購買日期等;車輛維修表:車輛ID、花費、內容;出車記錄表:車輛ID、收入、里程、備註。首先插入一些數據:

try{
    db.t1.insertMany([{
        "_id": "1",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-02-07",
        "FormItems": [{
            "key": "CarBrand",
            "value": "紅旗"
        },
        {
            "key": "VehicleType",
            "value": "H7"
        },
        {
            "key": "CarNum",
            "value": "京A00001"
        },
        {
            "key": "PurchaseDate",
            "value": "2019-01-01"
        }],
        "TableCode": "CarInfo"
    },{
        "_id": "2",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-02-08",
        "FormItems": [{
            "key": "CarBrand",
            "value": "紅旗"
        },
        {
            "key": "VehicleType",
            "value": "H5"
        },
        {
            "key": "CarNum",
            "value": "京A00002"
        },
        {
            "key": "PurchaseDate",
            "value": "2019-02-01"
        }],
        "TableCode": "CarInfo"
    },{
        "_id": "3",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-02-09",
        "FormItems": [{
            "key": "CarBrand",
            "value": "紅旗"
        },
        {
            "key": "VehicleType",
            "value": "L5"
        },
        {
            "key": "CarNum",
            "value": "京A00003"
        },
        {
            "key": "PurchaseDate",
            "value": "2019-03-01"
        }],
        "TableCode": "CarInfo"
    },
    {
        "_id": "4",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-02-10",
        "FormItems": [{
            "key": "CarBrand",
            "value": "長城"
        },
        {
            "key": "VehicleType",
            "value": "哈弗H6"
        },
        {
            "key": "CarNum",
            "value": "京A00004"
        },
        {
            "key": "PurchaseDate",
            "value": "2018-03-01"
        }],
        "TableCode": "CarInfo"
    },{
        "_id": "5",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-02-09",
        "FormItems": [{
            "key": "CarBrand",
            "value": "長城"
        },
        {
            "key": "VehicleType",
            "value": "哈弗H5"
        },
        {
            "key": "CarNum",
            "value": "京A00005"
        },
        {
            "key": "PurchaseDate",
            "value": "2018-03-01"
        }],
        "TableCode": "CarInfo"
    },{
        "_id": "6",
        "CreateUserName": "ddz1",
        "CreateDate": "2018-03-09",
        "FormItems": [{
            "key": "CarBrand",
            "value": "長城"
        },
        {
            "key": "VehicleType",
            "value": "哈弗H4"
        },
        {
            "key": "CarNum",
            "value": "京A00006"
        },
        {
            "key": "PurchaseDate",
            "value": "2018-03-01"
        }],
        "TableCode": "CarInfo"
    },{
        "_id": "7",
        "CreateUserName": "ddz100",
        "CreateDate": "2018-06-06",
        "FormItems": [{
            "key": "CarId",
            "value": "1"
        },
        {
            "key": "Cost",
            "value": 100
        },
        {
            "key": "Contents",
            "value": "噴漆"
        }],
        "TableCode": "MaintenanceRecord"
    },{
        "_id": "8",
        "CreateUserName": "ddz100",
        "CreateDate": "2018-08-06",
        "FormItems": [{
            "key": "CarId",
            "value": "1"
        },
        {
            "key": "Cost",
            "value": 25
        },
        {
            "key": "Contents",
            "value": "洗車"
        }],
        "TableCode": "MaintenanceRecord"
    },{
        "_id": "9",
        "CreateUserName": "ddz101",
        "CreateDate": "2018-06-03",
        "FormItems": [{
            "key": "CarId",
            "value": "2"
        },
        {
            "key": "Cost",
            "value": 1560
        },
        {
            "key": "Contents",
            "value": "換輪胎"
        }],
        "TableCode": "MaintenanceRecord"
    },{
        "_id": "10",
        "CreateUserName": "ddz102",
        "CreateDate": "2018-06-26",
        "FormItems": [{
            "key": "CarId",
            "value": "3"
        },
        {
            "key": "Cost",
            "value": 36
        },
        {
            "key": "Contents",
            "value": "爆胎"
        }],
        "TableCode": "MaintenanceRecord"
    },{
        "_id": "11",
        "CreateUserName": "ddz103",
        "CreateDate": "2018-09-08",
        "FormItems": [{
            "key": "CarId",
            "value": "3"
        },
        {
            "key": "Cost",
            "value": 1630
        },
        {
            "key": "Contents",
            "value": "換加速器"
        }],
        "TableCode": "MaintenanceRecord"
    },{
        "_id": "12",
        "CreateUserName": "ddz10",
        "CreateDate": "2018-06-06",
        "FormItems": [{
            "key": "CarId",
            "value": "1"
        },
        {
            "key": "Income",
            "value": 106
        },
        {
            "key": "Mileage",
            "value": 50
        },
        {
            "key": "Remarks",
            "value": "123"
        }],
        "TableCode": "DispatchRecord"
    },{
        "_id": "13",
        "CreateUserName": "ddz11",
        "CreateDate": "2018-06-16",
        "FormItems": [{
            "key": "CarId",
            "value": "1"
        },
        {
            "key": "Income",
            "value": 250
        },
        {
            "key": "Mileage",
            "value": 100
        },
        {
            "key": "Remarks",
            "value": "123"
        }],
        "TableCode": "DispatchRecord"
    },{
        "_id": "14",
        "CreateUserName": "ddz12",
        "CreateDate": "2018-06-16",
        "FormItems": [{
            "key": "CarId",
            "value": "2"
        },
        {
            "key": "Income",
            "value": 1000
        },
        {
            "key": "Mileage",
            "value": 630
        },
        {
            "key": "Remarks",
            "value": "2345sfgfg"
        }],
        "TableCode": "DispatchRecord"
    },{
        "_id": "15",
        "CreateUserName": "ddz12",
        "CreateDate": "2018-08-16",
        "FormItems": [{
            "key": "CarId",
            "value": "3"
        },
        {
            "key": "Income",
            "value": 1213
        },
        {
            "key": "Mileage",
            "value": 569
        },
        {
            "key": "Remarks",
            "value": "12asdfasdfasdf3"
        }],
        "TableCode": "DispatchRecord"
    }]);
}catch(e){
    print(e);
}
View Code

  這種存儲方式有一定的好處,但是弊端也很明顯:查詢、排序等很費勁,如果伺服器端以這樣的方式返回客戶端,客戶端還得解析FormItems,非常不友好(感覺自己在給自己找麻煩)……所以我就想看看能不能在所有的查詢操作之前,首先處理一下數據,將FormItems中的表單項都提到文檔的最外層和公共欄位一個層次,之後在進行查詢排序似乎就簡單點了,為了實現這個目標真是廢了九牛二虎之力,道路相當坎坷,經歷的時間也很長,不過最終好在找到一個例子解決了我們的問題:https://jira.mongodb.org/browse/SERVER-5947 。在此表示感謝,非常感謝……在這之後又遇到了一個問題就是:在關聯查詢時mongodb的處理方式是將所有的相關聯的數據存儲在一個屬性中,因為我們所有的“表”都存在這一個表中,所以關聯的也是這一張表,也就是關聯自己查詢,結果得到關聯的數據還是沒有處理之前的樣子,相同的數據最後的結構都不一樣,都不好意思返回客戶端,這個問題比之前的問題更費勁……不知道度過了多少天,我的一個同事說弄出來了,我就有點……所以我也想嘗試一下,最後廢了半天勁兒終於弄出來,不知道和同事弄的一樣不一樣,這裡只能吐槽mongodb你怎麼能這樣……看一下代碼:

db.getCollection('t1').aggregate([
    {$addFields:
        {
            FormValueObj:{
                $arrayToObject:{
                    $map:
                     {
                       input: "$FormItems",
                       as: "field",
                       in: ["$$field.key","$$field.value"]
                     }
                }
            }
        }
    },
    {
        $addFields:{
            "FormValueObj._id":"$_id",
            "FormValueObj.CreateUserName":"$CreateUserName",
            "FormValueObj.CreateDate":"$CreateDate",
            "FormValueObj.TableCode":"$TableCode"
        }
    },
    {
        $replaceRoot: { newRoot: "$FormValueObj" }
    },
    {
        $lookup:{
           from: "t1",
           localField: "_id",
           foreignField: "FormItems.value",
           as: "RelationData1"
        }
    },
    {
        $addFields:{
            RelationData1:{
                 $map:
                 {
                    input: "$RelationData1",
                    as: "tr",
                    in: {
                        $arrayToObject:{
                            $map:
                             {
                               input:{ 
                                   $concatArrays: [ 
                                   [
                                        {key:"_id",value:"$$tr._id"},
                                        {key:"CreateUserName",value:"$$tr.CreateUserName"},
                                        {key:"CreateDate",value:"$$tr.CreateDate"},
                                        {key:"TableCode",value:"$$tr.TableCode"},
                                   ], 
                                   "$$tr.FormItems" ] 
                               },
                               as: "field",
                               in: ["$$field.key","$$field.value"]
                             }
                        }
                    }
                 }
            }
        }
    }
])
查詢語句
/* 1 */
{
    "CarBrand" : "紅旗",
    "VehicleType" : "H7",
    "CarNum" : "京A00001",
    "PurchaseDate" : "2019-01-01",
    "_id" : "1",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-07",
    "TableCode" : "CarInfo",
    "RelationData1" : [ 
        {
            "_id" : "7",
            "CreateUserName" : "ddz100",
            "CreateDate" : "2018-06-06",
            "TableCode" : "MaintenanceRecord",
            "CarId" : "1",
            "Cost" : 100.0,
            "Contents" : "噴漆"
        }, 
        {
            "_id" : "8",
            "CreateUserName" : "ddz100",
            "CreateDate" : "2018-08-06",
            "TableCode" : "MaintenanceRecord",
            "CarId" : "1",
            "Cost" : 25.0,
            "Contents" : "洗車"
        }, 
        {
            "_id" : "12",
            "CreateUserName" : "ddz10",
            "CreateDate" : "2018-06-06",
            "TableCode" : "DispatchRecord",
            "CarId" : "1",
            "Income" : 106.0,
            "Mileage" : 50.0,
            "Remarks" : "123"
        }, 
        {
            "_id" : "13",
            "CreateUserName" : "ddz11",
            "CreateDate" : "2018-06-16",
            "TableCode" : "DispatchRecord",
            "CarId" : "1",
            "Income" : 250.0,
            "Mileage" : 100.0,
            "Remarks" : "123"
        }
    ]
}

/* 2 */
{
    "CarBrand" : "紅旗",
    "VehicleType" : "H5",
    "CarNum" : "京A00002",
    "PurchaseDate" : "2019-02-01",
    "_id" : "2",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-08",
    "TableCode" : "CarInfo",
    "RelationData1" : [ 
        {
            "_id" : "9",
            "CreateUserName" : "ddz101",
            "CreateDate" : "2018-06-03",
            "TableCode" : "MaintenanceRecord",
            "CarId" : "2",
            "Cost" : 1560.0,
            "Contents" : "換輪胎"
        }, 
        {
            "_id" : "14",
            "CreateUserName" : "ddz12",
            "CreateDate" : "2018-06-16",
            "TableCode" : "DispatchRecord",
            "CarId" : "2",
            "Income" : 1000.0,
            "Mileage" : 630.0,
            "Remarks" : "2345sfgfg"
        }
    ]
}

/* 3 */
{
    "CarBrand" : "紅旗",
    "VehicleType" : "L5",
    "CarNum" : "京A00003",
    "PurchaseDate" : "2019-03-01",
    "_id" : "3",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-09",
    "TableCode" : "CarInfo",
    "RelationData1" : [ 
        {
            "_id" : "10",
            "CreateUserName" : "ddz102",
            "CreateDate" : "2018-06-26",
            "TableCode" : "MaintenanceRecord",
            "CarId" : "3",
            "Cost" : 36.0,
            "Contents" : "爆胎"
        }, 
        {
            "_id" : "11",
            "CreateUserName" : "ddz103",
            "CreateDate" : "2018-09-08",
            "TableCode" : "MaintenanceRecord",
            "CarId" : "3",
            "Cost" : 1630.0,
            "Contents" : "換加速器"
        }, 
        {
            "_id" : "15",
            "CreateUserName" : "ddz12",
            "CreateDate" : "2018-08-16",
            "TableCode" : "DispatchRecord",
            "CarId" : "3",
            "Income" : 1213.0,
            "Mileage" : 569.0,
            "Remarks" : "12asdfasdfasdf3"
        }
    ]
}

/* 4 */
{
    "CarBrand" : "長城",
    "VehicleType" : "哈弗H6",
    "CarNum" : "京A00004",
    "PurchaseDate" : "2018-03-01",
    "_id" : "4",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-10",
    "TableCode" : "CarInfo",
    "RelationData1" : []
}

/* 5 */
{
    "CarBrand" : "長城",
    "VehicleType" : "哈弗H5",
    "CarNum" : "京A00005",
    "PurchaseDate" : "2018-03-01",
    "_id" : "5",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-09",
    "TableCode" : "CarInfo",
    "RelationData1" : []
}

/* 6 */
{
    "CarBrand" : "長城",
    "VehicleType" : "哈弗H4",
    "CarNum" : "京A00006",
    "PurchaseDate" : "2018-03-01",
    "_id" : "6",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-03-09",
    "TableCode" : "CarInfo",
    "RelationData1" : []
}

/* 7 */
{
    "CarId" : "1",
    "Cost" : 100.0,
    "Contents" : "噴漆",
    "_id" : "7",
    "CreateUserName" : "ddz100",
    "CreateDate" : "2018-06-06",
    "TableCode" : "MaintenanceRecord",
    "RelationData1" : []
}

/* 8 */
{
    "CarId" : "1",
    "Cost" : 25.0,
    "Contents" : "洗車",
    "_id" : "8",
    "CreateUserName" : "ddz100",
    "CreateDate" : "2018-08-06",
    "TableCode" : "MaintenanceRecord",
    "RelationData1" : []
}

/* 9 */
{
    "CarId" : "2",
    "Cost" : 1560.0,
    "Contents" : "換輪胎",
    "_id" : "9",
    "CreateUserName" : "ddz101",
    "CreateDate" : "2018-06-03",
    "TableCode" : "MaintenanceRecord",
    "RelationData1" : []
}

/* 10 */
{
    "CarId" : "3",
    "Cost" : 36.0,
    "Contents" : "爆胎",
    "_id" : "10",
    "CreateUserName" : "ddz102",
    "CreateDate" : "2018-06-26",
    "TableCode" : "MaintenanceRecord",
    "RelationData1" : []
}

/* 11 */
{
    "CarId" : "3",
    "Cost" : 1630.0,
    "Contents" : "換加速器",
    "_id" : "11",
    "CreateUserName" : "ddz103",
    "CreateDate" : "2018-09-08",
    "TableCode" : "MaintenanceRecord",
    "RelationData1" : []
}

/* 12 */
{
    "CarId" : "1",
    "Income" : 106.0,
    "Mileage" : 50.0,
    "Remarks" : "123",
    "_id" : "12",
    "CreateUserName" : "ddz10",
    "CreateDate" : "2018-06-06",
    "TableCode" : "DispatchRecord",
    "RelationData1" : []
}

/* 13 */
{
    "CarId" : "1",
    "Income" : 250.0,
    "Mileage" : 100.0,
    "Remarks" : "123",
    "_id" : "13",
    "CreateUserName" : "ddz11",
    "CreateDate" : "2018-06-16",
    "TableCode" : "DispatchRecord",
    "RelationData1" : []
}

/* 14 */
{
    "CarId" : "2",
    "Income" : 1000.0,
    "Mileage" : 630.0,
    "Remarks" : "2345sfgfg",
    "_id" : "14",
    "CreateUserName" : "ddz12",
    "CreateDate" : "2018-06-16",
    "TableCode" : "DispatchRecord",
    "RelationData1" : []
}

/* 15 */
{
    "CarId" : "3",
    "Income" : 1213.0,
    "Mileage" : 569.0,
    "Remarks" : "12asdfasdfasdf3",
    "_id" : "15",
    "CreateUserName" : "ddz12",
    "CreateDate" : "2018-08-16",
    "TableCode" : "DispatchRecord",
    "RelationData1" : []
}
查詢結果

  你可能看到了查詢語句太長了,這還是最基本的,幸好3.4之後提供了視圖的功能,我們可以創建一個視圖,弄成我們想要的樣子:

db.createView("t1view","t1",[
    {$addFields:
        {
            FormValueObj:{
                $arrayToObject:{
                    $map:
                     {
                       input: "$FormItems",
                       as: "field",
                       in: ["$$field.key","$$field.value"]
                     }
                }
            }
        }
    },
    {
        $addFields:{
            "FormValueObj._id":"$_id",
            "FormValueObj.CreateUserName":"$CreateUserName",
            "FormValueObj.CreateDate":"$CreateDate",
            "FormValueObj.TableCode":"$TableCode"
        }
    },
    {
        $replaceRoot: { newRoot: "$FormValueObj" }
    }
])
創建視圖

  之後我們就是在視圖中查詢了

db.getCollection('t1view').aggregate([
    {$match:{_id:"1"}},
    {
        $lookup:{
           from: "t1view",
           localField: "_id",
           foreignField: "CarId",
           as: "RelationData1"
        }
    }
])
視圖查詢
/* 1 */
{
    "CarBrand" : "紅旗",
    "VehicleType" : "H7",
    "CarNum" : "京A00001",
    "PurchaseDate" : "2019-01-01",
    "_id" : "1",
    "CreateUserName" : "ddz1",
    "CreateDate" : "2018-02-07",
    "TableCode" : "CarInfo",
    "RelationData1" : [ 
        {
            "CarId" : "1",
            "Cost" : 100.0,
            "Contents" : "噴漆",
            "_id" : "7",
            "CreateUserName" : "ddz100",
            "CreateDate" : "2018-06-06",
            "TableCode" : "MaintenanceRecord"
        }, 
        {
            "CarId" : "1",
            "Cost" : 25.0,
            "Contents" : "洗車",
            "_id" : "8",
            "CreateUserName" : "ddz100",
            "CreateDate" : "2018-08-06",
            "TableCode" : "MaintenanceRecord"
        }, 
        {
            "CarId" : "1",
            "Income" : 106.0,
            "Mileage" : 50.0,
            "Remarks" : "123",
            "_id" : "12",
            "CreateUserName" : "ddz10",
            "CreateDate" : "2018-06-06",
            "TableCode" : "DispatchRecord"
        }, 
        {
            "CarId" : "1",
            "Income" : 250.0,
            "Mileage" : 100.0,
            "Remarks" : "123",
            "_id" : "13",
            "CreateUserName" : "ddz11",
            "CreateDate" : "2018-06-16",
            "TableCode" : "DispatchRecord"
        }
    ]
}
視圖查詢結果

  這樣在操作就簡單多了,還避免了上面的第二個問題,

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動 ...
  • 所需器材有:樹莓派及其電源,usb攝像頭,路由器,兩台伺服舵機及其外殼,一臺筆記本,一根網線 樹莓派的基本操作及學習網站與資料 "樹莓派實驗室" "樹莓派3代入門資料及安裝教程" 按照以上操作初始化樹莓派 樹莓派控制攝像頭進行視頻流傳輸,併在pc端接收視頻 推薦使用 "MJPG streamer" ...
  • --正則-- 基礎正則 註:egrep,grep –E或sed –r 過濾一般特殊字元可以不轉義 擴展正則(egrep或grep -E) --grep-- 語法:grep [選項] [條件表達式] 目標文件 --sed-- 語法:sed [options] 'command' file(s) 選項: ...
  • 在IIS6下,經常出現w3wp的記憶體占用不能及時釋放,從而導致伺服器響應速度很慢。 可以做以下配置:1、在IIS中對每個網站進行單獨的應用程式池配置。即互相之間不影響。2、設置應用程式池的回收時間,預設為1720小時,可以根據情況修改。同時,設置同時運行的w3wp進程數目為1。再設置當記憶體或者cpu ...
  • 一、S5PV210時鐘系統 時鐘:一定頻率的電信號。 時鐘系統:基於CMOS工藝的高性能處理器時鐘系統,集成PLL可以從內部觸發,比從外部觸發更快且更準確,能有效地避免一些與信號完整性相關的問題。 S5pv210時鐘系統,參考s5pv210手冊第三章,CLOCK CONTROLLER S5pv210 ...
  • Figure 6.9. Virtual Machine Storage Settings In the Storage Tree section, select Empty below the IDE Controller. The CD/DVD Drive attributes are displ ...
  • 在此處只給出find的基本用法示例,都是平時我個人非常常用的搜索功能。如果有不理解的部分,則看後面的 "find運行機制詳解" 對於理論的說明,也建議在看完這些基本示例後閱讀一遍理論說明,它是本人翻譯自find的man文檔並加上了個人的理解。另外,在該理論說明結束後,還有find深入用法示例和分析。 ...
  • [20190416]11g下那些latch是Exclusive的.txt--//昨天測試了11g下那些latch是共用的,鏈接:--//是否反過來剩下的都是Exclusive的.繼續測試:1.環境:SYS@book> @ ver1PORT_STRING VERSION BANNER x86_64/L ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...