skip to Main Content

I have ‘Users’ collection which has two columns, ‘_id’ and ‘userName’, both of type string.
I want to add third column ‘UserId’ which will be UUID wrapping the id from _id column.

Tried few ways but without any success.

For example:

{
_id: "fe83f869-154e-4c26-a5db-fb147728820f",
userName: "alex"
}

I want it to be:

{
_id: "fe83f869-154e-4c26-a5db-fb147728820f",
userName: "alex",
UserId: UUID("fe83f869-154e-4c26-a5db-fb147728820f")
}

I tried something like:

db.Users_temp.update(
  {},
  { $set: {"UserId": UUID("$_id") } },
  false,
  true
)

But it results in columns with value UUID("—-")

Will appreciate any help.

2

Answers


  1. Chosen as BEST ANSWER

    Ok,

    Found a solution to my problem.

    db.Users_temp.find().forEach(function(user) {
        db.Users_temp.update(
            {"_id" : user._id},
            { "$set": {"UserId": UUID(user._id)} }
        )
    })
    

  2. this will work

    i am not sure why but this works only with set operation as an array rather than as a object

    db.Users_temp.update({},[{$set: {'UserId': '$_id'}}])

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search