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. 输入代码回车看反馈

具体操作请看前一篇

四、注意事项

事先备份!!!

事先备份!!!

事先备份!!!

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