2014-03-06 56 views
2

我打电话给Dropbox api“delta”以获取用户帐户中的所有图像文件。现在,我循环遍历入口数组,然后逐个插入。我想知道是否有更好的方法来插入它。node.js和猫鼬,如何批量插入?

{ 
     "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); 

回答

2

在高层次上,猫鼬似乎不支持这一点(请参阅评论)。

所以,你可以通过访问本地驱动程序的集合API:YourModel.collection

然后你就可以做本地INSERT命令:http://docs.mongodb.org/manual/reference/command/insert/#dbcmd.insert

它接受的文档的数组。

更新

您可以从本地驱动程序API使用mongodb bulk ops为好。

+5

请注意Model.create()不是批量插入。底层实现只是遍历所有模型元素并保存它们。 – outside2344