2

While running db.runCommand it is not able to recognize dollar symbol and therefore treating $answer as string and not actual value.

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

Output: dollar symbol is not recognized.

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

While using updateMany query the similar query works Update Query:

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"
                              }]
            }
    }]
)

Result:

 [
                {
                    "resumeLink": "https://cdn.upgrad.com/resume/asasjyotiranjana11.docx",
                    "dateUploaded": "2051-04-26T14:30:00.000Z",
                    "uploadSizeInByte": 644234,
                    "resumeId": "7fa1478d-478f-4869-9c4b-7ca8c0b9434g",
                    "source": "hiration"
                }
]

can some one help me how to get same result with runCommand. thanks in advance

kumar Anny
  • 346
  • 4
  • 12
  • UUID is a clientside/mongosh function. It does all javascript manipulations on the client before sending the update command to mongo. – Alex Blex Jun 10 '22 at 10:25
  • can you please explain for $answer, it refers to existing answer value, it works with updateMany but doesn't wotk with runCommand. Also if i have to use inbuilt function such as date() or uuid(), can't i use with runCommand – kumar Anny Jun 10 '22 at 10:27

0 Answers0