qinking126 qinking126 - 1 year ago 121
Node.js Question

node.js and mongoose, how to bulk insert?

I am calling dropbox api "delta" to get all image files in user's account. right now, I loop through the entries array, then insert one by one. i want to know if there's better way to insert it.

"has_more": false,
"cursor": "AAHmmLhWV0LhWya_OEKmZhPObxosWt4AHtk66EJiIm2_qoXPuwvyhWyuoH5Ybb_JVE9111PL06D_Td6v5bReJ3jpW_orbjBcYo4111LxRHqR3VKVxdQifemCZ7a-4njLA21TQbrIz5kaYe0vOczv668evAe",
"entries": [
"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"
"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

here's my dropboxEntry model.

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

Answer Source

On high level, mongoose don't seem to support this.(see comments).

So, you can get access to native driver's collection API via: YourModel.collection

And then you can do native insert command: http://docs.mongodb.org/manual/reference/command/insert/#dbcmd.insert

which accepts an array of documents.


You can use the mongodb bulk ops from native driver API as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download