将 Mongo 嵌入文档移动到自己的集合中

发布于 2024-10-24 18:11:37 字数 311 浏览 5 评论 0原文

有人能指出我正确的方向吗,我有一个集合(表单),每个表单都有一个嵌入的文档数组(响应)。对每种表单的响应已经变得巨大,事后看来嵌入它们是一个坏主意(包括嵌入在内的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2024-10-31 18:11:37

所以这是一个开始...这是在 mongo shell 中

db.questions.insert({name:"jwo", responses:[{question:"your name?", answer:"yomamma"}, {question:"your name?", answer:"pappa"}]});

创建了一个文档 json 结构,如下所示:

> db.questions.findOne();
{
    "_id" : ObjectId("4d877e89b75dc42c4709278d"),
    "name" : "jwo",
    "responses" : [
        {
            "question" : "your name?",
            "answer" : "yomamma"
        },
        {
            "question" : "your name?",
            "answer" : "pappa"
        }
    ]
}

现在循环响应,并使用问题的 _id 设置它们的 Question_id,然后将其插入到新的响应集合

> for(i=0; i<question.responses.length; ++i){
... question.responses[i].question_id = question._id;   
... db.responses.insert(question.responses[i]);                                                                      
... }

> db.responses.findOne();
{
    "_id" : ObjectId("4d878059b75dc42c4709278e"),
    "question" : "your name?",
    "answer" : "yomamma",
    "question_id" : ObjectId("4d877e89b75dc42c4709278d")
}

中想要更改 db.questions.findOne 以找到所有问题并循环。如果这确实需要一段时间,您可能需要切换到 Map-Reduce 函数。

So here's a start... This is in the mongo shell

db.questions.insert({name:"jwo", responses:[{question:"your name?", answer:"yomamma"}, {question:"your name?", answer:"pappa"}]});

This created a document json structure like so:

> db.questions.findOne();
{
    "_id" : ObjectId("4d877e89b75dc42c4709278d"),
    "name" : "jwo",
    "responses" : [
        {
            "question" : "your name?",
            "answer" : "yomamma"
        },
        {
            "question" : "your name?",
            "answer" : "pappa"
        }
    ]
}

Now loop through the responses, and set their question_id with the questions' _id, and then insert it into the new responses collection

> for(i=0; i<question.responses.length; ++i){
... question.responses[i].question_id = question._id;   
... db.responses.insert(question.responses[i]);                                                                      
... }

> db.responses.findOne();
{
    "_id" : ObjectId("4d878059b75dc42c4709278e"),
    "question" : "your name?",
    "answer" : "yomamma",
    "question_id" : ObjectId("4d877e89b75dc42c4709278d")
}

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.

谁的新欢旧爱 2024-10-31 18:11:37

这是我们最终得到的代码,基于 Jesse Wolgamott 的答案。

var count = 0;
db.transactions.find().sort({_id: 1}).forEach(function(t){
  if(count % 10000 == 0)
    print(""+t._id+" "+count);

  count += 1;

  for(i=0; i<t.inputs.length; ++i){
    t.inputs[i].transaction_id = t._id;
    db.input2s.insert(t.inputs[i]);
  }
});

Here is the code we ended up with, based off Jesse Wolgamott's answer.

var count = 0;
db.transactions.find().sort({_id: 1}).forEach(function(t){
  if(count % 10000 == 0)
    print(""+t._id+" "+count);

  count += 1;

  for(i=0; i<t.inputs.length; ++i){
    t.inputs[i].transaction_id = t._id;
    db.input2s.insert(t.inputs[i]);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文