简体   繁体   English

将MongoDB集合更新为数组的小写形式

[英]Update MongoDB collection toLowercase for array field

I have already seen these two posts 我已经看过这两篇文章

Update MongoDB collection using $toLower 使用$ toLower更新MongoDB集合

and

To loop through non associative array in mongoDB 在mongoDB中遍历非关联数组

But those didn't helped me. 但是那些并没有帮助我。

I have a collection with few documents like this: 我有一个收集了一些这样的文件:

{
"_id": "58bda97de000ae205f77c6e0",

"artistName": "ABC",

"trackNames": [
"1A",
"1B",
"1C",
"1D"
],
}

I want to change all the values of trackNames field to lowercase.Like this: 我想将trackNames字段的所有值更改为小写。

{
    "_id": "58bda97de000ae205f77c6e0",

    "artistName": "ABC",

    "trackNames": [
    "1a",
    "1b",
    "1c",
    "1d"
    ],
    }

Till now I tried like this: 直到现在我都这样尝试:

db.artist.find().forEach(function(e) {
e.trackNames.forEach(function(i){
i=i.toLowerCase();
db.artist.save(e)})})

But its not working. 但是它不起作用。 Please help. 请帮忙。

i = i.toLowerCase() does not actually update the object. i = i.toLowerCase()实际上并未更新该对象。 Instead you can replace the array itself with a map: 相反,您可以使用映射替换数组本身:

e.trackNames = e.trackNames.map(function (trackName) {
  return trackName.toLowerCase();
});

This will update the trackNames array on the original object which you can then save. 这将更新原始对象上的trackNames数组,然后可以保存它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM