Removing white spaces (leading and trailing) from string value

后端 未结 4 470
情歌与酒
情歌与酒 2020-12-10 00:00

I have imported a csv file in mongo using mongoimport and I want to remove leading and trailing white spaces from my string value.

Is it possible directly in mongo t

4条回答
  •  被撕碎了的回忆
    2020-12-10 00:33

    You can execute javascript in an MongoDB update command when it's in a cursor method:

    db.collection.find({},{ "category": 1 }).forEach(function(doc) {
      db.collection.update(
        { "_id": doc._id },
        { "$set": { "category": doc.category.trim() } }
      );
    })
    

    If you have a ton of records and need to batch process, you might want to look at the other answers here.

提交回复
热议问题