gpt4 book ai didi

mongodb - 将mongo中的大写字母更改为 Camel 外壳?

转载 作者:可可西里 更新时间:2023-11-01 09:34:30 32 4
gpt4 key购买 nike

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

{
firstName: 'FIDO',
secondName: 'JOHN',
...
}

我想知道是否可以将字段设置为驼峰大小写。

{
firstName: 'Fido',
secondName: 'John',
...
}

最佳答案

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

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) } }
);
});

关于mongodb - 将mongo中的大写字母更改为 Camel 外壳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41906508/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com