将 mongo 中的大写字母更改为驼峰式大小写?

ben*_*der 4 mongodb

我有一个名为 User 的集合,其中包含字段 FirstName 和 SecondName。但数据是大写字母。

{
  firstName: 'FIDO',
  secondName: 'JOHN',
  ...
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以将字段制作成骆驼箱。

{
  firstName: 'Fido',
  secondName: 'John',
  ...
}
Run Code Online (Sandbox Code Playgroud)

Sou*_*gat 5

您可以使用辅助函数来获得您想要的答案。

function titleCase(str) {
    return str.toLowerCase().split(' ').map(function(word) {
        return word.replace(word[0], word[0].toUpperCase());
    }).join(' ');
}

db.User.find().forEach(function(doc){
    db.User.update(
        { "_id": doc._id },
        { "$set": { "firstName": titleCase(doc.firstName) } }
    );
});
Run Code Online (Sandbox Code Playgroud)