为阵列字段更新MongoDB集合toLowercase(Update MongoDB collection toLowercase for array field)

编程入门 行业动态 更新时间:2024-10-24 15:25:42
阵列字段更新MongoDB集合toLowercase(Update MongoDB collection toLowercase for array field)

我已经看过这两个帖子了

使用$ toLower更新MongoDB集合

循环遍历mongoDB中的非关联数组

但那些没有帮助我。

我有一个包含这样的文档的集合:

{ "_id": "58bda97de000ae205f77c6e0", "artistName": "ABC", "trackNames": [ "1A", "1B", "1C", "1D" ], }

我想将trackNames字段的所有值更改为小写。如下所示:

{ "_id": "58bda97de000ae205f77c6e0", "artistName": "ABC", "trackNames": [ "1a", "1b", "1c", "1d" ], }

直到现在我尝试这样:

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

但它不起作用。 请帮忙。

I have already seen these two posts

Update MongoDB collection using $toLower

and

To loop through non associative array in 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:

{ "_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()实际上并不更新对象。 相反,您可以使用地图替换数组本身:

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

这将更新原始对象上的trackNames数组,然后您可以保存该数组。

i = i.toLowerCase() does not actually update the object. 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.

更多推荐

本文发布于:2023-08-01 04:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   字段   MongoDB   toLowercase   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!