【问题标题】:MongoDB - Join an array of strings and separate by commasMongoDB - 加入一个字符串数组并用逗号分隔
【发布时间】:2022-01-17 10:37:45
【问题描述】:

我目前有一个结构化数据库,例如

return new Schema({
_id: {
    type: mongoose.Schema.Types.ObjectId,
    auto: true
},
name: {type: String, unique: false, required: true},
criteria: [
    {
        _id: {
            type: mongoose.Schema.Types.ObjectId,
            auto: true
        },
        category: {type: String, required: false},
        links: {type: [String], required: false, default: []},
        dateOfEntry: {
            type: Date,
            default: new Date(),
            required: true
        },
        lastUpdated: {
            type: Date,
            default: new Date(),
            required: true
        }
    }
],
isActive: {type: Boolean, required: false, default: true}});

样本数据

[
  {
    "name": "item1",
    "criteria": [
      {
        "category": "category_A",
        "links": [
          "link_issue1",
          "link_issue2",
          "link_issue3"
        ]
      },
      {
        "category": "category_B",
        "links": [
          "link_issue1",
          "link_issue2"
        ]
      },
      
    ]
  },
  {
    "name": "item2",
    "criteria": [
      {
        "category": "category_C",
        "links": []
      },
    ]
  }
]

所以现在,我想将链接的数据类型从数组更改为字符串并更新现有数据

我的预期喜欢

[
  {
    "name": "item1",
    "criteria": [
      {
        "category": "category_A",
        "links": "link_issue1,link_issue2,link_issue3"
      },
      {
        "category": "category_B",
        "links": "link_issue1,link_issue2"
      },
      
    ]
  },
  {
    "name": "item2",
    "criteria": [
      {
        "category": "category_C",
        "links": ""
      },
      
    ]
  }
]

我的数据库有大约 500-1000 条记录需要更新。那么我们可以通过 MongoDB shell 更新它们吗? 非常感谢您的帮助。

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    使用$concat

    db.collection.update({},
    [
      {
        $set: {
          criteria: {
            $map: {
              input: "$criteria",
              as: "c",
              in: {
                category: "$$c.category",
                links: {
                  "$reduce": {
                    "input": "$$c.links",
                    "initialValue": "",
                    "in": {
                      "$concat": [
                        "$$value",
                        "$$this",
                        {
                          $cond: {
                            if: {
                              "$eq": [
                                { $subtract: [ { $size: "$$c.links" }, 1 ] },
                                { $indexOfArray: [ "$$c.links", "$$this" ] }
                              ]
                            },
                            then: "",
                            else: ","
                          }
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    ],
    {
      "multi": true
    })
    

    mongoplayground

    【讨论】:

    • 在第 2 项中,如果我添加更多链接,那么您的管道将不起作用。它只是更新第一项。那么我们应该为条件字段添加 $map 阶段吗?
    • @IrisLouis 我用{ "multi": true }更新我的答案
    猜你喜欢
    • 1970-01-01
    • 2017-04-10
    • 2017-10-09
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多