1

我正在尝试从 CSV 上传超过 5,000 条评论,然后将它们插入到集合中。

我得到以下信息:

all done dfae22fc33f08cde515ac7452729cf4921d63ebe.js:24
insert failed: MongoError: E11000 duplicate key error index: ag5Uriwu.comments.$_id_  dup key: { : "SuvPB3frrkLs8nErv" } dfae22fc33f08cde515ac7452729cf4921d63ebe.js:1
Connection timeout. No DDP heartbeat received. 

手头的脚本:

  'click .importComments': function(e) {
    var $self = $(e.target);

    $self.text("Importing...");
    $("#commentsCSV").parse({
      worker: true,
      config: {
        step: function(row) {
          var data = row.data;
          for (var key in data) {
            var obj = data[key];

            post = Posts.findOne({legacyId: obj[1]});

            var comment = {
              // attributes here
            };

            Comments.insert(comment);
            Posts.update(comment.postId, {
              $inc: { commentsCount: 1 },
            });
          }
          $self.text("Import Comments");
        },
        complete: function(results, file) {
          console.log("all done");
        }
      }
    });
  }

如何在不因连接超时错误而崩溃的情况下完成这项工作?

在本地,它似乎工作得很好,但在生产 (modulus.io) 上它突然结束了。

4

1 回答 1

2

I think the problem here is not to do with DDP but with MongoDB. The DDP connection is timing out due to the MongoDB error.

You're getting a duplicate key error on the _id field. The _id field is automatically indexed by MongoDB and it is a unique index so the same value cannot appear twice in the same collection.

The CSV you're uploading likely has its own _id fields in it meaning Mongo is not generating its own binary fields (which guarantee uniqueness).

So I'd recommend removing the _id field from the CSV if it exists.

You can also try using the following package: http://atmospherejs.com/package/csv-to-collection

于 2014-08-08T07:34:24.300 回答