我已经看过这两个帖子了
使用$ 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.
更多推荐
发布评论