将 Mongo 嵌入文档移动到自己的集合中
有人能指出我正确的方向吗,我有一个集合(表单),每个表单都有一个嵌入的文档数组(响应)。对每种表单的响应已经变得巨大,事后看来嵌入它们是一个坏主意(包括嵌入在内的 mongo 文档有最大大小限制)。
有没有一种方法可以快速轻松地将所有这些嵌入的响应移动到他们自己的集合中?有没有像旧的 SQL select into 这样的东西?我在 Rails 控制台中进行了查看,但它无法访问如此多的嵌入式文档,所以我想它必须是 mongo 控制台中的复杂查找和插入查询? (只是猜测)
我的模型是固定的,但这次迁移(和 mongo 文档)让我感到困惑。
TIA 道格尔
Can someone point me in the right direction, i have a Collection (Forms) each Form has an embedded document array (Responses). Responses for each form have gotten massive and in hindsight embedding them was a bad idea (mongo documents including embedded have a maximum size limit).
Is there a way i can quickly and easily move all of these embedded Responses into their own collection? is there such a thing like the old SQL select into? I have had a look around in rails console but it is inaccessible with so many embedded documents, so i imagine it'll have to be a complex find and insert query in the mongo console? (just guessing there)
My Model is fixed but this migration (and the mongo docs) are stumping me.
TIA
Dougle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以这是一个开始...这是在 mongo shell 中
创建了一个文档 json 结构,如下所示:
现在循环响应,并使用问题的 _id 设置它们的 Question_id,然后将其插入到新的响应集合
中想要更改 db.questions.findOne 以找到所有问题并循环。如果这确实需要一段时间,您可能需要切换到 Map-Reduce 函数。
So here's a start... This is in the mongo shell
This created a document json structure like so:
Now loop through the responses, and set their question_id with the questions' _id, and then insert it into the new responses collection
You'll want to change the db.questions.findOne to find all of them and loop over. If this does take a while, you may need to switch to a map-reduce function.
这是我们最终得到的代码,基于 Jesse Wolgamott 的答案。
Here is the code we ended up with, based off Jesse Wolgamott's answer.