HydroOJ 從入門到入土(15)批量修改題目標籤(tag)

選擇還是分支?這是一個 problem。


好消息:搞到了一批題目!

壞消息:題目沒有標籤

好消息:導入的題目有標籤!

壞消息:題目標籤和自己的不一樣

好消息:標籤全部手動改完了!

壞消息:還是覺得第一版好

一、需求

  • 雖然理論上應該是導入之前就把標籤全部調整好再導入,但實際上,導入之前調整標籤並沒有那麼容易,而且往往是導入之後才發現標籤不一樣,爲時晚矣。
  • 有時候因爲教學水平的增長,看到之前的標籤分類不合理,想重新分類的時候,會發現無比棘手。因爲分類好改,但是題目數據不跟着分類走,所以新開 oj 時候的分類,更像是一次不能後悔的試錯。

二、實現

參考前一篇,直接用 sort 字段(格式爲前綴+固定 6 位題號(前邊不足補零)+後綴)對範圍內的題目批量修改 tag,具體排序跟題庫中的排序一致。這個可以直接拿來用。

最終代碼如下:

查詢範圍內題目標籤:

db.document.find(
    {
        domainId: "system",
        docType: 10,
        sort: { $gte: "B000001", $lte: "B000005" },
    },
    {
        _id: 0, // Exclude the default _id field from the output
        pid: 1, // Include the pid field
        title: 1, // Include the title field
        tag: 1
    }
)

增加/覆蓋範圍內題目標籤:

db.document.updateMany(
   {
      domainId: "system",
      docType: 10,
      sort: { $gte: "B000001", $lte: "B000005" },
   },
   {
      $set: {
         tag: ["分支","數學"] // Set the tag field to empty array
      }
   }
)

查詢範圍內題目的某一個標籤:

db.document.find(
    {
        domainId: "system",
        docType: 10,
        sort: { $gte: "B000001", $lte: "B000005" },
        tag: "分支" // Match documents where the tag array contains the value "分支"

    },
    {
        _id: 0, // Exclude the default _id field from the output
        pid: 1, // Include the pid field
        title: 1, // Include the title field
        tag: 1
    }
)

替換範圍內題目的某一個標籤:

db.document.updateMany(
   {
      domainId: "system",
      docType: 10,
      sort: { $gte: "B000001", $lte: "B000005" },
      tag: "分支" // Match documents where the tag array contains the value "分支"
   },
   {
      $set: {
         "tag.$[element]": "選擇" // Set the value of "分支" to "選擇"
      }
   },
   {
      arrayFilters: [{ "element": "分支" }] // Specify the array filter to target elements with value "分支"
   }
)

刪除範圍內題目所有標籤:

db.document.updateMany(
   {
      domainId: "system",
      docType: 10,
      sort: { $gte: "B000001", $lte: "B000005" },
   },
   {
      $set: {
         tag: [ ] // Set the tag field to empty array
      }
   }
)

三、修改方法

  1. 備份
  2. 進入 db
  3. 輸入代碼回車看反饋

具體操作請看前一篇

四、注意事項

事先備份!!!

事先備份!!!

事先備份!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章