mongodb类型更改为数组 [英] mongodb type change to array

查看:428
本文介绍了mongodb类型更改为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中有一个字符串。 {field:some text} ,我想将它们全部转换为数组。 {field:[some text]}

I have a field in mongodb that's a string. {"field": "some text"}, I want to convert them all into arrays. {"field": ["some text"]}

我知道我可以遍历所有文件,得到该字段,然后更新,但我想知道是否有更清洁的方式。

I know I can just loop through all the documents, get the field, then update, but I'm wondering if there's a cleaner way.

谢谢。

推荐答案

您可以在map / reduce的Reduce函数中执行此操作以保留mongodb中的所有处理。基本上,您可以使用map / reduce将结果放入新集合中,然后将它们复制回旧集合(或删除旧集合并重命名新集合)。这样做的好处就是可以将所有内容保留在mongo中。

You could do it in a Reduce function of map/reduce to keep all the processing in mongodb. Essentially you would use map/reduce to put the results into a new collection and then you could copy them back to the old collection (or delete old one and rename the new one). This has the advantage of keeping everything inside of mongo.

更新:另一个选项可能是你使用 db.eval 为此。 db.eval 在服务器上执行,因此更新将在服务器上完成,没有任何流量/延迟。

Update: Another option might be for you to use db.eval for this. db.eval executes on the server so the updates would be done on the server without any traffic/latency.

我认为唯一的另一个选择是你所描述的 - 通过查询和更新每个选项在客户端上做。

I think the only other option is as you described - do it on the client by querying and updating each one.

这篇关于mongodb类型更改为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆