Dream4ever / Knowledge-Base

record every requirement and solution here
https://www.hewei.in/
36 stars 6 forks source link

MongoDB 对指定字段执行 trim 方法 #169

Closed Dream4ever closed 3 years ago

Dream4ever commented 3 years ago

解决过程

Google 关键词:mongodb remove space in documents field value

第一个搜索结果 Removing white spaces (leading and trailing) from string value 就给出了当前在用的 MongoDB 4.2 可以用的方法:

db.collection.update(
  {},
  [{ $set: { category: { $trim: { input: "$category" } } } }],
  { multi: true }
)