Home > Net >  Convert all elements of a collection into strings | MongoDB
Convert all elements of a collection into strings | MongoDB

Time:11-04

In my DB, all my collection's entries have tweet_id: number here. I would like to map through the entire collection with one command and reformat all these numbers (Int64) to string. Is there a command to do that? Doing it manually through the web interface would be very time-consuming.

CodePudding user response:

Use $toString in the aggregation pipeline in the update.

db.collection.update({},
[
  {
    $set: {
      tweet_id: {
        $toString: "$tweet_id"
      }
    }
  }
],
{
  multi: true
})

Mongo Playground

  • Related