需要将mongo字符串字段转换为对象数组

发布于 2025-02-06 02:12:26 字数 918 浏览 3 评论 0原文

我有这样的文档

{
    "_id" : ObjectId("6228cd8e72e74fa2a4bbd76c"),
    "userId" : 8426,
    "answer" : "https://cdn.upgrad.com/resumejyotiranjana.docx",
    "updatedAt" : ISODate("2022-06-09T13:48:17.296Z"),
    "questionIdentifier" : "resumeLink",
}

,需要将答案转换为对象数组,现有答案将成为RESUMELINK属性,并且更新AT将成为对象中的dateUploadate。

{
    "_id" : ObjectId("6228cd8e72e74fa2a4bbd76c"),
    "userId" : 8426,
    "answer" : [
                 {
                    "resumeLink":"https://cdn.upgrad.com/resume/asasjyotiranjana11.docx",
                    "dateUploaded": "2022-06-09T13:48:17.296Z",
                    "resumeId": "7fa1478d-478f-4869-9c4b-7ca8c0b9434g",
                    "source": "hiration"
                 }
                ],
    "updatedAt" : ISODate("2022-06-09T13:48:17.296Z"),
    "questionIdentifier" : "resumeLink",
}

通过Mongo查询来实现这一目标的快速方法是什么?提前致谢

I have a document like this

{
    "_id" : ObjectId("6228cd8e72e74fa2a4bbd76c"),
    "userId" : 8426,
    "answer" : "https://cdn.upgrad.com/resumejyotiranjana.docx",
    "updatedAt" : ISODate("2022-06-09T13:48:17.296Z"),
    "questionIdentifier" : "resumeLink",
}

I need to convert answer into array of objects, The existing answer will become resumeLink properties and updatedAT will become dateUploaded in object.

{
    "_id" : ObjectId("6228cd8e72e74fa2a4bbd76c"),
    "userId" : 8426,
    "answer" : [
                 {
                    "resumeLink":"https://cdn.upgrad.com/resume/asasjyotiranjana11.docx",
                    "dateUploaded": "2022-06-09T13:48:17.296Z",
                    "resumeId": "7fa1478d-478f-4869-9c4b-7ca8c0b9434g",
                    "source": "hiration"
                 }
                ],
    "updatedAt" : ISODate("2022-06-09T13:48:17.296Z"),
    "questionIdentifier" : "resumeLink",
}

Whats the quick way to achieve this with mongo query? thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2025-02-13 02:12:27

如下所示,使用聚合查询。
您可能需要在其中添加一些字段。

const agg = [
  {
    '$match': {
      'userId': 8426
    }
  }, {
    '$group': {
      '_id': '$userId', 
      'answers': {
        '$addToSet': {
          'recordId': '$_id', 
          'resumeLink': '$answer'
        }
      }
    }
  }
];

Use aggregation query as below.
You may need to add some fields in that.

const agg = [
  {
    '$match': {
      'userId': 8426
    }
  }, {
    '$group': {
      '_id': '$userId', 
      'answers': {
        '$addToSet': {
          'recordId': '$_id', 
          'resumeLink': '$answer'
        }
      }
    }
  }
];
つ低調成傷 2025-02-13 02:12:27

下面的Mongo查询工作:

db.getCollection('userfeedback').updateMany(
    {userId:8426, questionIdentifier:"resumeLink"},
    [{
        "$set": {
                    answer: [{  
                              "resumeLink": "$answer",
                              "resumeId": UUID().hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-'),
                              "uploadSizeInByte": -1,
                              "source":"manual",
                              "dateUploaded": "$updatedAt"
                              }]
            }
    }]
)

Below mongo query worked:

db.getCollection('userfeedback').updateMany(
    {userId:8426, questionIdentifier:"resumeLink"},
    [{
        "$set": {
                    answer: [{  
                              "resumeLink": "$answer",
                              "resumeId": UUID().hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-'),
                              "uploadSizeInByte": -1,
                              "source":"manual",
                              "dateUploaded": "$updatedAt"
                              }]
            }
    }]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文