node.js和mongoose,如何批量插入?

时间:2014-03-06 01:40:52

标签: node.js mongoose

我正在调用dropbox api“delta”来获取用户帐户中的所有图像文件。现在,我遍历条目数组,然后逐个插入。我想知道是否有更好的方法来插入它。

    {
      "has_more": false,
      "cursor": "AAHmmLhWV0LhWya_OEKmZhPObxosWt4AHtk66EJiIm2_qoXPuwvyhWyuoH5Ybb_JVE9111PL06D_Td6v5bReJ3jpW_orbjBcYo4111LxRHqR3VKVxdQifemCZ7a-4njLA21TQbrIz5kaYe0vOczv668evAe",
      "entries": [
        [
          "/four/220214_002.jpg",
          {
            "revision": 34,
            "rev": "222005885b",
            "thumb_exists": true,
            "bytes": 105219,
            "modified": "Sun, 23 Feb 2014 14:43:43 +0000",
            "client_mtime": "Sun, 23 Feb 2014 14:43:42 +0000",
            "path": "/four/220214_002.jpg",
            "is_dir": false,
            "icon": "page_white_picture",
            "root": "dropbox",
            "mime_type": "image/jpeg",
            "size": "102.8 KB"
          }
        ],
        [
          "/four/250214_002.jpg",
          {
            "revision": 65,
            "rev": "412005885b",
            "thumb_exists": true,
            "bytes": 96909,
            "modified": "Thu, 27 Feb 2014 00:45:28 +0000",
            "client_mtime": "Thu, 27 Feb 2014 00:45:23 +0000",
            "path": "/four/250214_002.jpg",
            "is_dir": false,
            "icon": "page_white_picture",
            "root": "dropbox",
            "mime_type": "image/jpeg",
            "size": "94.6 KB"
          }
        ],
        ...........

      ],
      "reset": true
    }

这是我的dropboxEntry模型。

            var mongoose = require('mongoose');

            var dropboxEntrySchema = new mongoose.Schema({
                //uid: Number,
                dropbox_uid: Number,
                revision: Number,
                rev: String,
                thumb_exists: Boolean,
                bytes: Number,
                modified: Date,
                path: { type: String, unique: true},
                path1: {type: String, unique: true},
                is_dir: Boolean,
                icon: String,
                root: String,
                size: String,
                deleted: Boolean,
                downloaded: Boolean,
                thumbnail: String
            });

            module.exports = mongoose.model('DropboxEntry', dropboxEntrySchema);

1 个答案:

答案 0 :(得分:2)

在高级别上,猫鼬似乎不支持这一点。(见评论)。

因此,您可以通过以下方式访问本机驱动程序的集合API:YourModel.collection

然后你可以做本机插入命令:http://docs.mongodb.org/manual/reference/command/insert/#dbcmd.insert

接受一系列文件。

<强>更新

您也可以使用本机驱动程序API中的mongodb bulk ops